300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > c语言窗口内鼠标位置 Gtk获取窗口内光标 | 指针 | 鼠标的绝对和相对坐标 绝对和相对位置...

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

时间:2020-02-06 14:41:51

相关推荐

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。