开发者

Get ID of Button for Switch Statement: MFC

Language: Visual C++, MFC

Environment: Visual Studio 2005

I have a dialog that requires the user to set file paths for six different settings. Each text box has a browse button which launches a file browser.

How can I set it up so that all of the browse buttons all call the same function to launch the chooser, then use a switch to determine which button invoked the file chooser so that I can set the text of the appropriate CEdit box with the path? // run-on sentence, hah

I'm sure I'll have to use GetDlgCtrlID, I'm just not sure how.

Thank you for your help in advance!

~ Jon

EDIT: Here's the code I have now...very simple because I'm just getting it to work for now.

BEGIN_MESSAGE_MAP(FSC_3DPersp, CSAPrefsSubDlg)
//{{AFX_MSG_MAP(FSC_3DPersp)
    // NOTE: the ClassWizard will add message map macros here
//}}AFX_MSG_MAP
ON_COMMAND(BN_CLICKED, &FSC_3DPersp::Browse)
 END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////

// FSC_3DPersp message handlers

void FSC_3DPersp::Browse(UINT uiID)
{
// TODO: Add your control notification handler code here

switch(uiID)
{
case IDC_BUTTON1: 
    MessageBox("1");
    break;
case IDC_BUTTON2:
    MessageBox("2");
    break;
case IDC_BUTTON3:
    MessageBox("3");
    break;
case IDC_BUTTON4:
    MessageBox("4");
    break;
case IDC_BUTTON5:
    MessageBox("5");
    break;
case IDC_BUTTON6:
    MessageBox("6");
    break;
case IDC_BUTTON7:
    MessageBox("7");
    break;
}
}

BOOL FSC_3DPersp::OnCommand(WPARAM wParam, LPARAM lParam)
{
if (HIWORD(wParam) == BN_CLICKED)
{
    Brow开发者_StackOverflow社区se(LOWORD(wParam));
    return TRUE;
}
return CWnd::OnCommand(wParam, lParam);
}


If you're responding to the BN_CLICKED message, the button ID will be contained in the LOWORD of the wparam of the message.

Edit: MFC normally discards the wparam of the message. To access it you must override the OnCommand handler in your dialog.

BOOL CMyDialog::OnCommand(WPARAM wParam, LPARAM lParam)
{
    if (HIWORD(wParam) == BN_CLICKED)
    {
        Browse(LOWORD(wParam));
        return TRUE;
    }
    return CWnd::OnCommand(wParam, lParam);
}


ON_COMMAND expects a function that has no arguments. For your Browse method you should use ON_CONTROL_RANGE macro:

ON_CONTROL_RANGE(BN_CLICKED, IDC_BUTTON1, IDC_BUTTON7, Browse)

You should make sure IDC_BUTTON1 to IDC_BUTTON7 have consecutive numerical values.

You can read this article for more info http://msdn.microsoft.com/en-us/library/84xtde24.aspx.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜