gtk窗口拖动
1.实现按下鼠标左键拖动窗口1>加入事件:
gtk_widget_add_events(window, GDK_BUTTON_PRESS_MASK);
2>关联信号与button按下的事件:
g_signal_connect(G_OBJECT(window), "button-press-event", G_CALLBACK(window_drag), NULL);
3>窗口拖动事件的实现:
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;
}
注意这里:GTK+3中,第二个参数是GdkEvent,而不是GdkEventButton,所以,如果是GTK+3,那么请把所有的event->后面加上button.
例如:event->button改成event->button.button
2.使窗口不能移出桌面最左边,在标题栏显示当前窗口的位置坐标
1>关联信号与事件:
g_signal_connect(G_OBJECT(window), "configure-event", G_CALLBACK(window_move), NULL);
2>窗口移动事件的实现:
gboolean window_move(GtkWidget *widget, GdkEvent *event, gpointer data)
{
gint x, y;
gchar buf = {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;
} 我在这里补充下,贴上个实例和大家分享下,以下是链接:鼠标拖动窗口
页:
[1]