【GTK】窗口停靠

源码:
#include <gdk/gdkcursor.h>
#include <gtk/gtk.h>

#define TEST_W 100
#define TEST_H 80

gboolean drag = FALSE;   // 只在左键按下时拖动窗体
gboolean hide = FALSE;   // 只在左键按下时拖动窗体
int nX = 0;
int nY = 0;
GtkWidget *window;


static gint button_press_event(GtkWidget * widget,GdkEventButton * event, gpointer data)

{

        if (event->button == 1)       // 判断是否左键按下
        {
                 drag = TRUE;
                 nX = event->x;  // 取得鼠标相对于窗口的位置
                 nY = event->y;

        }
        // gdk_window_move_resize (widget->window, x, y, width, height);
        //gdk_window_move_resize (widget->window, 0, 0, 10, 80);
        //gtk_widget_set_usize(window,50,TEST_H);// gtk_widget_set_size_request(window, 5, TEST_H);
        return TRUE;

}
static gint button_release_event(GtkWidget * widget, GdkEventButton * event,gpointer data)         // 鼠标抬起事件
{

    //gdk_window_move_resize (widget->window, 0, 0, 100, 80);
    //gtk_widget_set_usize(window,10, TEST_H);//gtk_widget_set_size_request(window, TEST_W, TEST_H);
        if (event->button == 1)

                 drag = FALSE;
        
        return TRUE;

}
static gint motion_notify_event(GtkWidget * widget, GdkEventButton * event,gpointer data)//鼠标移动事件
{
       gint x, y;
       GtkWidget *window = (GtkWidget *) data;
       gtk_window_get_position((GtkWindow *) window, &x, &y);         // 取窗体绝对坐标

       gdk_window_move_resize (widget->window, x, y, 100, 80);
        if (drag)
        {
                 
                 gtk_window_move((GtkWindow *) window, x + event->x - nX, y + event->y - nY);// 移动窗体
                 g_print("x=%d;y=%d\n",x,y);
                
        }

        return TRUE;

}
gboolean leave_event (GtkWidget *widget,GdkEventCrossing  *event,gpointer  data)//鼠标离开事件
{
    gint x, y;
        GtkWidget *window = (GtkWidget *) data;
        gtk_window_get_position((GtkWindow *) window, &x, &y);// 取窗体绝对坐标
        if(x<=5)
        
            gdk_window_move_resize (widget->window, 0, y, 5, 80);
           
       
    return TRUE;
}
int main(int argc, char **argv)
{
     
        gtk_init(&argc, &argv);
        window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        //gtk_window_set_decorated(GTK_WINDOW(window), FALSE);   // 去掉边框
        //gtk_widget_set_size_request(window, TEST_W, TEST_H);//设置窗体最小尺寸
        //gtk_window_set_default_size(window,TEST_W,TEST_H);
        //gtk_widget_set_usize(window,TEST_W, TEST_H);//设置窗体大小
        //gtk_window_move((GtkWindow *) window, -20, -10);
        gtk_widget_set_events(window,  // 设置窗体获取鼠标事件
                //window.allocation

                   GDK_EXPOSURE_MASK | GDK_LEAVE_NOTIFY_MASK

                   | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK

                   | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK);

        gtk_signal_connect(GTK_OBJECT(window), "button_press_event",

                   (GtkSignalFunc) button_press_event, window);       // 加入事件回调

        gtk_signal_connect(GTK_OBJECT(window), "motion_notify_event",

                   (GtkSignalFunc) motion_notify_event, window);

        gtk_signal_connect(GTK_OBJECT(window), "button_release_event",

                   (GtkSignalFunc) button_release_event, window);
        g_signal_connect (GTK_OBJECT (window), "leave_notify_event",

                   (GtkSignalFunc)   leave_event, window);

        gtk_widget_show_all(window);
        gtk_main();
        return TRUE;

}


posted @ 2011-12-26 12:41  MXi4oyu  阅读(297)  评论(0编辑  收藏  举报