a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁(yè) > 編程 > C > 正文

基于對(duì)話框程序中讓對(duì)話框捕獲WM_KEYDOWN消息的實(shí)現(xiàn)方法

2020-01-26 16:17:19
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

在對(duì)話框程序中,我們經(jīng)常是利用對(duì)話框上的子控件進(jìn)行命令響應(yīng)來(lái)處理一些事件。如果我們想要讓對(duì)話框(子控件的父窗口)類來(lái)響應(yīng)我們的按鍵消息,我們可以通過(guò)ClassWizard對(duì)WM_KEYDOWN消息進(jìn)行響應(yīng),當(dāng)程序運(yùn)行后,我們按下鍵盤(pán)上的按鍵,但對(duì)話框不會(huì)有任何的反應(yīng)。這是因?yàn)樵趯?duì)話框程序中,某些特定的消息,例如按鍵消息,它們被Windows內(nèi)部的對(duì)話框過(guò)程處理了(即在基類中完成了處理,有興趣的讀者可以查看MFC的源代碼),或者被發(fā)送給子控件進(jìn)行處理,所以我們?cè)趯?duì)話框類中就捕獲不到按鍵的消息了。

既然我們知道了這個(gè)處理的過(guò)程,我們就可以找到底層處理按鍵消息的函數(shù),然后在子類中重載它,就可以在對(duì)話框程序中處理按鍵消息了。在MFC中,是利用BOOL ProcessMessageFilter(int code, LPMSG lpMsg)這個(gè)虛函數(shù)來(lái)過(guò)濾或響應(yīng)菜單和對(duì)話框的特定Windows消息。下面我們通過(guò)程序給大家演示基于對(duì)話框的應(yīng)用程序?qū)M_KEYDOWN消息的捕獲。

第一步:新建一個(gè)工程,選擇MFC AppWizard (exe),工程名為WinSun,點(diǎn)擊ok,進(jìn)入下一步,選擇Dialog based,點(diǎn)擊Finish。

第二步:在CWinSunApp類上點(diǎn)擊右鍵,選擇Add Member Varialbe,增加一個(gè)類型為HWND,變量名m_hwndDlg的public的變量。

代碼如下:

復(fù)制代碼 代碼如下:

WinSun.h

class CWinSunApp : public CWinApp

{

public:

       HWND m_hwndDlg;

       CWinSunApp();

 

// Overrides

       // ClassWizard generated virtual function overrides

       //{{AFX_VIRTUAL(CWinSunApp)

       public:

       virtual BOOL InitInstance();

       //}}AFX_VIRTUAL

 

// Implementation

 

       //{{AFX_MSG(CWinSunApp)

              // NOTE - the ClassWizard will add and remove member functions here.

              //    DO NOT EDIT what you see in these blocks of generated code !

       //}}AFX_MSG

       DECLARE_MESSAGE_MAP()

};


第三步:在WinSun.cpp(CWinSunApp類)文件中的InitInstance()函數(shù)中添加如下代碼:
復(fù)制代碼 代碼如下:

WinSun.cpp

BOOL CWinSunApp::InitInstance()

{

       AfxEnableControlContainer();

 

       // Standard initialization

       // If you are not using these features and wish to reduce the size

       //  of your final executable, you should remove from the following

       //  the specific initialization routines you do not need.

 

#ifdef _AFXDLL

       Enable3dControls();                     // Call this when using MFC in a shared DLL

#else

       Enable3dControlsStatic();      // Call this when linking to MFC statically

#endif

 

       CWinSunDlg dlg;

       m_pMainWnd = &dlg;

       int nResponse = dlg.DoModal();

       if (nResponse == IDOK)

       {

              // TODO: Place code here to handle when the dialog is

              //  dismissed with OK

       }

       else if (nResponse == IDCANCEL)

       {

              // TODO: Place code here to handle when the dialog is

              //  dismissed with Cancel

       }

 

       // Since the dialog has been closed, return FALSE so that we exit the

       //  application, rather than start the application's message pump.

       m_hwndDlg=NULL;

       return FALSE;

}


第四步:在CWinSunApp類上點(diǎn)擊右鍵,選擇Add Virtual Function,在左邊一欄里,選擇ProcessMessageFilter,在右邊按鈕上選擇Add and Edit,然后加入以下代碼:
復(fù)制代碼 代碼如下:

WinSun.cpp

BOOL CWinSunApp::ProcessMessageFilter(int code, LPMSG lpMsg)

{

       // TODO: Add your specialized code here and/or call the base class

       if(m_hwndDlg!=NULL)

       {

              //判斷消息,如果消息是從對(duì)話框發(fā)出的或者其子控件發(fā)出的,我們就進(jìn)行處理。sunxin

              if((lpMsg->hwnd==m_hwndDlg) || ::IsChild(m_hwndDlg,lpMsg->hwnd))

              {

                     //如果消息是WM_KEYDOWN,我們就彈出一個(gè)消息框。sunxin

                     if(lpMsg->message==WM_KEYDOWN)

                     {

                            AfxMessageBox("捕獲WM_KEYDOWN消息成功!");

                     }

              }

       }

       return CWinApp::ProcessMessageFilter(code, lpMsg);

}


第五步:在WinSunDlg.cpp(CWinSunDlg類)中的OnInitialDialog()函數(shù)中加入以下代碼:
復(fù)制代碼 代碼如下:

WinSunDlg.cpp

BOOL CWinSunDlg::OnInitDialog()

{

       CDialog::OnInitDialog();

 

       // Add "About..." menu item to system menu.

 

       // IDM_ABOUTBOX must be in the system command range.

       ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);

       ASSERT(IDM_ABOUTBOX < 0xF000);

 

       CMenu* pSysMenu = GetSystemMenu(FALSE);

       if (pSysMenu != NULL)

       {

              CString strAboutMenu;

              strAboutMenu.LoadString(IDS_ABOUTBOX);

              if (!strAboutMenu.IsEmpty())

              {

                     pSysMenu->AppendMenu(MF_SEPARATOR);

                     pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);

              }

       }

 

       // Set the icon for this dialog.  The framework does this automatically

       //  when the application's main window is not a dialog

       SetIcon(m_hIcon, TRUE);                  // Set big icon

       SetIcon(m_hIcon, FALSE);          // Set small icon

     

       // TODO: Add extra initialization here

//將對(duì)話框的句柄傳遞到CWinSunApp類中。sunxin

       ((CWinSunApp*)AfxGetApp())->m_hwndDlg=m_hWnd;

       return TRUE;  // return TRUE  unless you set the focus to a control

}


第六步:在對(duì)話框窗口銷毀后,將CWinSunApp類中的變量m_hwndDlg置為NULL,為此我們?cè)贑WinSunDlg類上點(diǎn)擊右鍵,選擇Add Windows Message Handler,在左邊一欄中選擇WM_DESTROY,在右邊按鈕上選擇Add and Edit,然后加入以下代碼:
復(fù)制代碼 代碼如下:

WinSunDlg.cpp

void CWinSunDlg::OnDestroy()

{

       CDialog::OnDestroy();

     

       // TODO: Add your message handler code here

       ((CWinSunApp*)AfxGetApp())->m_hwndDlg=NULL;

}


至此,我們的工作就做完了,現(xiàn)在我們可以按Ctrl+F5運(yùn)行程序,看到我們想要的結(jié)果。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 天天操天天舔天天爽 | a免费在线观看 | 美日韩一区 | 精品国产一区二区三区免费 | 日韩一区中文字幕 | 欧美日韩成人激情 | 99在线视频观看 | 国产精品久久久久毛片软件 | 精久久| 成人五月网| 一区二区三区四区av | 一本久久a久久精品亚洲 | 欧美一级免费大片 | 成人精品一区二区三区 | 国产aⅴ一区二区 | 国产一区二区三区免费在线观看 | 欧美一级毛片日韩一级 | 日日摸夜夜添夜夜添特色大片 | 国产精品一区二区三区在线免费观看 | 一区综合 | 亚洲一区二区三区久久 | 国产精品九九九 | 亚洲午夜精品一区二区三区 | 91社区在线高清 | 欧美一级视频在线观看 | 久久9久久 | 免费黄色av | 超碰国产在线 | 天天澡天天狠天天天做 | 久久国产精品99国产 | 九九综合 | 91在线视频一区 | 日日夜夜狠狠 | 久久久亚洲精品视频 | 国产精品99视频 | 久久韩日 | 欧美日韩一区视频 | 美女视频黄又黄又免费 | 国产大片在线观看 | 久久久精品免费看 | 国产精品一区99 |