[Gtk-sharp-list] Changing widget colors

Miguel de Icaza miguel@ximian.com
12 Apr 2003 14:34:30 -0400


Hello,

> I've got a small program which tries to change the background and foreground colors of its widgets but doesn't work.  Attached is the code(small).  Any help is appreciated.

A GdkColor needs to be allocated on the colormap before it can be used. 
The documentation covers this.  Let me paste some of it:

      <para>
	The Gdk.Color structure is used to describe an allocated or
	unallocated color.  Unallocated colors only have the red,
	green and blue ushort values initialized.  Colors are
	allocated using the <see cref="M:Gdk.Colormap.AllocColor(Gdk.Color,bool,bool)" />
	method.  After a color is allocated the value in the <see cref="F:Gdk.Color.pixel" /> field is valid.
      </para>
      <example>
        <code lang="C#">
	  DrawRedLine (Gdk.Drawable drawable)
	  {
	  Gdk.GC gc = new Gdk.GC (drawable);
	  
	  Gdk.Color red_color = new Gdk.Color (0xff, 0, 0);
	  
	  // Use the system colormap, easy.
	  Gdk.Colormap colormap = Gdk.Colormap.System;
	  
	  colormap.AllocColor (red_color, true, true);

          gc.Foreground = red_color;
  
	  // Now you can use it
	  drawable.DrawLine (gc, 0, 0, 100, 100);
	  }
	</code>
      </example>