VC6.0中對消息的特殊處理--PreTranslateMessage說明

 

首先看看MSDN對PreTranslateMessage的說明

 

 

Override this function to filter window messages before they are dispatched to the Windows functions TranslateMessage and DispatchMessage The default implementation performs accelerator-key translation, so you must call the CWinApp::PreTranslateMessage member function in your overridden version.

 
virtual BOOL PreTranslateMessage(
MSG* pMsg
);

Parameters

pMsg

A pointer to a MSG structure that contains the message to process.

Return Value

Nonzero if the message was fully processed in PreTranslateMessage and should not be processed further. Zero if the message should be processed in the normal way.

Requirements

Header: afxwin.h

 

------------------------------------------------------

再看看一個具體的例子.

BOOL MyDialog::PreTranslateMessage(MSG* pMsg)
{
    if(pMsg->message == WM_KEYDOWN)
    {
        //Return key
        if( pMsg->wParam == VK_RETURN )
        {  //若焦點在本頁面上的一個名叫IDC_EDT_TEST_DATA_FILE的EditBox上,按回車鍵則焦點轉移到下個控件
            if(GetFocus() == GetDlgItem(IDC_EDT_TEST_DATA_FILE))
            {
               
                GetFocus()->GetNextWindow()->SetFocus();
                return TRUE;
            }

//若焦點在本頁面一個叫IDC_EDT_WAITTIME的EditBox上,按回車鍵時焦點轉移到父窗口的IDC_BEXECUTE按鈕上
            else if(GetFocus() == GetDlgItem(IDC_EDT_WAITTIME))
            {
                ((CArithmeticExpressionDlg*)this->GetParent())->GetDlgItem(IDC_BEXECUTE)->SetFocus();
                return TRUE;
            }
        }

        //[Shift] + [TAB]
        else if(GetKeyState(VK_SHIFT) & 0x8000 && pMsg->wParam == VK_TAB)
        {
            //若焦點在本頁面一個叫IDC_EDT_WAITTIME的EditBox上,Shift + TAB是,焦點跳到窗口名叫IDC_RBTN_STEP4的Radio Button上
            if(pMsg->hwnd == (GetDlgItem(IDC_EDT_TEST_DATA_FILE))->m_hWnd )
            {
                (((CArithmeticExpressionDlg*)this->GetParent())->GetDlgItem(IDC_RBTN_STEP4))->SetFocus();
                return TRUE;
            }
        }
        else if( pMsg->wParam == VK_TAB )
        {
            //[TAB]

//若焦點在本頁面一個叫IDC_EDT_WAITTIME的EditBox上,TAB時焦點轉移到父窗口的IDC_BEXECUTE按鈕上
             if( pMsg->hwnd == GetDlgItem(IDC_EDT_WAITTIME)->GetSafeHwnd())
            {
                ((CArithmeticExpressionDlg*)this->GetParent())->GetDlgItem(IDC_BEXECUTE)->SetFocus();
                return TRUE;
            }
        }
        //Escape key
        else if( pMsg->wParam == VK_ESCAPE )
        {
            //按Esc鍵時,相當於按窗口上有的EXIT按鈕.
            CButton* pBtnExit = (CButton*)((CArithmeticExpressionDlg*)this->GetParent())->GetDlgItem(IDC_BEXIT);
            ::SendMessage(pBtnExit->GetSafeHwnd(),WM_LBUTTONDOWN,0,0);
            ::SendMessage(pBtnExit->GetSafeHwnd(),WM_LBUTTONUP,0,0);
            return TRUE;
        }
        //other events

        /*********************some other code***************************/     
    }
    //others

    return CDialog::PreTranslateMessage(pMsg);
}

 

本例子的功能就是在父子窗口之前實現焦點的轉移.

例如,當焦點位於子窗口的最後一個控件時,此時接着操作TAB鍵,就要注焦點轉移到父窗口中,並且是緊跟着子窗口的那個控件.

 

 

聞香止步 收集於:http://blog.csdn.net/roofwei/archive/2009/02/18/3906112.aspx
淘寶店 擺件 飾品 *木雕系列*:海南黃花梨、越南黃花梨、草花梨、小葉紫檀、黑檀、綠檀木、黃楊木、桃木髮簪  木梳 樟木壁掛 佛珠 車飾 擺件

http://shop36570193.taobao.com

朋友,有空來看看,喜歡的朋友請收藏

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章