简体   繁体   English

MFC切换单选按钮被延迟

[英]MFC Toggle Radio Button Is Delayed

I have a radio button that is a CButton in a CDialog. 我有一个单选按钮,它是CDialog中的CButton。 When the user clicks the radio button, the function OnClickedRadioButton is called. 用户单击单选按钮时,将调用函数OnClickedRadioButton

Inside OnClickedRadioButton I toggle the button by calling this function: 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);
        }
    }
}

When I compile & run the program: (i) if the radio button is checked on, I can click it to clear it. 当我编译并运行程序时:(i)如果选中了单选按钮,则可以单击将其清除。 (ii) if the radio button is unchecked, I click it and nothing happens. (ii)如果未选中该单选按钮,则单击它不会发生任何反应。 But if I click on a different program (ie visual studio) and then click back on the CDialog, the radio button checks on. 但是,如果我单击其他程序(即Visual Studio),然后再单击CDialog,则单选按钮会选中。

I've looked & tried functions Cwnd::UpdateDialogControls and Cwnd::UpdateData , but I was not able to get these to solve my problem. 我已经看过并尝试过函数Cwnd::UpdateDialogControlsCwnd::UpdateData ,但是我无法获得这些函数来解决我的问题。

I believe the problem was related to @rrirower comment that SetCheck will cause another OnClickedRadioButton event. 我相信问题与@rrirower注释有关,即SetCheck将导致另一个OnClickedRadioButton事件。

Regardless of the root cause, the quick fix to allow my implementation to toggle a radio button between BST_CHECKED and BST_UNCHECKED was to set the radio button's Auto property to False. 不管根本原因是什么,允许我的实现在BST_CHECKEDBST_UNCHECKED之间切换单选按钮的快速解决BST_UNCHECKED是将单选按钮的Auto属性设置为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. 为此,请执行以下操作:1)在Visual Studio中打开资源2)右键单击单选按钮,然后选择“属性”。3)在“外观”部分中,将“自动”属性设置为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): 这是在CDialog的子类中切换单个单选按钮的整体解决方案(假设您已经添加了带有ID IDC_RADIO单选按钮的Dialog资源):

1) Add radio button IDC_RADIO to the message map by placing this line 1)通过放置此行,将单选按钮IDC_RADIO添加到消息映射中

ON_BN_CLICKED(IDC_RADIO, OnBnClickedRadioButton)

between BEGIN_MESSAGE_MAP and END_MESSAGE_MAP. 在BEGIN_MESSAGE_MAP和END_MESSAGE_MAP之间。

2) Add the handler function to your subclass of CDialog 2)将处理函数添加到CDialog的子类中

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

3) Add the toggle function to your subclass of CDialog 3)将toggle函数添加到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);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM