外面我使用GTK +和空地工具C. 它的父窗口彈出時,按鈕按下開發一個彈出窗口(飾非)。當用戶點擊此窗口時,我想銷燬或隱藏此彈出窗口。用戶可以單擊父窗口或任何其他窗口。 我試圖捕獲GDK_FOCUS_CHANGE
事件,但我無法捕獲此事件。有什麼辦法可以做到這一點?我如何知道點擊是在其他窗口上,然後彈出窗口?如何清楚彈出窗口失去了焦點? 這樣我就可以隱藏它。 相關的代碼如下:如何隱藏的Gtk彈出式窗口,當用戶clickes窗口
/*
* 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版本。
請不要轉發問題。這似乎是(更好地呈現)重複http://stackoverflow.com/questions/1740947/problem-with-gtk-popup-window-focus-change-event-handling – mlibby 2009-11-17 12:21:50
我將在未來照顧這一點。 – kbalar 2009-11-17 14:52:37