c语言窗口内鼠标位置,Gtk获取窗口内光标 | 指针 | 鼠标的绝对和相对坐标,绝对和相对位置...

获取指针绝对坐标和窗口内的相对坐标

方法一:直接调用函数获得

int ax, ay; /* absolutely postition*/

int wx, wy; /* The absolutely position of the left up corner of window*/

int rx, ry; /* The position of the pointer relative to the left up corner of window*/

GdkDisplay *display = gdk_display_get_default ();

GdkSeat *device_manager = gdk_display_get_default_seat (display);

GdkDevice *device = gdk_seat_get_pointer (device_manager);

/*获取指针在屏幕上的绝对坐标*/

gdk_device_get_position (device, NULL, &ax, &ay);

/*获取窗口左上角在屏幕上的绝对坐标*/

gdk_window_get_position ( gtk_widget_get_window(window), &wx, &wy );

rx = ax - wx; /*通过计算获取指针相对于窗口的坐标rx*/

ry = ay - wy; /*通过计算获取指针相对于窗口的坐标ry*/

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

intax,ay;/* absolutely postition*/

intwx,wy;/* The absolutely position of the left up corner of window*/

intrx,ry;/* The position of the pointer relative to the left up corner of window*/

GdkDisplay *display=gdk_display_get_default();

GdkSeat *device_manager=gdk_display_get_default_seat(display);

GdkDevice *device=gdk_seat_get_pointer(device_manager);

/*获取指针在屏幕上的绝对坐标*/

gdk_device_get_position(device,NULL,&ax,&ay);

/*获取窗口左上角在屏幕上的绝对坐标*/

gdk_window_get_position(gtk_widget_get_window(window),&wx,&wy);

rx=ax-wx;/*通过计算获取指针相对于窗口的坐标rx*/

ry=ay-wy;/*通过计算获取指针相对于窗口的坐标ry*/

详情见注释,注意window变量来自 Gtkwidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL); 代码中没有给出

方法二: 注册回调函数获得

/*主函数内*/

g_signal_connect(window, "motion-notify-event", G_CALLBACK(deal_motion_notify_event), NULL);

1

2

3

4

/*主函数内*/

g_signal_connect(window,"motion-notify-event",G_CALLBACK(deal_motion_notify_event),NULL);

/*回调函数*/

gboolean deal_motion_notify_event ( GtkWidget *widget, GdkEventMotion *event, gpointer *data) {

int wx, wy; /* The absolutely position of the left up corner of window*/

int rx, ry; /* The position of the pointer relative to the left up corner of window*/

gdk_window_get_position ( gtk_widget_get_window(window), &wx, &wy );

rx = (int)event->x_root - wx;

ry = (int)event->y_root - wy;

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

/*回调函数*/

gbooleandeal_motion_notify_event(GtkWidget *widget,GdkEventMotion *event,gpointer *data){

intwx,wy;/* The absolutely position of the left up corner of window*/

intrx,ry;/* The position of the pointer relative to the left up corner of window*/

gdk_window_get_position(gtk_widget_get_window(window),&wx,&wy);

rx=(int)event->x_root-wx;

ry=(int)event->y_root-wy;

}

其中 event->x_root  ,event->y_root  就是指针在屏幕上的绝对坐标,wx,wy同方法一,为窗口左上角的坐标值,二者作差即可得指针在窗口内的坐标(相对于窗口左上角的坐标值)。

注意window变量来自 Gtkwidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL); 代码中没有给出,请自行想办法传递进去

其中主函数内监听的信号该从GTK文档中何处寻找呢?

首先我们用 gtk_window_new 新建了一个窗口,窗口类型由 GtkWidget * 声明,所以所有需要用到的信号应从 GtkWidget 中去寻找,点开链接后,下拉至 Signals,下面指示的所有信号都是 Gtkwidget 可以监听的,如下:

最左侧是回调函数的返回类型,中间的为监听的信号名称,点击监听名称,可跳转至回调函数的原型示例,包含内部参数应是什么类型等内容。

Post Views:

981

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值