R – How to change the background of a STATIC win32 control

cuser interfacewinapi

If the parent form of a (text) static control has a pattern on its background, then the area around the static control is an ugly blotch of solid color. How can I paint the background of the static control with the same pattern that its parent window uses?

I've tried this,

SetClassLong(retval , GCL_HBRBACKGROUND, (LONG)stripes);

where retval was an HWND, pointing to the static control I just created,
and stripes is an HBRUSH created from a bitmap. I tried with this, too:

SetClassLongPtr(retval , GCLP_HBRBACKGROUND, (LONG)stripes);

Neither of them worked. Does anyone know how to change the background of a static control in C?

Update

I handled the WM_CTLCOLORSTATIC message, which worked to an extent–it filled up the empty space in all the labels with the pattern I wanted. But the color right behind the text was just white… How can I make the pattern fill up that space as well?

Nevermind, got it.
SetBkMode(hdc, TRANSPARENT);

Best Answer

You can set the background color for a static control by handling the WM_CTLCOLOR message.

From the documentation

If an application processes this message, it returns a handle to a brush. The system uses the brush to paint the background of the control.

The message also passes a pointer to the display context that you can use.