3状态复选框Win32运行时

时间:2013-03-13 23:25:01

标签: c++ winapi

当我在运行时将对话框窗口中的BS_AUTO3STATE样式添加到默认样式的复选框时;

this->Style |= BS_AUTO3STATE; // wrapper of Get/SetWindowLongPtr, ignore the specifics

..它变成了一个组合框而不是一个三态复选框。我做错了什么?

我的控制风格是否错误?

1 个答案:

答案 0 :(得分:7)

此问题是由于BS_Xxx值实际上未在标头中定义以用作位标志的事实引起的。相反,它们的值只是线性增加:

#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
// ... and so on

请注意BS_GROUPBOX(这是您得到和不想要的样式)等于0x7。您的控件最终会以该样式标志集结束,因为您设置的标志组合的值为0x7。不幸的是,你不能只将OR标志放在一起并得到你想要的结果。

相反,您必须使用BS_TYPEMASK标志清除当前按钮样式,然后设置所需的单个BS_Xxx标志。对于普通复选框,可能是BS_AUTOCHECKBOX;对于3状态复选框,即BS_AUTO3STATE

工作示例代码:

void ToggleCheckboxCtrl(HWND hwndCheckBox)
{
    // Retrieve the control's current styles.
    LONG_PTR styles = GetWindowLongPtr(hwndCheckBox, GWL_STYLE);

    // Remove any button styles that may be set so they don't interfere
    // (but maintain any general window styles that are also set).
    styles &= ~BS_TYPEMASK;

    // Just for example purposes, we're maintain our last state as a static var.
    // In the real code, you probably have a better way of determining this!
    static bool isRegularCheckBox = true;
    if (isRegularCheckBox)
    {
        // If we're a regular checkbox, toggle us to a 3-state checkbox.
        styles |= BS_AUTO3STATE;
    }
    else
    {
        // Otherwise, we want to go back to being a regular checkbox.
        styles |= BS_AUTOCHECKBOX;
    }
    isSet = !isSet;

    // Update the control's styles.
    // (You'll also need to force a repaint to see your changes.)
    SetWindowLongPtr(hwndCheckBox, GWL_STYLE, styles);
}

Spy ++实用程序(与Visual Studio捆绑在一起)是一个不可或缺的小工具,用于确定切换窗口样式时出现的问题。运行您的应用程序,并使用Spy ++找到窗口并枚举其当前样式。然后更改样式,使用Spy ++转储新样式,看看出了什么问题。

相关问题