分类: Python/Ruby
2022-12-09 17:56:14
#include
#include
#include
void gtk_drawBap(GdkDrawable* drawable,gint w,gint h,gint x,gint y,const guchar *buff)
{
gint x0=x;
guchar temp;
gint i,j;
/*创建gc*/
GdkGC* gc_white = gdk_gc_new(drawable);
/*设置颜色*/
GdkColor color;
color.red = 0xFFFF;
color.green =0xFFFF;
color.blue = 0xFFFF;
gdk_gc_set_rgb_fg_color (gc_white, &color);
GdkGC* gc_black = gdk_gc_new(drawable);
color.red = 0;
color.green =0;
color.blue = 0;
gdk_gc_set_rgb_fg_color (gc_black, &color);
for(i=0;i
{
temp=buff[i];
for(j=0;j<8;j++)
{
if(temp&0x80)
{
gdk_draw_point(drawable,gc_black,x0,y);
}
x0++;
temp<<=1;
}
if(x0-x==w)
{
x0=x;
y++;
}
}
}
/*信号处理函数*/
int draw_image(GtkWidget *widget,GdkEvent *event,gpointer data)
{
/*将widget转换为drawable 指针*/
GdkDrawable* drawable = GDK_DRAWABLE(widget->window);
gtk_drawBap(drawable,200,137,100,170,buff);
return FALSE;
}
void change_background(GtkWidget *widget, int w, int h, const gchar *path)
{
//1.允许窗口可以绘图
gtk_widget_set_app_paintable(widget, TRUE);
gtk_widget_realize(widget);
/* 更改背景图时,外汇跟单gendan5.com图片会重叠
* 这时要手动调用下面的函数,让窗口绘图区域失效,产生窗口重绘制事件(即 expose 事件)。
*/
gtk_widget_queue_draw(widget);
GdkPixbuf *src = gdk_pixbuf_new_from_file(path, NULL);
GdkPixbuf *dst = gdk_pixbuf_scale_simple(src,w,h,GDK_INTERP_BILINEAR);
/* 创建pixmap图像;
* NULL:不需要蒙版;
* 123: 0~255,透明到不透明
*/
GdkPixmap *pixmap = NULL;
gdk_pixbuf_render_pixmap_and_mask(dst,&pixmap,NULL,0);
// 通过pixmap给widget设置一张背景图,{BANNED}最佳后一个参数必须为: FASL
gdk_window_set_back_pixmap(widget->window,pixmap,FALSE);
g_object_unref(src);
g_object_unref(dst);
g_object_unref(pixmap);
return;
}
int main(int argc,char *argv[])
{
GtkWidget *window;
GtkWidget *drawarea;
GtkWidget *hbox;
GtkWidget *label;
/*gtk初始化*/
gtk_init(&argc,&argv);
/*创建窗口*/
window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window),"卡塔尔世界杯");
/*固定窗口大小*/
gtk_window_set_resizable (GTK_WINDOW(window),FALSE);
/*设置窗口大小*/
gtk_widget_set_size_request(window,800,480);
gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER_ALWAYS);//居中显示
/*连接信号*/
g_signal_connect(G_OBJECT(window),"destroy",G_CALLBACK(gtk_main_quit),NULL);
/*创建横向盒*/
hbox=gtk_hbox_new(FALSE,0);
gtk_container_add(GTK_CONTAINER(window),hbox);
gtk_widget_show(hbox);
/*创建绘图区域*/
gtk_widget_push_visual(gdk_rgb_get_visual());
gtk_widget_push_colormap(gdk_rgb_get_cmap());
drawarea=gtk_drawing_area_new();
gtk_widget_pop_visual();
gtk_widget_pop_colormap();
gtk_container_add(GTK_CONTAINER(hbox),drawarea);
/*显示绘图区域*/
gtk_widget_show(drawarea);
/*连接信号*/
g_signal_connect(G_OBJECT(drawarea),"expose_event",G_CALLBACK(draw_image),NULL);
gtk_widget_set_size_request(GTK_WIDGET(drawarea),400,480);
/*创建标签*/
label=gtk_label_new("姓名:");
gtk_box_pack_start(GTK_BOX(hbox),label,TRUE,FALSE,0);
gtk_label_set_markup(GTK_LABEL(label),"2022卡特尔世界杯\n为世界杯喝彩!");
gtk_widget_show(label);
gtk_widget_show_all(window);
gtk_main();
}