簡體   English   中英

如何在GTK +中檢測鼠標點擊圖像?

[英]How to detect mouse click over an image in GTK+?

我正在使用gtk + 2.0在C項目中工作。

我必須檢查用戶是否按下左鍵單擊圖像。 我想在按下左鍵時調用一個函數來獲取鼠標的位置,但是我怎么能這樣做呢?

我希望我可以假設您知道如何將事件連接到窗口小部件,但如果沒有:這是我之前的答案,演示了如何做到這一點。

g_signal_connect用於鼠標右鍵單擊?

正如您所看到的那樣,事件作為GdkEventButton *傳遞(從現在開始的event )。 這個結構有你以后的成員字段: gdouble event->xgdouble 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM