How do I dynamically create controls with the same

2019-05-05 16:48发布

问题:

I'm using Visual Studio 2005 (and win32 API in C++), and I have a window which was designed using the built-in dialog box editor.

At runtime, I want to be able to use CreateWindow() to add new controls to it. This works, however the controls I create all look really ugly. For the purpose of concreteness, the control I'm adding is a TabControl, when I add it using the built-in dialog box editor, the text in the tab labels looks nice. When I create it at runtime with CreateWindow(), the text is big and bold and looks out of place.

I found Using Windows XP Visual Styles on MSDN, which seems to describe stuff in the right area, but when I follow the instructions in there (embedding a manifest), the dynamically created control seems to be a newer style than the one used by the dialog box editor (the background of the tab control is a much lighter colour).

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

回答1:

You really need to show us what you are currently doing (the code) if you want people to be able to help. This answer is going to be as much guesswork as a proper answer. so.

You probably don't need to muck around with the theme handle, Just having themes enabled for your app should be sufficient so long as you set the window styles for your controls correctly.

You need to make sure that you send a WM_SETFONT message to the windows you create. Lots of standard controls default to a really ugly backward compatible font until you give them a new one. In most cases you can probably use GetStockObject(DEFAULT_GUI_FONT) (or GetStockFont() if you include windowsx.h) as the font you send them. If you use a stock font then you don't have to keep track if it and free it later.

You also need to set the WS_EX_CLIENTEDGE or WS_EX_STATICEDGE style for most controls to get the newer display behavior. I think it's usually WS_EX_STATICEDGE when themes are turned on and WS_EX_CLIENTEDGE when they aren't. But you will need to play around with these. Use Spy++ to look around at various controls and see what styles they use and make sure that you match them. Leaving these styles off has the side effect of disabling theme drawing.

Note that these are _EX_ style flags, so you will need to use CreateWindowEx rather than CreateWindow

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