樹心幽徑

« 20180611在GTK視窗上建立二個Widget控制項(一個text,一個label或二個label) | Main | 20180612在GTK視窗的Drawing Area顯示影像並偵測滑鼠按鍵位置(gtk9.c) »

20180612在GTK視窗顯示影像並偵測滑鼠按鍵位置(gtk8.c)
2018/06/12,20:51

(1) gtk8.c

treehrt@treehrt-BM6AE-BM1AE-BP1AE:~/mysqlc$ cat gtk8.c
#include <gtk/gtk.h>
static gboolean button_press_callback (GtkWidget      *event_box, GdkEventButton *event, gpointer        data)
{
    g_print ("%f,%f\n", event->x, event->y);
    return TRUE;
}

int main (int argc, char *argv[])
{
  GtkWidget *window;

  gtk_init (&argc, &argv);
  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
 
  GtkWidget *event_box;
  event_box = gtk_event_box_new ();
  gtk_container_add (GTK_CONTAINER (window), event_box);

  GtkWidget * im;
  im = gtk_image_new_from_file("map.png");
  gtk_container_add (GTK_CONTAINER (event_box), im);
  g_signal_connect (G_OBJECT(event_box), "button_press_event", G_CALLBACK (button_press_callback), im);

  gtk_window_set_title(GTK_WINDOW (window), "map");

  gtk_widget_show_all (window);
  gtk_main();
  return 0;
}

 

(2)編譯與執行


treehrt@treehrt-BM6AE-BM1AE-BP1AE:~/mysqlc$ gcc gtk8.c -Wall `pkg-config --cflags --libs gtk+-3.0` -export-dynamic
treehrt@treehrt-BM6AE-BM1AE-BP1AE:~/mysqlc$ ./a.out &
[12] 4470
treehrt@treehrt-BM6AE-BM1AE-BP1AE:~/mysqlc$ 536.113159,156.131699
536.113159,156.131699
536.113159,156.131699
520.113159,137.131699
285.690460,133.012070
285.690460,133.012070
285.690460,133.012070
285.690460,133.012070
285.690460,133.012070

迴響

 
Accessible and Valid XHTML 1.0 Strict and CSS Powered by LifeType