![]() |
|
How to make static controls transparent?
By Alexander Shargin, April 28, 2003.
QuestionI have a dialog with some static controls on it. The dialog displays a bitmap on the background. I want to make my static controls transparent. How can I do it? I tried to handle WM_CTLCOLOR message but this doesn't work. What did I do wrong? AnswerCorrect handler for WM_CTLCOLOR message looks like this: This code works well on desktop Windows but fails on CE. Well actually the static becomes transparent. The problem is that the dialog never draws anything under child controls (neither from OnEraseBkgnd nor from OnPaint) - even if WS_CLIPCHILDREN style is not set! Here is a quote from CreateWindow API doc which describes this behaviour: "All windows implicitly have the WS_CLIPSIBLINGS and WS_CLIPCHILDREN styles." So you will have to use a different approach. The easiest way to create transparent labels is to draw them manually from WM_ERASEBKGND or WM_PAINT handler of your dialog. You can use DrawText to do this. In order to make the text transparent you have to call CDC::SetBkMode and set background mode to TRANSPARENT before you draw your labels. Here is a simple OnPaint handler which draws a transparent label on the dialog. This approach works well but there are some problems with it. First, you can not use resource editor to position your labels on the dialog. Second, localization of your app becomes more difficult since instead of localizing a dialog resource you have to localize string resources for all labels and manually check if new labels fit in the dialog layout. To fix these problems you can do the following. Place static controls in your dialog like you already do but make them invisible (remove Visible flag in the control's properties). Then in OnPaint handler iterate through all static controls, obtain their text and position and draw them as in the previous sample. You can also analyze static's style and specify corresponding DrawText flags to make this approach even more generic. Here is the code which does all this for you. You can download a sample project - TransparentCtl.zip (17 Kb). Related resources:
DiscussDiscuss this article. Here you can write your comments and read comments of other developers. |
|||||||||||||||||||||