gtk窗口拖拽

来源:互联网 发布:手机怎么删除淘宝评价 编辑:程序博客网 时间:2024/05/02 01:52

In the next example, we show an interesting feature. We will show a borderless window and learn, how we can drag and move such a window.

#include <gtk/gtk.h>gboolean on_button_press (GtkWidget* widget,  GdkEventButton * event, GdkWindowEdge edge){  if (event->type == GDK_BUTTON_PRESS)  {    if (event->button == 1) {      gtk_window_begin_move_drag(GTK_WINDOW(gtk_widget_get_toplevel(widget)),          event->button,  event->x_root,  event->y_root,  event->time);    }  }  return FALSE;}int main( int argc, char *argv[]){  GtkWidget *window;  gtk_init(&argc, &argv);  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);  gtk_window_set_default_size(GTK_WINDOW(window), 230, 150);  gtk_window_set_title(GTK_WINDOW(window), "Drag & drop");  gtk_window_set_decorated(GTK_WINDOW (window), FALSE);  gtk_widget_add_events(window, GDK_BUTTON_PRESS_MASK);  g_signal_connect(G_OBJECT(window), "button-press-event",      G_CALLBACK(on_button_press), NULL);  g_signal_connect_swapped(G_OBJECT(window), "destroy",        G_CALLBACK(gtk_main_quit), G_OBJECT(window));  gtk_widget_show(window);  gtk_main();  return 0;}

The example demonstrates a drag and drop of a borderless window.

 gtk_window_set_decorated(GTK_WINDOW (window), FALSE);

We remove the decoration of the window. This means, that the window will not have borders and titlebar.

  g_signal_connect(G_OBJECT(window), "button-press-event",      G_CALLBACK(on_button_press), NULL);

We connect the window to the button-press-event signal.

gboolean on_button_press (GtkWidget* widget,  GdkEventButton * event, GdkWindowEdge edge){  if (event->type == GDK_BUTTON_PRESS)  {    if (event->button == 1) {      gtk_window_begin_move_drag(GTK_WINDOW(gtk_widget_get_toplevel(widget)),          event->button,  event->x_root,  event->y_root,  event->time);    }  }  return FALSE;}

Inside the on_button_press(), we do perform the drag and drop operation. We check if the left mouse button was pressed. Then we call the gtk_window_begin_move_drag() function.

0 0
原创粉丝点击