如何使用 Win32 设置组框标题的字体和颜色 [英] How do I set the font and color for a group box caption using Win32

查看:42
本文介绍了如何使用 Win32 设置组框标题的字体和颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我坚持使用 WIN32(没有 .NET 或任何管理的东西)

I am stuck with WIN32 ( no .NET or anything managed )

推荐答案

WM_CTLCOLORSTATIC 是控制分组框标题颜色的正确方法.

WM_CTLCOLORSTATIC was the correct way to control the color of the group box title.

但是,它不再有效:如果您的应用程序使用清单来包含第 6 版 comctl 库,则 Groupbox 控件不再向其父级发送 WM_CTLCOLORSTATIC 以获取画笔.如果您的对话框控件看起来丑陋、方形和灰色 - 就像 windows 95 控件一样,那么您没有启用 xp 样式并且您可以控制分组框的颜色.但这是一个可怕的牺牲!:P

However, it no longer works: If your application uses a manifest to include the version 6 comctl library, the Groupbox control no longer sends the WM_CTLCOLORSTATIC to its parent to get a brush. If your dialog controls look ugly, square and grey - like the windows 95 controls, then you don't have xp styles enabled and you can control the color of group boxes. But thats a terrible sacrifice to make! :P

接下来,大多数标准控件将 WM_CTLCOLORxxx 消息发送到它们的父级(对话框)以控制它们的绘画.识别控件的唯一方法是查找它们的控件 ID - 这就是为什么为控件分配一个标识符以指示该控件需要特定颜色或字体是一个好主意.即不要将 IDC_STATIC 用于需要红色文本的控件.将它们设置为 IDC_DRAWRED 或一些编造的 id.

Next, most of the standard controls send WM_CTLCOLORxxx messages to their parent (the dialog) to control their painting. The only way to identify the controls is to look up their control IDs - which is why assigning controls a identifier that indicates that that control needs a specific color or font is a good idea. i.e. Don't use IDC_STATIC for controls that need red text. Set them to IDC_DRAWRED or some made up id.

不要使用 GetDlgItem(hwndDlg,IDC_ID) == hwndCtl 来测试 WM_CTLCOLOR 消息是否用于正确的控件:GetDlgItem 将简单地返回第一个控件的句柄在具有特定 Id 的对话框上,这意味着只会绘制一个控件.

Dont use GetDlgItem(hwndDlg,IDC_ID) == hwndCtl to test if the WM_CTLCOLOR message is for the correct control: GetDlgItem will simply return the handle of the first control on the dialog with the specific Id, which means only one control will be painted.

case WM_CTLCOLORSTATIC:
  if(GetWindowLong( (HWND)lParam, GWL_ID) == IDC_RED)
    return MakeControlRed( (HDC)wParam );

你总是*需要从 WM_CTLCOLORxxx 消息中返回一个 HBRUSH - 即使你真的只想篡改"传入的 HDC.如果你没有从你的对话过程中返回一个有效的画笔,那么对话窗口过程会认为您根本没有处理该消息并将其传递给 DefWindowProc - 这将重置您对 HDC 所做的任何更改.系统没有创建画笔,而是在待机时缓存画笔以绘制标准 ui 元素:GetSysColorBrush

You always* need to return a HBRUSH from a WM_CTLCOLORxxx message - even if you really just want to 'tamper' with the HDC being passed in. If you don't return a valid brush from your dialog proc then the dialogs window procedure will think you didn't handle the message at all and pass it on to DefWindowProc - which will reset any changes to the HDC you made. Instead of creating brushes, the system has a cache of brushes on standby to draw standard ui elements: GetSysColorBrush

当然,您并不总是需要返回 HBRUSH.如果您在应用程序中启用了 xp 主题样式,则有时允许您返回 null :- 因为 xp 主题对话框具有不同颜色的背景(尤其是在选项卡控件上),返回 syscolor 画笔会导致在较亮的背景上出现丑陋的灰色框:- 在这些特定情况下,对话框管理器将允许您返回 null 并且不会在 DC 中重置您的更改.

Of course, you DON'T always need to return an HBRUSH. IF you have the xp theme style enabled in your app, you are sometimes allowed to return null :- because xp theme dialogs have differently colored backgrounds (especially on tab controls) returning a syscolor brush would result in ugly grey boxes on a lighter background :- in those specific cases the dialog manager will allow you to return null and NOT reset your changes in the DC.

这篇关于如何使用 Win32 设置组框标题的字体和颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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