GTK窗口位置问题
1.获取顶层窗口的位置和大小:
// 获取包含entry控件的顶层窗口的位置和大小
GdkRectangle window_rect;
GdkWindow* window = gtk_widget_get_window(gtk_widget_get_toplevel(m_entryparent));//m_entryparent为任一控件
//获取顶层窗口相对于屏幕左上角的坐标(获取屏幕坐标)
gdk_window_get_origin(window, &window_rect.x, &window_rect.y);
//获取窗口的几何属性,包括位置、大小和边框等
gdk_window_get_geometry(window, NULL, NULL, &window_rect.width, &window_rect.height);
2.获取控件的位置和大小:
// 获取 entry 控件的位置和大小
GdkRectangle entry_rect;
gtk_widget_get_allocation(m_entryparent, &entry_rect);
注意:gtk_widget_get_allocation获取控件的位置不正确;通过以下代码获取控件相对于顶级窗口的位置
// 获取 entry 控件相对于顶级窗口的位置
GtkWidget* toplevel = gtk_widget_get_toplevel(m_entryparent);
int x1, y1;
gtk_widget_translate_coordinates(m_entryparent, toplevel, 0, 0, &x1, &y1);