gpt4 book ai didi

c - 保存 gtk 窗口位置

转载 作者:IT王子 更新时间:2023-10-29 00:54:40 27 4
gpt4 key购买 nike

我正在尝试保存 gtk 窗口位置(绝对)以便在我再次打开应用程序时恢复它

到目前为止,这是我的代码:

gint x,y;
gtk_window_get_position(main_window,&x,&y);
printf("current position is:\nx: %i\ny:%i\n",x,y);

此代码在应用程序退出时运行,我总是得到:当前位置是:×:0y:0

我做错了什么。

最佳答案

gtk_window_get_position 通常会做出最佳猜测,但您不能依赖它,因为

the X Window System does not specify a way to obtain the geometry of the decorations placed on a window by the window manager.

(来自 gtk_window_get_position reference)

要查看运行中的函数,请尝试以下操作:

#include <gtk/gtk.h>

int main(int argv, char* argc[])
{
GtkWidget *window, *button;
gint x, y;

gtk_init(&argv, &argc);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Test Window");
button = gtk_button_new_with_label("Close");

g_signal_connect(G_OBJECT(button), "clicked",
G_CALLBACK(gtk_main_quit), (gpointer)NULL);
gtk_container_set_border_width(GTK_CONTAINER(window), 10);
gtk_container_add(GTK_CONTAINER(window), button);
gtk_window_get_position(GTK_WINDOW(window), &x, &y);

printf("current position is:\nx: %i\ny:%i\n", x, y);
gtk_window_set_position(GTK_WINDOW(window),
GTK_WIN_POS_CENTER_ALWAYS);
gtk_window_get_position(GTK_WINDOW(window), &x, &y);

printf("new position is:\nx: %i\ny:%i\n", x, y);
gtk_widget_show_all(window);


gtk_main();
}

编辑

如果你希望窗口出现在一个特定的位置,你可以尝试这样的事情:

 gtk_window_move(GTK_WINDOW(window), 420, 180);

但是上面的函数应该放在后面

gtk_widget_show_all(window);

因为

most window managers ignore requests for initial window positions (instead using a user-defined placement algorithm) and honor requests after the window has already been shown.

(来自 gtk_window_move reference)

关于c - 保存 gtk 窗口位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34168786/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com