兩種方法實(shí)現(xiàn)回車響應(yīng)tab鍵:
第一種比較簡(jiǎn)單:
只要在你的對(duì)話框類C**Dlg中右擊添加虛函數(shù)PreTranslateMessage然后加如下代碼即可:
BOOL CTest2Dlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if(pMsg->message==WM_KEYDOWN)
{
if(pMsg->wParam==VK_RETURN)
{
pMsg->wParam=VK_TAB;
}
}
return CDialog::PreTranslateMessage(pMsg);
}
第二種方法好一點(diǎn),但實(shí)現(xiàn)起來也比較難一點(diǎn):
1.新建一個(gè)基于對(duì)話框的工程test3;我把"確定"和"取消"都刪除了!
2.在對(duì)話框中插入如圖的控件(其中Button1的ID是IDOK)

3.右擊工程添加一個(gè)MFC類,類名為CMyEdit
4.然后右擊這個(gè)新類添加WM_KEYDOWN消息函數(shù)OnKeyDown;并加如下代碼:
void CMyEdit::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
if(nChar==VK_RETURN)
{
CTest3Dlg * pwnd=(CTest3Dlg *)GetParent();//為個(gè)是主對(duì)話框類,所以在CPP文件中加#include "test3Dlg.h"
//不要在頭文件中加,在頭文件中會(huì)出錯(cuò)的!
pwnd->NextDlgCtrl();
}
CEdit::OnKeyDown(nChar, nRepCnt, nFlags);
}
5.然后將主對(duì)話框中的兩個(gè)編輯框定義關(guān)聯(lián)變量:
在classwizard中選擇 member variables 選項(xiàng),class name選擇對(duì)話框類,control ids中選擇編輯框的id號(hào),然后選擇add variable按扭。在對(duì)話框中 categary選擇control, variable type 剛才定義的類m_myedit1,m_myedit2;那時(shí)它會(huì)提示你加頭文件
"MyEdit.h" 你此時(shí)就在頭文件Test3Dlg.h加#include "MyEdit.h"
6.為了讓焦點(diǎn)落在按鈕1時(shí),若按下回車響應(yīng)按鈕1的函數(shù),我加了兩個(gè)函數(shù):
void CTest3Dlg::OnOK()//其中這個(gè)就是按鈕1的響應(yīng)函數(shù)
{
// TODO: Add extra validation here
MessageBox("dragon");
//CDialog::OnOK();//我為了不讓默認(rèn)的OnOK函數(shù)執(zhí)行,將它屏蔽了!
}
void CTest3Dlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)//右擊類添加的!
{
// TODO: Add your message handler code here and/or call default
if(nChar==VK_RETURN)
OnOK();
CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
}