基于对话框程序中让对话框捕获WM_KEYDOWN消息的实现方法

2016-02-19 09:51 2 1 收藏

下面图老师小编要向大家介绍下基于对话框程序中让对话框捕获WM_KEYDOWN消息的实现方法,看起来复杂实则是简单的,掌握好技巧就OK,喜欢就赶紧收藏起来吧!

【 tulaoshi.com - 编程语言 】

在对话框程序中,我们经常是利用对话框上的子控件进行命令响应来处理一些事件。如果我们想要让对话框(子控件的父窗口)类来响应我们的按键消息,我们可以通过ClassWizard对WM_KEYDOWN消息进行响应,当程序运行后,我们按下键盘上的按键,但对话框不会有任何的反应。这是因为在对话框程序中,某些特定的消息,例如按键消息,它们被Windows内部的对话框过程处理了(即在基类中完成了处理,有兴趣的读者可以查看MFC的源代码),或者被发送给子控件进行处理,所以我们在对话框类中就捕获不到按键的消息了。

既然我们知道了这个处理的过程,我们就可以找到底层处理按键消息的函数,然后在子类中重载它,就可以在对话框程序中处理按键消息了。在MFC中,是利用BOOL ProcessMessageFilter(int code, LPMSG lpMsg)这个虚函数来过滤或响应菜单和对话框的特定Windows消息。下面我们通过程序给大家演示基于对话框的应用程序对WM_KEYDOWN消息的捕获。

第一步:新建一个工程,选择MFC AppWizard (exe),工程名为WinSun,点击ok,进入下一步,选择Dialog based,点击Finish。

第二步:在CWinSunApp类上点击右键,选择Add Member Varialbe,增加一个类型为HWND,变量名m_hwndDlg的public的变量。

代码如下:
代码如下:

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()函数中添加如下代码:
代码如下:

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

       }

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

       else if (nResponse == IDCANCEL)

       {

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

              //  dismissed with Cancel

       }

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

       // 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)

       {

              //判断消息,如果消息是从对话框发出的或者其子控件发出的,我们就进行处理。sunxin

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

              {

                     //如果消息是WM_KEYDOWN,我们就弹出一个消息框。sunxin

                     if(lpMsg-message==WM_KEYDOWN)

                     {

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

                     }

              }

       }

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

       return CWinApp::ProcessMessageFilter(code, lpMsg);

}

第五步:在WinSunDlg.cpp(CWinSunDlg类)中的OnInitialDialog()函数中加入以下代码:
代码如下:

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;

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

              strAboutMenu.LoadString(IDS_ABOUTBOX);

              if (!strAboutMenu.IsEmpty())

              {

                     pSysMenu-AppendMenu(MF_SEPARATOR);

                     pSysMenu-AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);

              }

       }

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

       // 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;

}

至此,我们的工作就做完了,现在我们可以按Ctrl+F5运行程序,看到我们想要的结果。

来源:https://www.tulaoshi.com/n/20160219/1592419.html

延伸阅读
代码如下: package test001; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JToolBar; public class TestJOptionPane implements ActionListener{     private JFrame jf = new JFr...
如何在基于对话框的程序中动态设置鼠标指针 赵湘宁 本文范例程序     本文旨在示范在对话框的按钮上(或其它控制上)使用单独的鼠标指针。Windows编程中有两种方法改变指针:一种是当应用的主窗口类注册时,为WNDCLASS结构提供一个全程光标指针(HCU...
标签: PS PS教程
      横轴代表像素本身的两度,竖轴代表新调节的亮度。中间的直线代表目前输入与输出之间的关系。可以单击某个Channels,进行Curves设置,或按Shift键选择两个以上的Channels.     在图像中准备要调节亮度的地方按下鼠标,一个圆出现在Curves对话框中的直线上。拖动这个圆即可调节图...
标签: Web开发
    1.将下面一段代码插入head与/head之间: script function rusure() {  question = confirm("确实要去进入吗?")  if (question !="0") { window.open("","测试公告窗口","width=340,height=163,toolbar=0,status=0,menubar=0,resize=0"); } } /script     2.在链接标签内加入onclick()语句...
(1) 参数 typedef struct { DWORD lStructSize; HWND hwndOwner; HWND hInstance; COLORREF rgbResult; COLORREF * lpCustColors; DWORD Flags; LPARAM lCustData; LPCCHOOKPROC lpfnHook; LPCTSTR lpTemplateName; } CHOOSECOLOR, *LPCHOOSECOLOR; (2) API...

经验教程

994

收藏

39
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部