简体   繁体   中英

How can I make a button in MFC do something different based on what previous button was clicked?

So I have a C++ program in Visual Studio 2012 with an MFC GUI. I did not build the base of this code, I am modifying pre-existing code that included the GUI.

The program starts with all the buttons but 2 grayed out. What I would like to do is link the grayed out buttons to different functions depending on which of the two non grayed out buttons I click. Right now the buttons are linked to functions in a two step process. I'm pretty sure I want to leave the first step alone and modify the second, but here is both in case I am wrong.

Step 1:

void CTCPClientDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_VERSION, m_Version);
    DDX_Control(pDX, IDC_OPTIMIZE, m_Optimize);
    DDX_Control(pDX, IDC_NORMALIZED_SPECTRUM, m_Normalize);
    DDX_Control(pDX, IDC_AQUIRE, m_Acquire);
    DDX_Control(pDX, IDC_DISCONNECT, m_Disconnect);
    DDX_Control(pDX, IDC_ASD_CONNECT, m_Connect1);
    DDX_Control(pDX, IDC_OCEAN_CONNECT, m_Connect2);
}

Step 2:

BEGIN_MESSAGE_MAP(CTCPClientDlg, CDialog)
    ON_WM_SYSCOMMAND()
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_BN_CLICKED(IDC_VERSION, OnVersion)
    ON_BN_CLICKED(IDC_OPTIMIZE, OnOptimize)
    ON_BN_CLICKED(IDC_NORMALIZED_SPECTRUM, OnNormalize)
    ON_BN_CLICKED(IDC_AQUIRE, OnAcquire)
    ON_BN_CLICKED(IDC_DISCONNECT, OnDisconnect)
    ON_BN_CLICKED(IDC_CONNECT_1, OnConnect1)
    ON_BN_CLICKED(IDC_CONNECT_2, OnConnect2)
END_MESSAGE_MAP()

These are one right after the other near the start of the code. Connect1 and Connect2 are the two buttons that start clickable, then I want the other buttons to link to different functions after I click them (so say if I click Connect2 then Version would go to OnVersion2 and such). It seems like if I could just declare the ON_BN_CLICKED for the other buttons inside the OnConnect1 and OnConnect2 functions I would be good, but I'm not really sure how to do that. I might be focusing in the completely wrong spot too, so any help would be greatly appreciated.

Edit: Forgot to mention I know I can just have a variable that gets set to 0 or 1 or whatever and have each function split by an if statement. But I was wondering if I could do without that, it seemed like it would be cleaner to just have two separate functions entirely. I'm already in the process of setting it up with the variable, but would like to change it if possible.

ON_BN_CLICKED is part of a table that is defined at compile time, listing a button ID and a function. That can't be changed at run time. Just do it in code with a boolean variable in the OnConnect1 message handler.

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