4

我在 C++ 中使用 C 风格的 GTK 函数,但我不知道如何设置主窗口的光标。

4

5 回答 5

8

采用gdk_set_cursor()

https://developer.gnome.org/gdk3/stable/gdk3-Windows.html#gdk-window-set-cursor

在由创建的 GdkCursor 上gdk_cursor_new()

https://developer.gnome.org/gdk3/stable/gdk3-Cursors.html

于 2012-04-21T17:55:28.770 回答
6

gdk_window_set_cursor()在由 .创建的 GdkCursor 上使用gdk_cursor_new_from_name()

要获取 GtkWindow 的 GdkWindow,您可以使用gtk_widget_get_window(),因为 GtkWindow 是 GtkWidget 的子类。

注意:此答案是对idefixs 答案(重要更正+链接更新+小修改以使答案完整+改进格式)的改进,该答案被拒绝为编辑。

于 2017-08-24T15:46:39.910 回答
5

发布此内容是因为有关获取 GdkWndow 的评论到目前为止没有得到答复。

对于大多数小部件,可以将 GdkWindow 作为 GtkWidget 结构的窗口数据字段检索。以下代码在 GtkWindow 小部件上设置光标:

GtkWidget* win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
GdkCursor* watchCursor = gdk_cursor_new(GDK_WATCH);

/* set watch cursor */
gdk_window_set_cursor(win->window, watchCursor);

/* return to normal */
gdk_window_set_cursor(win->window, NULL);

如果您需要从 GtkWindow 的其中一个子小部件访问祖先,您可以使用:

GtkWidget* win = gtk_widget_get_ancestor(someWidget, GTK_TYPE_WINDOW);
于 2012-07-06T22:54:39.657 回答
1

我的解决方案:

void gtkSetCursor(GdkCursorType cursorType) {
    GdkScreen * screen = gdk_screen_get_default();
    GdkWindow * win = gdk_screen_get_root_window(screen);
    GdkCursor * cursor = gdk_cursor_new(cursorType); //http://developer.gimp.org/api/2.0/gdk/gdk-Cursors.html
    gdk_window_set_cursor(win, cursor);
    while (gtk_events_pending()) gtk_main_iteration();
}

...
gtkSetCursor(GDK_WATCH);
start your stuff here
...
end of your stuff
gtkSetCursor(GDK_LEFT_PTR);
于 2016-04-06T18:20:25.180 回答
0

杰夫的回答对我不起作用(Gtk3)。所以这是我的解决方案:

GdkWindow* win = gtk_widget_get_parent_window(widget);
GdkCursor* watchCursor = gdk_cursor_new(GDK_WATCH);
gdk_window_set_cursor(win, watchCursor);
于 2014-05-24T13:31:28.537 回答