[英]How to detect mouse click over an image in GTK+?
我正在使用gtk + 2.0在C项目中工作。
我必须检查用户是否按下左键单击图像。 我想在按下左键时调用一个函数来获取鼠标的位置,但是我怎么能这样做呢?
我希望我可以假设您知道如何将事件连接到窗口小部件,但如果没有:这是我之前的答案,演示了如何做到这一点。
正如您所看到的那样,事件作为GdkEventButton *
传递(从现在开始的event
)。 这个结构有你以后的成员字段: gdouble
event->x
和gdouble
event->y
都是gdouble
字段。
无论如何,@ unwind是对的。 正如GTK文档明确指出:
GtkImage是一个“无窗口”小部件(它没有自己的GdkWindow),因此默认情况下不接收事件。 如果要在图像上接收事件(例如按钮单击),请将图像放在GtkEventBox中,然后连接到事件框上的事件信号。
GtkImage
不是唯一的“无窗口”小部件,BTW。 例如,如果您想要处理标签上的点击, GtkLabel
需要类似的方法。 无论如何: 这里有更多信息 。
然后,手册页继续提供有关如何处理GtkImage
小部件上的点击的完整代码示例 。 只需查看标题“处理GtkImage上的按钮按钮事件”。 有关完整的解释,但这里是链接中断的代码:
static gboolean
button_press_callback (GtkWidget *event_box,
GdkEventButton *event,
gpointer data)
{
g_print ("Event box clicked at coordinates %f,%f\n",
event->x, event->y);
// Returning TRUE means we handled the event, so the signal
// emission should be stopped (don’t call any further callbacks
// that may be connected). Return FALSE to continue invoking callbacks.
return TRUE;
}
static GtkWidget*
create_image (void)
{
GtkWidget *image;
GtkWidget *event_box;
image = gtk_image_new_from_file ("myfile.png");
event_box = gtk_event_box_new ();
gtk_container_add (GTK_CONTAINER (event_box), image);
g_signal_connect (G_OBJECT (event_box),
"button_press_event",
G_CALLBACK (button_press_callback),
image);
return image;
}
问题是用于在GTK +中显示图像的GtkImage小部件不会生成事件。
它是一个“nowindow”小部件,意味着它是一个被动容器,用于显示信息而不与用户交互。
您可以通过将图像包装在GtkEventBox中来解决这个问题 ,这将添加事件支持。
在GTK中,您可以使用按钮按下事件 gtk小部件来执行此操作
在纯c中,来自Programming Simplified
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<dos.h>
int initmouse();
void showmouseptr();
void hidemouseptr();
void getmousepos(int*,int*,int*);
union REGS i, o;
main()
{
int gd = DETECT, gm, status, button, x, y, tempx, tempy;
char array[50];
initgraph(&gd,&gm,"C:\\TC\\BGI");
settextstyle(DEFAULT_FONT,0,2);
status = initmouse();
if ( status == 0 )
printf("Mouse support not available.\n");
else
{
showmouseptr();
getmousepos(&button,&x,&y);
tempx = x;
tempy = y;
while(!kbhit())
{
getmousepos(&button,&x,&y);
if( x == tempx && y == tempy )
{}
else
{
cleardevice();
sprintf(array,"X = %d, Y = %d",x,y);
outtext(array);
tempx = x;
tempy = y;
}
}
}
getch();
return 0;
}
int initmouse()
{
i.x.ax = 0;
int86(0X33,&i,&o);
return ( o.x.ax );
}
void showmouseptr()
{
i.x.ax = 1;
int86(0X33,&i,&o);
}
void getmousepos(int *button, int *x, int *y)
{
i.x.ax = 3;
int86(0X33,&i,&o);
*button = o.x.bx;
*x = o.x.cx;
*y = o.x.dx;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.