- 论坛徽章:
- 0
|
你的颜色分配有问题。比较混乱。有的函数在新的gtk版本中已经不用了。比如gdk_draw_string,应该用PangoLayout来处理文本。使用PangoLayout可以显示出文本。但是乱码问题没有找出来,应该是colomap的问题。
#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>
#include<string.h>
#define WIDTH 100
#define HEIGHT 100
GdkPixmap* newPixmap;
int my_expose_event(GtkWidget *widget , GdkEventExpose *event , gpointer data)
{
GdkFont* newFont;
PangoLayout *pl;
PangoRectangle pr;
pl = gtk_widget_create_pango_layout (GTK_WIDGET (widget), NULL);
GdkPixbuf* pixBuf = NULL;
if(newPixmap)
g_object_unref(newPixmap);
newFont = gdk_font_load("micro");
//newPixmap = gdk_pixmap_new(NULL, WIDTH, HEIGHT, 16);
newPixmap = gdk_pixmap_new(widget->window, WIDTH, HEIGHT, -1);
GdkColormap *colormap;
colormap = gdk_window_get_colormap (widget->window);
GdkGC* gc = gdk_gc_new(newPixmap);
gdk_gc_copy (gc, widget->style->black_gc);
GdkColor white;
white.red =65535;
white.green = 65535;
white.blue= 65535;
gdk_colormap_alloc_color (colormap, &white, FALSE, TRUE);
//gdk_gc_set_background(gc, &white);
gdk_gc_set_foreground(gc, &white);
/*
gdk_draw_rectangle(newPixmap, gc, TRUE,
0, 0, 100, 100);
*/
printf("hehe\n");
pango_layout_set_text (pl, "Hello, Test ok?", strlen ("Hello, Test ok?"));
pango_layout_get_pixel_extents (pl, &pr, NULL);
gdk_draw_layout(newPixmap, gc, 0, 0, pl);
gdk_draw_line(newPixmap, gc, 0, 0, 100, 100);
printf("xixi\n");
// gdk_draw_line(widget->window, gc, 0, 0, 100, 100);
/*
pixBuf = gdk_pixbuf_get_from_drawable(NULL, newPixmap,
gdk_drawable_get_colormap(newPixmap), 0, 0, 0, 0, 100, 100);
if(pixBuf){
printf("pixbuf is ok");
gdk_pixbuf_unref(pixBuf);
}*/
gdk_draw_pixmap(widget->window ,
widget->style->fg_gc[GTK_WIDGET_STATE(widget)] ,
newPixmap , 0 , 0 , 0 , 0,
widget->allocation.width ,
widget->allocation.height);
return 0;
}
int
main(int argc, char** argv){
gtk_init(&argc, &argv);
GtkWidget* window;
GtkWidget* draw_area;
GdkFont* newFont;
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
newFont = gdk_font_load("micro");
/*
gdk_draw_drawable(window->window, gdk_gc_new(newPixmap),
newPixmap, 10, 10, 30, 30, 10, 10);
*/
draw_area = gtk_drawing_area_new();
gtk_widget_set_size_request( draw_area, WIDTH*3, HEIGHT*2 );
gtk_container_add(GTK_CONTAINER(window), draw_area);
/*
printf("Hello \n");
gdk_draw_string(draw_area->window, newFont, draw_area->style->fg_gc[GTK_WIDGET_STATE(draw_area)],
WIDTH, HEIGHT, "Hell World!!");
printf(" World\n");
*/
g_signal_connect(G_OBJECT(draw_area) , "expose_event" ,
G_CALLBACK(my_expose_event) , NULL);
// gtk_widget_show(draw_area);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
因为在工作,没有太多时间来找出这个错误。呵呵,你自己慢慢找一下吧 |
|