如何动态创建与其父级相同的视觉样式的控件? [英] How do I dynamically create controls with the same visual style as their parent?

查看:189
本文介绍了如何动态创建与其父级相同的视觉样式的控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Visual Studio 2005(和C ++中的win32 API),我有一个使用内置对话框编辑器设计的窗口。

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.

在运行时,我想要能够使用 CreateWindow()向其中添加新的控件。这工作,但是我创建的控件看起来真的很丑陋。为了具体化,我添加的控件是一个TabControl,当我使用内置对话框编辑器添加它,标签中的文本看起来不错。当我在运行时使用 CreateWindow()创建它时,文本是大而粗体,看起来不合适。

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.

我在MSDN上找到使用Windows XP可视样式,这似乎描述东西在正确的区域,但是当我按照其中的说明(嵌入清单),动态创建的控件似乎是一个比对话框编辑器使用的更新的风格(选项卡控件的背景是一个更轻颜色)。

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).

我还发现了 SetWindowTheme()函数。我不太确定如何使用这个函数...我希望我可以在窗口中使用 GetWindowTheme(),然后将结果传递给 SetWindowTheme()使它们看起来一样,但是 GetWindowTheme()返回一个HTHEME,甚至可以用这些...你绝对不能将它们传递给 SetWindowTheme()

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.

推荐答案

你真的需要告诉我们你现在在做什么(代码)如果你想让人们能够帮助。这个答案将是一个正确答案的猜测。所以。

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.

您需要确保向创建的窗口发送 WM_SETFONT 消息。许多标准控件默认为一个真正丑陋的向后兼容的字体,直到你给他们一个新的。在大多数情况下,您可以使用 GetStockObject(DEFAULT_GUI_FONT)(或 GetStockFont()如果包括windowsx.h)as您发送的字体。如果你使用股票字体,那么你不必跟踪它,并释放它以后。

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.

您还需要设置 WS_EX_CLIENTEDGE WS_EX_STATICEDGE 样式,以获得更新的显示行为。我认为当主题打开时,通常 WS_EX_STATICEDGE ,当它们不是时, WS_EX_CLIENTEDGE 。但你需要玩弄这些。使用Spy ++来查看各种控件,看看他们使用什么样式,并确保匹配它们。让这些样式关闭具有禁用主题绘图的副作用。

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.

请注意,这些是 _EX _ 样式标记,因此您将需要使用 CreateWindowEx ,而不是 CreateWindow

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.

这篇关于如何动态创建与其父级相同的视觉样式的控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆