How to dynamically create controls with the same visual style as their parent?

I am using Visual Studio 2005 (and the win32 API in C ++), and I have a window that was developed using the built-in dialog box editor.

At run time, I want to use CreateWindow() to add new controls to it. This works, however the controls that I create look really ugly. For the sake of more specificity, the control I am adding is TabControl, when I add it using the built-in dialog editor, the text in the tab shortcuts looks good. When I create it at runtime with CreateWindow() , the text is large and bold and looks out of place.

I found Using Windows XP Visual Styles on MSDN, which seems to describe the material in the right area, but when I follow the instructions in there (embedding the manifest) the dynamically created control looks newer than the one used by the dialog editor (element background tab controls are much lighter).

I also found the SetWindowTheme() function. I'm not quite sure how to use this function ... I was hoping I could use GetWindowTheme() in a window and then pass the result of this to SetWindowTheme() so that they look the same, however GetWindowTheme() returns an HTHEME, and I have no idea have what you can do with them ... you definitely can't pass them to SetWindowTheme() , though.

+4
source share
1 answer

You really need to show us what you are doing now (code) if you want people to be able to help. This answer will be as much a guess as the correct answer. so.

You probably don't need to guess with a topic descriptor. Just having themes for your application should be sufficient if you properly configure window styles for your controls.

You need to make sure that you send the WM_SETFONT message to the windows you create. Many default controls have a really ugly backward compatible font until you give them a new one. In most cases, you can use GetStockObject(DEFAULT_GUI_FONT) (or GetStockFont() if you enable windowsx.h) as the font you are sending. If you use a stock font, then you do not need to track it and release it later.

You also need to set the WS_EX_CLIENTEDGE or WS_EX_STATICEDGE for most controls to get a newer display behavior. I think that usually WS_EX_STATICEDGE when topics are included, and WS_EX_CLIENTEDGE when they are not. But you will need to play with them. Use Spy ++ to look around the various controls and see what styles they use and make sure you map them. Removing these styles has a side effect of disabling theme drawing.

Note that these are _EX_ style _EX_ , so you will need to use CreateWindowEx , not CreateWindow

There may be other things, but try this and see how far it leads you.

+5
source

All Articles