• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            大龍的博客

            常用鏈接

            統(tǒng)計

            最新評論

            如何在對話框程序中讓對話框捕獲WM_KEYDOWN消息 ---- zt

            如何在對話框程序中讓對話框捕獲WM_KEYDOWN消息

            作者:孫鑫                  日期:2003-9-4

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

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

            第一步:新建一個工程,選擇MFC AppWizard (exe),工程名為WinSun,點擊ok,進入下一步,選擇Dialog based,點擊Finish

            第二步:在CWinSunApp類上點擊右鍵,選擇Add Member Varialbe,增加一個類型為HWND,變量名m_hwndDlgpublic的變量。代碼如下:

            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.cppCWinSunApp類)文件中的InitInstance()函數(shù)中添加如下代碼:

            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類上點擊右鍵,選擇Add Virtual Function,在左邊一欄里,選擇ProcessMessageFilter,在右邊按鈕上選擇Add and Edit,然后加入以下代碼:

            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)

                   {

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

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

                          {

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

                                 if(lpMsg->message==WM_KEYDOWN)

                                 {

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

                                 }

                          }

                   }

                   return CWinApp::ProcessMessageFilter(code, lpMsg);

            }

             

            第五步:在WinSunDlg.cppCWinSunDlg類)中的OnInitialDialog()函數(shù)中加入以下代碼:

            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

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

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

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

            }

             

            第六步:在對話框窗口銷毀后,將CWinSunApp類中的變量m_hwndDlg置為NULL,為此我們CWinSunDlg類上點擊右鍵,選擇Add Windows Message Handler,在左邊一欄中選擇WM_DESTROY,在右邊按鈕上選擇Add and Edit,然后加入以下代碼:

            WinSunDlg.cpp

            void CWinSunDlg::OnDestroy()

            {

                   CDialog::OnDestroy();

                  

                   // TODO: Add your message handler code here

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

            }

             

            至此,我們的工作就做完了,現(xiàn)在我們可以按Ctrl+F5運行程序,看到我們想要的結(jié)果。當(dāng)然,如果我們想捕獲WM_KEYUPWM_CHAR消息,也是類似,這就交給讀者下來自行完成了。想要瀏覽更多技術(shù)文章,請登錄網(wǎng)站:http://www.sunxin.org

            posted on 2007-08-29 11:35 大龍 閱讀(675) 評論(0)  編輯 收藏 引用


            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            亚洲人成伊人成综合网久久久| 美女写真久久影院| 亚洲欧美另类日本久久国产真实乱对白| 一级做a爰片久久毛片16| 久久久久久国产精品美女| 欧美一区二区久久精品| 久久久一本精品99久久精品66| 伊人久久大香线蕉亚洲五月天| 久久久亚洲欧洲日产国码二区| 色成年激情久久综合| 2019久久久高清456| 99久久精品国产麻豆| 久久久久久亚洲精品无码| 亚洲AV无码久久精品色欲| 日本三级久久网| 一本一本久久a久久综合精品蜜桃| 国产精品久久久久久搜索| 亚洲人AV永久一区二区三区久久 | 久久久久久九九99精品| 久久综合视频网站| 青青草原综合久久大伊人精品| 精品久久久久久久国产潘金莲| 久久婷婷国产麻豆91天堂| 午夜精品久久久久久99热| 久久人妻少妇嫩草AV蜜桃| 办公室久久精品| 久久久精品人妻一区二区三区蜜桃| 人妻丰满?V无码久久不卡| 99久久免费只有精品国产| 漂亮人妻被黑人久久精品| 久久精品卫校国产小美女| 天堂无码久久综合东京热| 久久高潮一级毛片免费| 曰曰摸天天摸人人看久久久| avtt天堂网久久精品| 久久久久99精品成人片直播| 精品国产乱码久久久久软件| 日本WV一本一道久久香蕉| 色天使久久综合网天天| 久久99精品国产麻豆宅宅| 无码专区久久综合久中文字幕|