GTK编程窗口居中显示

LINUX2010-11-21 08:48

方式一:

gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER_ALWAYS);
gtk_window_show (GTK_WINDOW (window));

方式二:

//只有先显示才能产生窗口
gtk_widget_show_all (GTK_WINDOW (window));
GdkScreen* screen = gdk_screen_get_default ();
//获取当前屏幕的宽和高
gint screen_w = gdk_screen_get_width (screen);
gint screen_h = gdk_screen_get_height (screen);
//获取当前窗口的宽和高
gint window_w = window->allocation.width;
gint window_h = window->allocation.height;
//将窗口移动到屏幕中心
gtk_window_move (GTK_WINDOW (window), (screen_w - window_w) / 2, (screen_h - window_h) / 2);

原文链接: http://blog.jtwo.me/center-window-display-in-gtk-program