http://blog.chinaunix.net/u/27977/showart_213718.html
先用gimp制作一个有透明背景的图片,并存为shape1.xpm格式。
编写代码:
我有点懒,过程以后慢慢再写.下面附有源代码。
编译运行:
$gcc shape1.c -o shape1 `pkg-config --cflags --libs gtk+-2.0`
$./shape1
运行效果图:
源代码:
/* vi: set sw=4 ts=4: */
/****************************************************************************
*
* Copyright (C) 2006 by yetist <[email protected]>.
*
* 文 件 名:shape1.c
* 版 本:0.1
* 创建时间:2006年12月11日 星期一 23时21分26秒
* 描 述:________
*
* Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
*****************************************************************************/
#include <gtk/gtk.h>
typedef struct _cursoroffset {gint x,y;} CursorOffset;
static void
shape_pressed (GtkWidget *widget, GdkEventButton *event)
{
CursorOffset *p;
if (event->type != GDK_BUTTON_PRESS)
return;
p = g_object_get_data (G_OBJECT (widget), "cursor_offset");
p->x = (int) event->x;
p->y = (int) event->y;
gtk_grab_add (widget);
gdk_pointer_grab (widget->window, TRUE,
GDK_BUTTON_RELEASE_MASK |
GDK_BUTTON_MOTION_MASK |
GDK_POINTER_MOTION_HINT_MASK,
NULL, NULL, 0);
}
static void
shape_released (GtkWidget *widget)
{
gtk_grab_remove (widget);
gdk_display_pointer_ungrab (gtk_widget_get_display (widget),
GDK_CURRENT_TIME);
}
static void
shape_motion (GtkWidget *widget,
GdkEventMotion *event)
{
gint xp, yp;
CursorOffset * p;
GdkModifierType mask;
p = g_object_get_data (G_OBJECT (widget), "cursor_offset");
gdk_window_get_pointer (NULL, &xp, &yp, &mask);
gdk_window_move (widget->window, xp- p->x, yp-p->y);
}
int
main (gint argc, gchar** argv)
{
GtkWidget *window;
GtkWidget *pixmap;
GtkWidget *fixed;
GdkGC* gc;
GdkBitmap *gdk_pixmap_mask;
GdkPixmap *gdk_pixmap;
GtkStyle *style;
GdkScreen *screen;
CursorOffset* icon_pos;
gchar *xpm_file = "shape1.xpm";
gtk_init (&argc, &argv);
screen = gdk_screen_get_default();
style = gtk_widget_get_default_style ();
gc = style->black_gc;
window = gtk_window_new (GTK_WINDOW_POPUP);
gtk_window_set_screen (GTK_WINDOW (window), screen);
gtk_window_set_position (GTK_WINDOW(window), GTK_WIN_POS_CENTER);
fixed = gtk_fixed_new ();
gtk_widget_set_size_request (fixed, 440, 120);
gtk_container_add (GTK_CONTAINER (window), fixed);
gtk_widget_show (fixed);
gtk_widget_set_events (window,
gtk_widget_get_events (window) |
GDK_BUTTON_MOTION_MASK |
GDK_POINTER_MOTION_HINT_MASK |
GDK_BUTTON_PRESS_MASK);
gtk_widget_realize (window);
gdk_pixmap = gdk_pixmap_create_from_xpm (window->window, &gdk_pixmap_mask,
&style->bg[GTK_STATE_NORMAL],
xpm_file);
pixmap = gtk_image_new_from_pixmap (gdk_pixmap, gdk_pixmap_mask);
gtk_fixed_put (GTK_FIXED (fixed), pixmap, 0, 0);
gtk_widget_show (pixmap);
gtk_widget_shape_combine_mask (window, gdk_pixmap_mask, 0, 0);
g_object_unref (gdk_pixmap_mask);
g_object_unref (gdk_pixmap);
g_signal_connect (window, "button_press_event",
G_CALLBACK (shape_pressed), NULL);
g_signal_connect (window, "button_release_event",
G_CALLBACK (shape_released), NULL);
g_signal_connect (window, "motion_notify_event",
G_CALLBACK (shape_motion), NULL);
g_signal_connect (window, "delete_event",
G_CALLBACK (gtk_widget_destroyed), NULL);
g_signal_connect (window, "destroy",
G_CALLBACK (gtk_widget_destroyed), NULL);
icon_pos = g_new (CursorOffset, 1);
g_object_set_data (G_OBJECT (window), "cursor_offset", icon_pos);
gtk_widget_show_all (window);
gtk_main();
return 1;
}