gpt4 book ai didi

c - 用户在窗口外单击时如何隐藏 Gtk 弹出窗口

转载 作者:太空狗 更新时间:2023-10-29 16:44:35 24 4
gpt4 key购买 nike

我在 C 中使用 GTK+ 和 glade 工具开发了一个弹出窗口(非装饰)。单击按钮时,它会在其父窗口中弹出。当用户单击此窗口外时,我想销毁或隐藏此弹出窗口。用户可以单击父窗口或任何其他窗口。我试图捕获 GDK_FOCUS_CHANGE 事件,但我无法捕获此事件。有什么办法可以做到这一点?我怎么知道点击是在其他窗口然后弹出窗口?如何清楚弹出窗口失去了焦点?这样我就可以隐藏它。相关代码如下:

/*
* Compile me with:

gcc -o popup popup.c $(pkg-config --cflags --libs gtk+-2.0 gmodule-2.0)
*/

#include <gtk/gtk.h>

static void on_popup_clicked (GtkButton*, GtkWidget*);
static gboolean on_popup_window_event(GtkWidget*, GdkEventExpose*);

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

gtk_init (&argc, &argv);

window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "Parent window");
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
gtk_widget_set_size_request (window, 300, 300);
gtk_window_set_position (GTK_WINDOW (window),GTK_WIN_POS_CENTER);

button = gtk_button_new_with_label("Pop Up");
g_signal_connect (G_OBJECT (button), "clicked",G_CALLBACK (on_popup_clicked),(gpointer) window);

vbox = gtk_vbox_new (FALSE, 3);
gtk_box_pack_end(GTK_BOX (vbox), button, FALSE, FALSE, 5);
gtk_container_add (GTK_CONTAINER (window), vbox);

gtk_widget_show_all (window);
gtk_main ();
return 0;
}

void on_popup_clicked (GtkButton* button, GtkWidget* pWindow)
{
GtkWidget *popup_window;
popup_window = gtk_window_new (GTK_WINDOW_POPUP);
gtk_window_set_title (GTK_WINDOW (popup_window), "Pop Up window");
gtk_container_set_border_width (GTK_CONTAINER (popup_window), 10);
gtk_window_set_resizable(GTK_WINDOW (popup_window), FALSE);
gtk_window_set_decorated(GTK_WINDOW (popup_window), FALSE);
gtk_widget_set_size_request (popup_window, 150, 150);
gtk_window_set_transient_for(GTK_WINDOW (popup_window),GTK_WINDOW (pWindow));
gtk_window_set_position (GTK_WINDOW (popup_window),GTK_WIN_POS_CENTER);
g_signal_connect (G_OBJECT (button), "event",
G_CALLBACK (on_popup_window_event),NULL);

GdkColor color;
gdk_color_parse("#3b3131", &color);
gtk_widget_modify_bg(GTK_WIDGET(popup_window), GTK_STATE_NORMAL, &color);


gtk_widget_show_all (popup_window);
}

gboolean on_popup_window_event(GtkWidget *popup_window, GdkEventExpose *event)
{
if(event->type == GDK_FOCUS_CHANGE)
gtk_widget_hide (popup_window);

return FALSE;
}

在这里,当用户单击父窗口或其他窗口时,我无法隐藏此弹出窗口。我怎样才能做到这一点?

我必须坚持使用 Gtk+2.14 版本。

最佳答案

变化:

  • GTK_WINDOW_POPUP 切换到 GTK_WINDOW_TOPLEVEL,违反直觉,但我不知道如何让弹出窗口接受焦点。
  • 添加 gtk_window 提示以防止弹出窗口显示在任务栏和寻呼​​机中
  • 故意将焦点放在弹出窗口上
  • 使用 gtk_widget_set_eventsGDK_WINDOW 上设置 GDK_FOCUS_CHANGE_MASK(下一步需要)
  • 连接到弹窗的focus-out-event
  • 更改信号处理程序以处理不同的信号

我还建议阅读 GTK+ 源代码,看看它是如何处理显示工具提示和菜单的弹出窗口的……但是这些通常是根据鼠标移出范围而不是弹出窗口失去焦点而销毁的se.


#include

static void on_popup_clicked (GtkButton*, GtkWidget*);
gboolean on_popup_focus_out (GtkWidget*, GdkEventFocus*, gpointer);

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

gtk_init (&argc, &argv);

window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "Parent window");
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
gtk_widget_set_size_request (window, 300, 300);
gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);

button = gtk_button_new_with_label ("Pop Up");
g_signal_connect (G_OBJECT (button),
"clicked",
G_CALLBACK (on_popup_clicked),
(gpointer) window);

vbox = gtk_vbox_new (FALSE, 3);
gtk_box_pack_end (GTK_BOX (vbox), button, FALSE, FALSE, 5);
gtk_container_add (GTK_CONTAINER (window), vbox);

gtk_widget_show_all (window);
gtk_main ();
return 0;
}

void
on_popup_clicked (GtkButton* button, GtkWidget* pWindow)
{
GtkWidget *popup_window;

popup_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (popup_window), "Pop Up window");
gtk_container_set_border_width (GTK_CONTAINER (popup_window), 10);
gtk_window_set_resizable (GTK_WINDOW (popup_window), FALSE);
gtk_window_set_decorated (GTK_WINDOW (popup_window), FALSE);
gtk_window_set_skip_taskbar_hint (GTK_WINDOW (popup_window), TRUE);
gtk_window_set_skip_pager_hint (GTK_WINDOW (popup_window), TRUE);
gtk_widget_set_size_request (popup_window, 150, 150);
gtk_window_set_transient_for (GTK_WINDOW (popup_window), GTK_WINDOW (pWindow));
gtk_window_set_position (GTK_WINDOW (popup_window), GTK_WIN_POS_CENTER);

gtk_widget_set_events (popup_window, GDK_FOCUS_CHANGE_MASK);
g_signal_connect (G_OBJECT (popup_window),
"focus-out-event",
G_CALLBACK (on_popup_focus_out),
NULL);

GdkColor color;
gdk_color_parse ("#3b3131", &color);
gtk_widget_modify_bg (GTK_WIDGET (popup_window), GTK_STATE_NORMAL, &color);

gtk_widget_show_all (popup_window);
gtk_widget_grab_focus (popup_window);
}

gboolean
on_popup_focus_out (GtkWidget *widget,
GdkEventFocus *event,
gpointer data)
{
gtk_widget_destroy (widget);
return TRUE;
}

关于c - 用户在窗口外单击时如何隐藏 Gtk 弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1747519/

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