GTK Advanced Learning: Mouse events

Source: Internet
Author: User
Tags gtk

Mouse events can be understood as actions that manipulate the mouse. For a window, the user operates the mouse, the window detects the mouse operation (generating a signal), and then to do the corresponding processing (call its prescribed callback function), can be considered as a mouse event, or signal and callback function knowledge points.


For the use of signals and callback functions, please click here.


The window does not receive mouse actions by default and needs to be manually added to receive them.


Set the control to capture (receive) the corresponding event:

void Gtk_widget_add_events (Gtkwidget *widget, gint events);

Widgets: Controls

Events: The event type, which is an enumeration constant of Gdkeventmask, is enumerated as follows

Gdk_button_press_mask: Mouse click

Gdk_button_release_mask: Mouse Release

Gdk_button_motion_mask: Mouse Movement

Gdk_key_press_mask: Keyboard pressed

Gdk_enter_notify_mask: entering the control area

...... ......


Signal generated by mouse click event: button-press-event

Signal generated by mouse release event: button-release-event

Its callback function is defined as follows:

Gboolean Callback (Gtkwidget *widget, Gdkeventbutton *event, gpointer data)

{

Event: Mouse key events struct variables, internal variables of the system, not user-transmitted

Event->x,event->y: Get Click Coordinate values

Event->button: which button of the mouse is pressed

Event->type: Whether to double-click


Return value: It doesn't make much sense.

}


Signals generated by mouse movement events: motion-notify-event

Its callback function is defined as follows:

Gboolean Callback (Gtkwidget *widget, gdkeventmotion *event, gpointer data)

{

Event->x,event->y: Get the Moving coordinate value

}


Note: You can find the corresponding event signal in Gtkwidget, how the callback function defines that the help document should be viewed.


Please click here for tutorials on how to use the Help documentation.


The following example is a click of the mouse can get the coordinates of the click, at the same time can determine which key pressed, moving the mouse can also get its coordinates:

#include <gtk/gtk.h>//header file//mouse click event handler Gboolean deal_mouse_press (Gtkwidget *widget, Gdkeventbutton *event, G Pointer data) {switch (Event->button) {///To determine the type of mouse click Case 1:printf ("left button!!              \ n ");          Break Case 2:printf ("Middle button!!              \ n ");          Break Case 3:printf ("right button!!              \ n ");          Break default:printf ("Unknown button!!      \ n ");      } if (Event->type = = gdk_2button_press) {printf ("double click\n");      }//Get the coordinate value of the click, distance from the left vertex of the window gint i = event->x;      Gint j = event->y;        printf ("press_x =%d, press_y =%d\n", I, j);  return TRUE;  }//Mouse move event (click any mouse button) handler function Gboolean deal_motion_notify_event (gtkwidget *widget, gdkeventmotion *event, gpointer data)      {//get the coordinate value of the moving mouse, gint i = event->x; from the left vertex of the window      Gint j = event->y;            printf ("motion_x =%d, motion_y =%d\n", I, j); REturn TRUE;     } int main (int argc,char *argv[]) {gtk_init (&AMP;ARGC, &AMP;ARGV);      Initialize//Create top-level window Gtkwidget *window = gtk_window_new (gtk_window_toplevel);      Set the title of the Window Gtk_window_set_title (Gtk_window (window), "mouse_event");      Sets the position of the window in the display as centered gtk_window_set_position (Gtk_window (window), gtk_win_pos_center);      Sets the minimum size of the window gtk_widget_set_size_request (Windows, 400, 300);        "Destroy" and Gtk_main_quit connection g_signal_connect (window, "Destroy", G_callback (Gtk_main_quit), NULL); Window receive mouse Event//gdk_button_press_mask: Mouse click event//Gdk_button_motion_mask: Press and hold mouse to move event gtk_widget_add_events (window , Gdk_button_press_mask |        Gdk_button_motion_mask); "Button-press-event" with deal_mouse_event connection, mouse click event G_signal_connect (window, "Button-press-event", G_callback (deal_m      ouse_press), NULL); "Motion-notify-event" connection with deal_motion_notify_event, press and hold mouse move event g_signal_connect (window, "Motion-notify-event", G_CAL LbacK (deal_motion_notify_event), NULL);    Gtk_widget_show_all (window);     Show window All controls Gtk_main ();  Main Event loop return 0;   }

Operation Result:




Source code Download: http://download.csdn.net/detail/lianghe_work/8946495

Turn from:

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

GTK Advanced Learning: Mouse events

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.