有點編程基礎,這段時間一直在學MFC的對話框,我原先想法是在打開的對話框中按ENTER鍵回圈設定每個有TABSTOP屬性的控制元件的焦點。情況是現在對話框一共有8個子空間,我按tab鍵可以正常切換焦點,然后按enter鍵就會回應當前有焦點的空間訊息處理函式,這個是正確的我也理解,但是用我寫的代碼跑enter鍵就只能設定編輯框焦點,button控制元件我debug了下,其實也有跑setfocus()函式,只是沒任何反應,看起來效果是應該有8個控制元件輪流被focus但是現在有4次enter鍵在button上沒作用,但是函式也跑了,我也看了setfocus的值,包括么m_hWnd和回傳值也是有效的,所以很迷惑,請理解的人給予解答,謝謝。
另外我注意到按tab鍵的作用可以回應當前的button,同樣的道理我按enter鍵似乎只能setfocus,而不能“激活”這個button。
以下是代碼片段,很簡單。
void CCalcDlg::OnBnClickedOk()
{
// TODO: 在此添加控制元件通知處理程式代碼
//CDialogEx::OnOK();
//#define OKflag NULL
// int intRet = OKflag;
// EndDialog(intRet);
//callback functon of the default control box
CWnd *p1 = GetFocus();
CWnd *p2 = GetNextDlgTabItem(p1);
CWnd *p3 = p2->SetFocus();
CString ptr = __T("");
p2->GetWindowTextW(ptr);
//MessageBox(ptr.GetString());
//GetFocus()->GetNextWindow()->SetFocus();
}
//afx_msg LRESULT CCalcDlg::OnInitdialog(WPARAM wParam, LPARAM lParam)
//{
// return 0;
//}
WNDPROC oldWindowproc;
LRESULT CALLBACK newWindowproc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam)
{
BOOL ret = false;
if (uMsg == WM_CHAR && wParam == 0x0d)
{
SetFocus(::GetNextWindow(hwnd, GW_HWNDNEXT));
ret = true;
return ret;
}
else
{
return CallWindowProc(
oldWindowproc,
hwnd,
uMsg,
wParam,
lParam);
}
//oldWindowproc(
// hwnd,
// uMsg,
// wParam,
// lParam);
}
afx_msg LRESULT CCalcDlg::OnInitdialog(WPARAM wParam, LPARAM lParam)
{
CDialogEx::OnInitDialog();
pcombox1 = (CComboBox*)GetDlgItem(IDC_COMBO1);
if (!pcombox1->GetCount())
{
pcombox1->InsertString(0, __T("加"));
pcombox1->InsertString(1, __T("減"));
pcombox1->InsertString(2, __T("乘"));
pcombox1->InsertString(3, __T("除"));
}
pcombox1->SetCurSel(0);
GetDlgItem(IDC_EDIT2)->SetFocus();
//::SetFocus(GetDlgItem(IDC_EDIT2)->m_hWnd);
//oldWindowproc = (WNDPROC)SetWindowLongPtr(GetDlgItem(IDC_EDIT2)->m_hWnd, GWLP_WNDPROC, (long)newWindowproc);
//it is very inportant here to set the return value FALSE,otherwise we the default focus will always be set
return FALSE;
}

uj5u.com熱心網友回復:
BOOL CFocus_testDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if( WM_KEYDOWN == pMsg->message )
{
CWnd *pFocus = GetFocus();
if( pFocus )
{
if( IDC_BUTTON1 == pFocus->GetDlgCtrlID() )
{
( CButton * )( GetDlgItem( IDC_BUTTON2 ) )->SetFocus();
}
else if( IDC_BUTTON2 == pFocus->GetDlgCtrlID() )
{
( CButton * )( GetDlgItem( IDC_BUTTON3 ) )->SetFocus();
}
else if( IDC_BUTTON3 == pFocus->GetDlgCtrlID() )
{
( CButton * )( GetDlgItem( IDC_BUTTON1 ) )->SetFocus();
}
return TRUE;
}
}
return CDialog::PreTranslateMessage(pMsg);
}
uj5u.com熱心網友回復:
uj5u.com熱心網友回復:
按鈕獲取焦點的時候按回車應該等效按鈕點擊, 而不會OnBnClickedOkuj5u.com熱心網友回復:
我試了下這個preTranslateMessage()也不行,還是button上的setfocus()不起作用,但是當我設定了斷點在setfocus()函式后面能看到button被聚焦,我正常跑程式就啥都看不到,button沒反應,我原來的代碼也是這樣,不知道為什么。望解答困惑。謝謝
uj5u.com熱心網友回復:
我這是參考孫鑫的視頻教程自己做得例子,環境是VS2015,難道更新了東西嗎。uj5u.com熱心網友回復:
問一下,你的tab切換怎么做的,我用ctrl+D切換順序設定好,用preTranslateMessage攔截訊息,但是點擊鍵盤始終跟不進preTranslateMessage,不知道為什么,我這個是子對話框,在上層對話框也加了preTranslateMessage進行訊息過濾,但是都沒進preTranslateMessage這個函式uj5u.com熱心網友回復:
OnBnClickedOk()OK 鍵 搶了焦點 !
uj5u.com熱心網友回復:
BOOL CAboutDlg::PreTranslateMessage(MSG* pMsg){
// TODO: Add your specialized code here and/or call the base class
if((WM_KEYDOWN == pMsg->message ) && (VK_RETURN == (int) pMsg->wParam))
{
pMsg->wParam=VK_TAB;
}
return CDialog::PreTranslateMessage(pMsg);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/62483.html
標籤:界面
