[comp.windows.ms.programmer] Button Colors

woodside@ttidcb.tti.com (George Woodside) (12/13/90)

I've been unable to change the color of a button, created inside a
window. I've followed the example in Petzold's book, and tried various
other attempts, but the child button remains gray. The 'ybrush' handle
is valid, and has been used successfully. "SPY" shows the messages are
being received as expected, and debug logs show all values are as
expected. But, the child buttons remain gray, instead of being painted
yellow. Any help anyone can offer will be appreciated. The unsuccessful
code segment:

extern HBRUSH   ybrush;			/* yellow brush for folder	    */

long FAR PASCAL WndProc(HWND hwnd, unsigned message, WORD wParam, LONG lParam)
{
  POINT           point;		/* origin point for child brush	    */
....
  switch (message)			/* check message type		    */
  {
....
      case WM_CTLCOLOR:			/* child button repainting	    */
      if (HIWORD(lParam) == CTLCOLOR_BTN) /* if a button repaint	    */
      {
	SetBkColor((HDC) wParam, RGB(255, 255, 0)); /* set yellow background */
	SetTextColor((HDC) wParam, R(0, 0, 0)); /* text black	    */
	UnrealizeObject(ybrush);	/* reset brush origin		    */
	point.x = 0;			/* horizontal synch with child	    */
	point.y = 0;			/* vertical synch with child	    */
	ClientToScreen((HDC) wParam, &point); /* synch to client window	    */
	SetBrushOrg((HDC) wParam, point.x, point.y); /* synch the brush	    */
	return ((DWORD) ybrush);	/* paint button in yellow	    */
      }					/* end child button repaint	    */
      break;
....
  }					/* end message switch		    */
}					/* end WndProc			    */

davidds@microsoft.UUCP (David D'SOUZA) (12/27/90)

In article <21843@ttidca.TTI.COM> woodside@ttidcb.tti.com (George Woodside) writes:
>I've been unable to change the color of a button, created inside a
>window. I've followed the example in Petzold's book, and tried various
>other attempts, but the child button remains gray. The 'ybrush' handle
>is valid, and has been used successfully. "SPY" shows the messages are
>being received as expected, and debug logs show all values are as
>expected. But, the child buttons remain gray, instead of being painted
>yellow. Any help anyone can offer will be appreciated. The unsuccessful
>code segment:
>

Sorry, George, this is Windows biting you...  This is probably something
that should be included in the FAQ since it will bite first time windows
programmers since this example seems to appear in a lot of places...

Turns out in Windows 3.0, the button face is defined by two colors. The
grey (white if ega) face and a dark grey (grey if ega) shadow.  The
colors also change when the button goes from a normal to pushed in
state.  The WM_CTLCOLOR message only allows you to change one color at
a time so to which of the button face colors should this apply?
(Windows 2 button faces had only one color so it made sense.)

Maybe something tricky could have been done by using the background
color for the shadow and foreground color for the face and perhaps
doing something strange to get the text color in another way... And how
do you return 2 brushes (you now need a foreground and a background
brush)?  Or maybe even better, make colors a property of the window and
some windows could have multiple color properties...

Anyway, Windows doesn't look at the WM_CTLCOLOR message for buttons.  Try it
with a listbox instead...

--Dave

<Standard disclaimers apply>