GetWindowlong to check button style
I would like to use raw winapi32 to check button style whether it is a checkbox button or a pushbutton.
boo开发者_运维百科l isPushBtn(HWND hBtn, DWORD dwStyle)
{
return (0!=dwStyle | GetWindowLong(hBtn,GWL_STYLE));
}
But this always returns false. Do you know a way to check this ? Thank you.
In order to understand how button styles work we need to look at the values used by the style constants:
#define BS_PUSHBUTTON 0x00000000L
#define BS_DEFPUSHBUTTON 0x00000001L
#define BS_CHECKBOX 0x00000002L
#define BS_AUTOCHECKBOX 0x00000003L
#define BS_RADIOBUTTON 0x00000004L
#define BS_3STATE 0x00000005L
#define BS_AUTO3STATE 0x00000006L
#define BS_GROUPBOX 0x00000007L
#define BS_USERBUTTON 0x00000008L
#define BS_AUTORADIOBUTTON 0x00000009L
#define BS_PUSHBOX 0x0000000AL
#define BS_OWNERDRAW 0x0000000BL
#define BS_TYPEMASK 0x0000000FL
#define BS_LEFTTEXT 0x00000020L
#define BS_TEXT 0x00000000L
#define BS_ICON 0x00000040L
#define BS_BITMAP 0x00000080L
#define BS_LEFT 0x00000100L
#define BS_RIGHT 0x00000200L
#define BS_CENTER 0x00000300L
#define BS_TOP 0x00000400L
#define BS_BOTTOM 0x00000800L
#define BS_VCENTER 0x00000C00L
#define BS_PUSHLIKE 0x00001000L
#define BS_MULTILINE 0x00002000L
#define BS_NOTIFY 0x00004000L
#define BS_FLAT 0x00008000L
#define BS_RIGHTBUTTON BS_LEFTTEXT
The other essential reference is the Button Styles topic at MSDN. However, what that document does not explain is that the BS_PUSHBUTTON
to BS_OWNERDRAW
flags, the type flags, are mutually exclusive. The other flags can be used in combination with one of the type flags. This can be inferred from the bit patterns of the values.
The documentation for BS_TYPEMASK
states:
Windows 2000: A composite style bit that results from using the OR operator on BS_* style bits. It can be used to mask out valid BS_* bits from a given bitmask. Note that this is out of date and does not correctly include all valid styles. Thus, you should not use this style.
However I think this is misleading and endorse what ybungalobill said in his answer. No harm can come of following that advice.
In other words you should mask the style with BS_TYPEMASK
and then test for a particular button type.
bool isButtonType(HWND hBtn, DWORD dwType)
{
assert(dwType<=BS_TYPEMASK);
return (GetWindowLong(hBtn, GWL_STYLE) & BS_TYPEMASK) == dwType;
}
It should be:
return (GetWindowLong(hBtn,GWL_STYLE) & BS_TYPEMASK) == dwStyle;
It should be
return (dwStyle == (GetWindowLongPtr(hBtn, GWL_STYLE) & dwStyle));
精彩评论