简体   繁体   中英

C++/MFC is there a way to click a button while clicking another one?

I have 2 buttons in my projects :

-Button A

-Button B

Is there a way i can push the buttonA and perform the click of ButtonB aswell.

Call the callback for ButtonB explicitly from ButtonA click callback.


BOOL MyDialog::OnButton_A_Clicked()
{
  ...
  OnButton_B_Clicked();
}

As mentioned in this comment , calling a handler directly may cause subtle problems, I would propose the following solution from the same comment:

Send button click message to the button B.

BOOL MyDialog::OnButton_A_Clicked()
{
  ...
  CWnd *pBtnB = GetDlgItem(IDC_BUTTONB);
  ASSERT(pBtnB != NULL);  // You can use MFC 
  pBtnB->SendMessage(BN_CLICKED);
}

Although the answer was accepted, I would like to offer another solution.

First, the proposed linking of buttons will break as soon as you decide that clicking on ButtonB should cause click on ButtonA too.

I would separate all that clicking from the underlying functionality, and invoke it as needed:

void MyDialog::Button_A_Action()
{
}

void MyDialog::Button_B_Action()
{
}

void MyDialog::OnButton_A_Clicked()
{
  Button_A_Action();
  Button_B_Action();
}

void MyDialog::OnButton_B_Clicked()
{
  Button_B_Action();
}

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