简体   繁体   中英

MFC button click to enable and disable a function

Ii have issues in using the button click on the MFC environment. I'mrequired to make a button click to execute a certain function and then unclick it to disable the function. Meaning if I click again the function will become false.

void CTacNavDlg::OnBnClickedBtnInfo()
{   
   oglFunction.enablePopup = true;
}

Is there a way to this on MFC?

This functionality is already built into the OS. You only need to change the button to a checkbox, and add the BS_PUSHLIKE button style. This is easiest done by editing the resource script. Assuming that your dialog resource is defined something like this:

IDD_PUSHLIKEBUTTON_DIALOG DIALOGEX ...
...
BEGIN
    PUSHBUTTON      "Up",IDC_PUSHLIKE_BTN_1,7,7,50,14
    PUSHBUTTON      "Up",IDC_PUSHLIKE_BTN_2,79,7,50,14
END

you need to change it to read this instead:

IDD_PUSHLIKEBUTTON_DIALOG DIALOGEX ...
...
BEGIN
    AUTOCHECKBOX      "Up",IDC_PUSHLIKE_BTN_1,7,7,50,14,BS_PUSHLIKE
    AUTOCHECKBOX      "Up",IDC_PUSHLIKE_BTN_2,79,7,50,14,BS_PUSHLIKE
END

The checkbox sends a WM_COMMAND message to its parent, whenever it gets clicked. The MFC way to respond to messages is to add an entry into the message map of the parent. For simplicity, I'm going to bind a range to a single handler:

BEGIN_MESSAGE_MAP(..., CDialogEx)
    ...
    ON_COMMAND_RANGE(IDC_PUSHLIKE_BTN_1, IDC_PUSHLIKE_BTN_2, &OnPushlikeBtn)
END_MESSAGE_MAP()

The following message handler implementation responds by toggling the button text. It shows how to determine the current state as well:

void CPushLikeButtonDlg::OnPushlikeBtn(UINT nID)
{
    auto const btn_handle { ::GetDlgItem(m_hWnd, nID) };
    auto const is_checked { Button_GetCheck(btn_handle) == BST_CHECKED };
    ::SetWindowTextW(btn_handle, is_checked ? L"Down" : L"Up");
}

This produces output that looks similar to this:

示例用户界面

The result is a checkbox with the appearance of a pushbutton. That used to be more common before the introduction of visual styles. Today, you rarely see this, because it makes for a confusing user experience. The flat user interfaces removes the 3D effect, making it near impossible to visually decide between the pushed and hover state.

void CTacNavDlg::OnBnClickedBtnInfo()
{   
    oglFunction.enablePopup = ! oglFunction.enablePopup;
}  
  • First click => oglFunction.enablePopup = true
  • Second click => oglFunction.enablePopup = false
  • Third click => oglFunction.enablePopup = true
  • ....

This is an extension to the answer provided by IInspectable .

It is quite easy to use the Resource Editor to create these controls.

  • Step 1 — Add a Check Box control to your dialog:

添加复选框

  • Step 2 — Set the Push Like property to True :

设置 Push Like 属性

  • Step 3 — Resize your control:

对话框上调整大小的控件

You can also set Radio controls as Push Like too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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