hbrBackground 中的颜色窗口

Nav*_*iel 8 c++ windows

WNDCLASS wc;
...
wc.hbrBackground = (HBRUSH)GetStockObject(COLOR_WINDOW+1);
Run Code Online (Sandbox Code Playgroud)

我无法理解什么是+1为了什么是HBRUSH什么?

jam*_*345 6

你不是说吗?

wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
Run Code Online (Sandbox Code Playgroud)

GetStockObject获取常用画笔、钢笔、字体或调色板之一。您不应该将 COLOR_WINDOW 与它一起使用。

使用其中一个库存画笔,因此对于白色背景,您可以使用...

wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
Run Code Online (Sandbox Code Playgroud)

无论什么工作,使用它!

  • 交易是,`COLOR_SCROLLBAR`,第一个可能的值,其值为0。因此,顺便说一下,对画笔(`NULL`)执行空句柄。因此,必须将 1 添加到每个枚举颜色值以支持空画笔句柄。如果你明白为什么,这是完全有道理的,但乍一看有点奇怪。 (2认同)

Abh*_*eet 4

系统颜色定义于Winuser.h

#define CTLCOLOR_MSGBOX 0

#define CTLCOLOR_EDIT 1

#define CTLCOLOR_LISTBOX 2

#define CTLCOLOR_BTN 3

#define CTLCOLOR_DLG 4

#define CTLCOLOR_SCROLLBAR 5

#define CTLCOLOR_STATIC 6

#define CTLCOLOR_MAX 7

#define COLOR_SCROLLBAR 0

#define COLOR_BACKGROUND 1

#define COLOR_ACTIVECAPTION 2

#define COLOR_INACTIVECAPTION 3

#define COLOR_MENU 4

#define COLOR_WINDOW 5

#define COLOR_WINDOWFRAME 6

#define COLOR_MENUTEXT 7

#define COLOR_WINDOWTEXT 8

#define COLOR_CAPTIONTEXT 9

#define COLOR_ACTIVEBORDER 10

#define COLOR_INACTIVEBORDER 11

#define COLOR_APPWORKSPACE 12

#define COLOR_HIGHLIGHT 13

#define COLOR_HIGHLIGHTTEXT 14

#define COLOR_BTNFACE 15

#define COLOR_BTNSHADOW 16

#define COLOR_GRAYTEXT 17

#define COLOR_BTNTEXT 18

#define COLOR_INACTIVECAPTIONTEXT 19

#define COLOR_BTNHIGHLIGHT 20

#if(WINVER >= 0x0400)

#define COLOR_3DDKSHADOW 21

#define COLOR_3DLIGHT 22

#define COLOR_INFOTEXT 23

#define COLOR_INFOBK 24

#endif /* WINVER >= 0x0400 */

#if(WINVER >= 0x0500)

#define COLOR_HOTLIGHT 26

#define COLOR_GRADIENTACTIVECAPTION 27

#define COLOR_GRADIENTINACTIVECAPTION 28

#if(WINVER >= 0x0501)

#define COLOR_MENUHILIGHT 29

#define COLOR_MENUBAR 30

#endif /* WINVER >= 0x0501 */

#endif /* WINVER >= 0x0500 */

#if(WINVER >= 0x0400)

#define COLOR_DESKTOP COLOR_BACKGROUND

#define COLOR_3DFACE COLOR_BTNFACE

#define COLOR_3DSHADOW COLOR_BTNSHADOW

#define COLOR_3DHIGHLIGHT COLOR_BTNHIGHLIGHT

#define COLOR_3DHILIGHT COLOR_BTNHIGHLIGHT

#define COLOR_BTNHILIGHT COLOR_BTNHIGHLIGHT

#endif /* WINVER >= 0x0400 */
Run Code Online (Sandbox Code Playgroud)

因此,正如 @CodyGray 所定义的,添加 1 是为了区分 COLOR_SCROLLBAR 和 NULL HBRUSH。并且,(引用 @CodyGray 的评论并纠正我的解决方案)“HBRUSH 是 C 风格的演员,您应该使用static_cast<HBRUSH>而不是 C 风格的演员。”

  • HBRUSH *不是* 操作员。这是一个C风格的演员阵容。您链接到的文档适用于 MFC CBrush 类,该类确实提供了到 HBRUSH 的隐式转换运算符。但这与这里的问题(他没有使用 MFC)或提供的示例代码无关。事实上,在 C++ 中,他*应该*使用 `static_cast&lt;HBRUSH&gt;` 而不是 C 风格的强制转换。 (2认同)
  • `static_cast&lt;HBRUSH&gt;` 不起作用,因为 HBRUSH 被定义为 `typedef HBRUSH__ *HBRUSH`,因此需要 `reinterpret_cast` (2认同)