读书人

怎样设置默认按钮?该怎么处理

发布时间: 2012-05-20 16:03:12 作者: rapoo

怎样设置默认按钮?
*********************
edit1 edit2
button1
edit3 edit4
button2
*********************
如果焦点在edit1,edit2上,则默认按钮为button1,
如果焦点在edit3,edit4上,则默认按钮为button2.
请问怎么实现?

[解决办法]
BOOL CMyDialog::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN)
{
switch (::GetDlgCtrlID(pMsg->hwnd))
{
case IDC_EDIT1:
case IDC_EDIT2:
PostMessage(WM_COMMAND, MAKEWPARAM(IDC_BUTTON1, BN_CLICKED), (LPARAM)GetDlgItem(IDC_BUTTON1)->m_hWnd);
return TRUE;
case IDC_EDIT3:
case IDC_EDIT4:
PostMessage(WM_COMMAND, MAKEWPARAM(IDC_BUTTON2, BN_CLICKED), (LPARAM)GetDlgItem(IDC_BUTTON2)->m_hWnd);
return TRUE;
}
}

return CDialog::PreTranslateMessage(pMsg);
}

或:

void CMyDialog::OnOK()
{
CWnd *pWnd = GetFocus();
if (pWnd)
{
switch (pWnd->GetDlgCtrlID())
{
case IDC_EDIT1:
case IDC_EDIT2:
PostMessage(WM_COMMAND, MAKEWPARAM(IDC_BUTTON1, BN_CLICKED), (LPARAM)GetDlgItem(IDC_BUTTON1)->m_hWnd);
return;
case IDC_EDIT3:
case IDC_EDIT4:
PostMessage(WM_COMMAND, MAKEWPARAM(IDC_BUTTON2, BN_CLICKED), (LPARAM)GetDlgItem(IDC_BUTTON2)->m_hWnd);
return;
}
}

CDialog::OnOK();
}

读书人网 >VC/MFC

热点推荐