Skip to content

gtk窗口拖动(迁移2010-12-31)

Published: at 01:07 PM | 1 min read

实现按下鼠标左键拖动窗口


  1. 加入事件:
gtk_widget_add_events(window, GDK_BUTTON_PRESS_MASK);
  1. 关联信号与button按下的事件:
g_signal_connect(G_OBJECT(window), "button-press-event", G_CALLBACK(window_drag), NULL);
  1. 窗口拖动事件的实现:
gboolean window_drag(GtkWidget *widget, GdkEventButton *event, GdkWindowEdge edge)
{
    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;

使窗口不能移出桌面最左边,在标题栏显示当前窗口的位置坐标


  1. 关联信号与事件:
g_signal_connect(G_OBJECT(window), "configure-event", G_CALLBACK(window_move), NULL);
  1. 窗口移动事件的实现:
gboolean window_move(GtkWidget *widget, GdkEvent *event, gpointer data)
{
    gint x, y;
    gchar buf[10] = {0};
    x = event->configure.x;
    y = event->configure.y;
    if (x <= 0)//如果窗口超出桌面最左边框
    {
        x = 0;
        gtk_window_move(GTK_WINDOW(widget), x, y);
    }
    sprintf(buf, "%d,%d", x, y);
    gtk_window_set_title(GTK_WINDOW(widget), buf);
    return TRUE;
}