抱歉,上述代码中使用的gdk_screen_get_width()
和gdk_screen_get_height()
函数已经被标记为过时。为了避免警告,可以使用替代函数gdk_screen_get_width_mm()
和gdk_screen_get_height_mm()
来获取屏幕宽度和高度(以毫米为单位)。然后将其转换为像素单位。
修改后的代码如下:
#include <gtk/gtk.h>
void move_window_to_center(GtkWidget *widget, gpointer data) {
GtkWindow *window = GTK_WINDOW(data);
GdkScreen *screen = gtk_widget_get_screen(GTK_WIDGET(window));
gint screen_width_mm = gdk_screen_get_width_mm(screen);
gint screen_height_mm = gdk_screen_get_height_mm(screen);
gint screen_width = gdk_screen_width() * screen_width_mm / gdk_screen_width_mm();
gint screen_height = gdk_screen_height() * screen_height_mm / gdk_screen_height_mm();
gint window_width, window_height;
gtk_window_get_size(window, &window_width, &window_height);
gint x = (screen_width - window_width) / 2;
gint y = (screen_height - window_height) / 2;
gtk_window_move(window, x, y);
}
int main(int argc, char *argv[]) {
gtk_init(&argc, &argv);
GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Move Window to Center");
GtkWidget *button = gtk_button_new_with_label("Move Window");
g_signal_connect(button, "clicked", G_CALLBACK(move_window_to_center), window);
GtkWidget *box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
gtk_container_add(GTK_CONTAINER(box), button);
gtk_container_add(GTK_CONTAINER(window), box);
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
通过使用gdk_screen_get_width_mm()
和gdk_screen_get_height_mm()
函数来获取屏幕的宽度和高度(以毫米为单位),然后根据屏幕的像素密度进行转换,可以获得准确的像素值。
内容由零声教学AI助手提供,问题来源于学员提问