0

我有一个单选按钮,它是 CDialog 中的 CButton。当用户单击单选按钮时,将OnClickedRadioButton调用该函数。

在里面OnClickedRadioButton我通过调用这个函数来切换按钮:

void toggleButton(CButton& theButton)
{
    switch(theButton.GetCheck())
    {
        case BST_UNCHECKED:
        {
            theButton.SetCheck(BST_CHECKED);
            break;
        }
        case BST_CHECKED:
        {
            theButton.SetCheck(BST_UNCHECKED);
            break;
        }
        default:
        {
            theButton.SetCheck(BST_UNCHECKED);
        }
    }
}

当我编译和运行程序时: (i) 如果选中单选按钮,我可以单击它来清除它。(ii) 如果未选中单选按钮,我单击它并且没有任何反应。但是,如果我单击不同的程序(即 Visual Studio),然后单击 CDialog,则单选按钮会选中。

我已经查看并尝试了函数Cwnd::UpdateDialogControlsCwnd::UpdateData,但我无法让这些来解决我的问题。

4

1 回答 1

0

I believe the problem was related to @rrirower comment that SetCheck will cause another OnClickedRadioButton event.

Regardless of the root cause, the quick fix to allow my implementation to toggle a radio button betweenBST_CHECKED and BST_UNCHECKED was to set the radio button's Auto property to False.

To do this: 1) Open the resource in visual studio 2) Right-click on the radio button and select Properties 3) In the Appearance section, set the Auto property to False.

Here is the overall solution to toggle a single radio button in a subclass of CDialog (assuming you already added a Dialog resource with a radio button with ID IDC_RADIO):

1) Add radio button IDC_RADIO to the message map by placing this line

ON_BN_CLICKED(IDC_RADIO, OnBnClickedRadioButton)

between BEGIN_MESSAGE_MAP and END_MESSAGE_MAP.

2) Add the handler function to your subclass of CDialog

void OnBnClickedRadioButton()
{
    toggleButton(*(CButton*)GetDlgItem(IDC_RADIO));
}

3) Add the toggle function to your subclass of CDialog

void toggleButton(CButton& theButton)
{
    switch(theButton.GetCheck())
    {
        case BST_UNCHECKED:
        {
            theButton.SetCheck(BST_CHECKED);
            break;
        }
        case BST_CHECKED:
        {
            theButton.SetCheck(BST_UNCHECKED);
            break;
        }
        default:
        {
            theButton.SetCheck(BST_UNCHECKED);
        }
    }
}
于 2015-01-13T18:43:01.593 回答