4

I am trying to implement a static control which refreshes(change text) in response to some event, which occurs once every second. Since I didn't want to paint the entire client area every second and so I decided to use a static control, now the problem is the parent window is skinned, meaning it has custom bitmap as its background, and the static control doesn't fit in, so am looking for ways to make the static control's background transparent.

This is what I have now:

     hHandle = CreateWindowEx( WS_EX_TRANSPARENT, "STATIC", "", WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS, 60, 212, 477, 20, hwnd, 0, hInstance, 0) ;


     case WM_CTLCOLORSTATIC: 
          {
              HDC hdC = (HDC)wParam;

              SetTextColor( hdC, RGB(31,122,179) );

              SetBkMode( hdC, TRANSPARENT );


              return 0;//(HRESULT)GetStockObject(NULL_BRUSH);
          }
          break;

I tried returning NULL brush to paint the background, hoping it would make its background transparent but it didn't what more it forced the static control to not repaint properly, what I mean is that the text gets painted on top of the old text so its all messy.

Is subclassing is the only option ?

3 Answers 3

4

The way to get a WC_STATIC control to show only text in the color you choose (over an image or other surface) is the return, as I understand this. This is what worked for me from this link.

case WM_CTLCOLORSTATIC:
   SetTextColor((HDC)wParam, RGB(255, 0, 0));
   SetBkMode((HDC)wParam, TRANSPARENT);

   // the correct return needs HOLLOW_BRUSH
   return (LRESULT)GetStockObject(HOLLOW_BRUSH);
break;
1
3

The only way I've found to do this reliably is to sub-class the static control and paint the background manually.

WS_EX_TRANSPARENT does not make a control truly transparent to underlying pixels (although it may appear like that) and WS_EX_COMPOSITED can not be used for child windows.

Instead, sub-class the static, and catch the WM_ERASEBKGND message. You can then paint the appropriate portion of the underlying bitmap.

0
0

You don't mention the window styles on the static control, specifically the extended styles WS_EX_TRANSPARENT seems intended to solve the problem you describe with the repaint. Also as I understand it WS_EX_COMPOSITED might be useful in your context.

You also might consider whether the background window should have WS_CLIPCHILDREN set as that might affect the repaint.

1
  • It seems to work on only to the portion of the background of the static control under the new text, if the new text length is smaller then the previous one the remaining text of the previous paint which is not under the new text stays there, it doesn't go away(sorry for the english :P)
    – StudentX
    Dec 6, 2012 at 17:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.