当前位置--> 首 页 --> 文 章 -->Linux Develop

※阅读文章※

GTK+ FAQ--5. 关於gdk


作者:不祥 [文章出自: www.fanqiang.com]

--------------------------------------------------------------------------------

5. 关於gdk
5.1 什麽是gdk? 
gdk基本上是个将标准Xlib函数包装起来的函数库. 如果您对Xlib很熟, gdk的函数您就会很喜欢. 所有函数提供很简单的方式来用Xlib函数, 使其更加直觉. 此外, 既然gdk使用glib, 它将会更加的具有可移植性. 



5.2 要如何使用颜色配置? 
GDK有一点很好就是它是在Xlib的上层; 这倒是造成一点问题, 特别是颜色管理的问题. 如果您想要使用颜色(像是画个四方形), 您的程式看起来要像这样: 

{
  GdkColor *color;
  int width, height;
  GtkWidget *widget;
  GdkGC *gc;

  ...
  
  /* first, create a GC to draw on */
  gc = gdk_gc_new(widget->window);

  /* find proper dimensions for rectangle */
  gdk_window_get_size(widget->window, &width, &height);

  /* the color we want to use */
  color = (GdkColor *)malloc(sizeof(GdkColor));
  
  /* red, green, and blue are passed values, indicating the RGB triple
   * of the color we want to draw. Note that the values of the RGB components
   * within the GdkColor are taken from 0 to 65535, not 0 to 255.
   */
  color->red = red * (65535/255);
  color->green = green * (65535/255);
  color->blue = blue * (65535/255);
  
  /* the pixel value indicates the index in the colormap of the color.
   * it is simply a combination of the RGB values we set earlier
   */
  color->pixel = (gulong)(red*65536 + green*256 + blue);

  /* However, the pixel valule is only truly valid on 24-bit (TrueColor)
   * displays. Therefore, this call is required so that GDK and X can
   * give us the closest color available in the colormap
   */
  gdk_color_alloc(gtk_widget_get_colormap(widget), color);

  /* set the foreground to our color */
  gdk_gc_set_foreground(gc, color);
  
  /* draw the rectangle */
  gdk_draw_rectangle(widget->window, gc, 1, 0, 0, width, height);

  ...
}



--------------------------------------------------------------------------------

文章加入时间: 2004-11-17 14:56:30 责任编辑: w9   (2546 人次查阅)
 
Copyright © 1998-2004 中国PHP联盟 All rights reserved.