GTK-drag any button in the window

Source: Internet
Author: User

1. Drag any button in the window

Source code:

# Include <GTK/GTK. h> <br/> gtkwidget * window; <br/> gtkwidget * fixed; <br/> gtkwidget * button; <br/> gboolean drag = false; // drag the form only when the left button is pressed </P> <p> Gint Nx = 0; <br/> Gint ny = 0; </P> <p> void destroy (gtkwidget * widget, gpointer * Data) <br/>{< br/> gtk_main_quit (); <br/>}</P> <p> static Gint button_press_event (gtkwidget * widget, gdkeventbutton * event, gpointer data) <br/>{< br/> If (Event-> button = 1) // determine whether to press the left button <br/>{< br/> drag = true; <br/> Nx = event-> X; // obtain the cursor position relative to the window. <br/> ny = event-> Y; <br/>}</P> <p> return true; </P> <p >}< br/> static Gint button_release_event (gtkwidget * widget, gdkeventbutton * event, gpointer data) // The Mouse up event <br/>{</P> <p> If (Event-> button = 1) // press the left mouse button for the second time <br/> drag = false; </P> <p> // gtk_fixed_move (gtk_fixed (fixed), button, event-> X, event-> Y);/* move the button */<br/> return true; </P> <p >}</P> <p> static Gint motion_policy_event (gtkwidget * widget, gdkeventbutton * event, gpointer data) // move the cursor over the event <br/>{</P> <p> If (drag) <br/>{< br/> // Gint X, Y; <br/> // gtkwidget * window = (gtkwidget *) data; <br/> // gtk_window_get_position (gtkwindow *) window, & X, & Y ); // obtain the absolute coordinates of the form<Br/> gtk_fixed_move (gtk_fixed (fixed), button, widget-> allocation. X + Event-> X-NX, widget-> allocation. X + Event-> Y-NY);/* move the button */<br/> // g_print ("x = % d; y = % d \ n ", X + Event-> X-NX, Y + Event-> Y-NY); <br/> // gtk_fixed_move (gtk_fixed (fixed), button, event-> X, event-> Y);/* move the button */<br/> // gtk_window_move (gtkwindow *) window, x + Event-> X-NX, Y + Event-> Y-NY); // move the form <br/>}</P> <p> return true; </P> <p >}</P> <p> int main (INT argc, char * argv []) <br/>{</P> <p> gtk_init (& argc, & argv); // initialization <br/> window = gtk_window_new (gtk_window_toplevel); <br/> gtk_signal_connect (gtk_object (window), "Destroy", (gtksignalfunc) destroy, null); <br/> gtk_window_set_title (gtk_window (window), "button_drag"); <br/> // gtk_window_fullscreen (gtk_window )); </P> <p> fixed = gtk_fixed_new (); // function used to create a new fixed container <br/> gtk_con Tainer_add (gtk_container (window), fixed); </P> <p> button = gtk_button_new_with_label ("button "); /* Create a button with a label */<br/> // gtk_container_add (gtk_container (window), button ); /* Add the button to the form */<br/> gtk_fixed_put (gtk_fixed (fixed), button, 50, 50 ); // The function places the widget in the position specified by x and y in fixed. <Br/> gtk_widget_set_size_request (button, 80, 35); </P> <p> // set the button to get the mouse event <br/> gtk_widget_set_events (button, response | response | gdk_button_press_mask | response); <br/> gtk_signal_connect (gtk_object (button), "button_press_event", (gtksignalfunc) button_press_event, window ); // Add Event Callback <br/> gtk_signal_connect (gtk_object (button), "button_release_event", (gtksignalfunc) button_release_event, window); <br/> gtk_signal_connect (gtk_object (button ), "motion_policy_event", (gtksignalfunc) motion_policy_event, window); </P> <p> gtk_widget_show_all (window); <br/> gtk_main (); <br/> return 0; <br/>}

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: 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.