抱歉,这是一个警告提示,表示gdk_screen_get_width()
函数已被标记为过时。为了避免警告,您可以使用新的替代函数gdk_screen_get_width_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);
// 将毫米转换为像素,假设每毫米有 0.0393701 像素
gint screen_width_px = (gint)(screen_width_mm * 0.0393701);
gint window_width, window_height;
gtk_window_get_size(window, &window_width, &window_height);
gint x = (screen_width_px - window_width) / 2;
gint y = (screen_height_px - 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()
函数获取屏幕的宽度,然后将其转换为像素。请注意,转换因子0.0393701是一个近似值,根据您的实际情况可能需要进行调整。
内容由零声教学AI助手提供,问题来源于学员提问