码迷,mamicode.com
首页 > 编程语言 > 详细

MFC消息分类及处理方式

时间:2015-07-13 22:28:32      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:mfc

1. 标准消息
  除WM_COMMAND之外,所有以WM_开头的消息;从CWnd派生的类,都可以接收到这类消息,比如:
  ON_WM_CREATE      对应WM_CREATE消息
  ON_WM_PAINT           对应WM_PAINT消息
  ON_WM_MOUSEMOVE    对应WM_MOUSEMOVE消息

  ON_WM_XXX   ……

对上边列举出的消息进行运用(功能:让一个字符创跟着鼠标的移动而移动),建一个win32简单应用程序,删除入口函数,将stdafx.h中的头文件 <windows.h> 更改为 <afxwin.h>,Project-->Settings菜单项中设置使用MFC库

// XiaoXi.cpp : Defines the entry point for the application.
//

#include "stdafx.h"


class CMyFrameWnd : public CFrameWnd
{
	DECLARE_MESSAGE_MAP()
protected:
	afx_msg int OnCreate( LPCREATESTRUCT lpCreateStruct );
	afx_msg void OnPaint( );
	afx_msg void OnMouseMove( UINT nFlags, CPoint point );
public:
	int xPos,yPos;
	CMyFrameWnd();
	
};
BEGIN_MESSAGE_MAP(CMyFrameWnd,CFrameWnd)
ON_WM_CREATE(WM_CREATE,OnCreate)
ON_WM_PAINT(WM_PAINT,OnPaint)
ON_WM_MOUSEMOVE(WM_MOUSEMOVE,OnMouseMove)
END_MESSAGE_MAP()
CMyFrameWnd::CMyFrameWnd()
{
	xPos = 100;
	yPos = 100;
}

void CMyFrameWnd ::OnMouseMove( UINT nFlags, CPoint point )
{
	xPos = point.x;
	yPos = point.y;
	nFlags = MK_SHIFT;
	::InvalidateRect(m_hWnd,NULL,TRUE);
}

void CMyFrameWnd ::OnPaint()
{
	PAINTSTRUCT ps = {0};
	HDC hdc = ::BeginPaint(m_hWnd,&ps);
	TextOut(hdc,xPos,yPos,"Hello fc !",10);
	::EndPaint(m_hWnd,&ps);
}

int CMyFrameWnd::OnCreate( LPCREATESTRUCT lpCreateStruct )
{
	MessageBox("CMyFrameWnd::OnCreate");
	return CFrameWnd::OnCreate(lpCreateStruct);
}



class CMyWinApp: public CWinApp
{
public: 
	virtual BOOL InitInstance();
};

CMyWinApp TheApp;
BOOL CMyWinApp::InitInstance()
{
	CMyFrameWnd*pFrame =  new CMyFrameWnd;
	pFrame->Create(NULL,"Xiaoxi");
	m_pMainWnd = pFrame;
    m_pMainWnd->ShowWindow(SW_SHOW);
	m_pMainWnd->UpdateData();
	return TRUE;
	
}

2.自定义消息:n的值有3万多个
  #define WM_MYMESSAGE WM_USER+n

 做一个按钮,点击按钮响应自己的定义的消息:

// XiaoXi.cpp : Defines the entry point for the application.
//

#include "stdafx.h"

#define WM_MYMESSAGE WM_USER+1001  //自定义消息


class CMyFrameWnd : public CFrameWnd
{
	DECLARE_MESSAGE_MAP( )
protected:
	afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
	afx_msg LRESULT OnMyMessage( WPARAM wParam,LPARAM lParam );
	afx_msg void OnTest1( );

};
BEGIN_MESSAGE_MAP(CMyFrameWnd,CFrameWnd)
ON_WM_CREATE( )
ON_COMMAND( 1001, OnTest1 )
END_MESSAGE_MAP()

int CMyFrameWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	AfxMessageBox( "CMyFrameWnd::OnCreate" );
	::PostMessage( this->m_hWnd, WM_MYMESSAGE, 1, 2 );  //发自己定义的消息
	::CreateWindowEx( 0, "BUTTON", "Test1", WS_CHILD|   //窗口创建但未显示的时候创建按钮
		WS_VISIBLE, 100, 100, 100, 40, 
		this->m_hWnd, (HMENU)1001, 
		AfxGetInstanceHandle(), NULL );
	
	return CFrameWnd::OnCreate(lpCreateStruct);
}

void CMyFrameWnd::OnTest1( )
{
	AfxMessageBox( "Test1被点击" );
}
LRESULT CMyFrameWnd::OnMyMessage( WPARAM wParam, LPARAM lParam )
{
	AfxMessageBox( "自己的消息被处理" );
	return 0;
}

class CMyWinApp: public CWinApp
{
public: 
	virtual BOOL InitInstance();
};

CMyWinApp TheApp;
BOOL CMyWinApp::InitInstance()
{
	CMyFrameWnd*pFrame =  new CMyFrameWnd;
	pFrame->Create(NULL,"Xiaoxi");
	m_pMainWnd = pFrame;
    m_pMainWnd->ShowWindow(SW_SHOW);
	m_pMainWnd->UpdateData();
	return TRUE;
	
}
技术分享

3.命令消息(WM_COMMAND)

    来自菜单、加速键或工具栏按钮的消息。这类消息都以WM_COMMAND呈现。在MFC中,通过菜单项的标识(ID)来区分不同的命令消息

  两种形式:
  ON_COMMAND( ID, 处理函数)
  ON_COMMAND_RANGE( 起始ID, 终止ID, 处理函数 )
  例子:创建三个按钮,我们先按照普通消息处理COMMAND消息

补充: WM_COMMAND的消息会在文档,视图,框架,应用程序 四种链表进行遍历形成消息网

// XiaoXi.cpp : Defines the entry point for the application.
//

#include "stdafx.h"




class CMyFrameWnd : public CFrameWnd
{
	DECLARE_MESSAGE_MAP( )
protected:
	afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
	afx_msg void OnTest( UINT nID );

};
BEGIN_MESSAGE_MAP(CMyFrameWnd,CFrameWnd)
ON_WM_CREATE( )
ON_COMMAND_RANGE( 1001, 1003, OnTest ) //多个按钮共用一个宏
END_MESSAGE_MAP()

int CMyFrameWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	AfxMessageBox( "CMyFrameWnd::OnCreate" );
	::CreateWindowEx( 0, "BUTTON", "Test1", WS_CHILD|   //窗口创建但未显示的时候创建按钮
		WS_VISIBLE, 100, 100, 100, 40, 
		this->m_hWnd, (HMENU)1001, 
		AfxGetInstanceHandle(), NULL );
	::CreateWindowEx ( 0, "BUTTON", "Test2", WS_CHILD|
		WS_VISIBLE, 100, 200, 100, 40,
		this->m_hWnd, (HMENU)1002,
		AfxGetInstanceHandle(), NULL );
	::CreateWindowEx( 0, "BUTTON", "Test3", WS_CHILD|
		WS_VISIBLE, 100, 300, 100, 40,
		this->m_hWnd, (HMENU)1003,
		AfxGetInstanceHandle(), NULL );
	
	return CFrameWnd::OnCreate(lpCreateStruct);
}


VOID CMyFrameWnd::OnTest( UINT nID )
{
	if( nID==1001)
		AfxMessageBox( "Test1被点击" );
	else if( nID == 1002 )
		AfxMessageBox( "Test2被点击" );
	else
		AfxMessageBox( "Test3被点击" );
}


class CMyWinApp: public CWinApp
{
public: 
	virtual BOOL InitInstance();
};

CMyWinApp TheApp;
BOOL CMyWinApp::InitInstance()
{
	CMyFrameWnd*pFrame =  new CMyFrameWnd;
	pFrame->Create(NULL,"Xiaoxi");
	m_pMainWnd = pFrame;
    m_pMainWnd->ShowWindow(SW_SHOW);
	m_pMainWnd->UpdateData();
	return TRUE;
	
}
技术分享



4 通知消息

        由控件产生的消息,例如,按钮的单击,列表框的选择等均产生此类消息,为的是向其父窗口(通常是对话框)通知事件的发生。这类消息也是以WM_COMMAND形式呈现。
   ON_EN_CHANFE
   ON_通知码
   例子:加一个编辑框。

// XiaoXi.cpp : Defines the entry point for the application.
//

#include "stdafx.h"




class CMyFrameWnd : public CFrameWnd
{
	DECLARE_MESSAGE_MAP( )
protected:
	afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
	afx_msg void OnEnChange( );

};
BEGIN_MESSAGE_MAP(CMyFrameWnd,CFrameWnd)
ON_WM_CREATE( )
ON_EN_CHANGE( 1004, OnEnChange )
END_MESSAGE_MAP()

int CMyFrameWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	::CreateWindowEx( 0, "EDIT", "", WS_CHILD|WS_VISIBLE|
		WS_BORDER, 300, 100, 100, 100, 
		this->m_hWnd, (HMENU)1004,
		AfxGetInstanceHandle(), NULL );
	
	return CFrameWnd::OnCreate(lpCreateStruct);
}

void CMyFrameWnd::OnEnChange( )
{
	AfxMessageBox( "内容被修改" );
}

class CMyWinApp: public CWinApp
{
public: 
	virtual BOOL InitInstance();
};

CMyWinApp TheApp;
BOOL CMyWinApp::InitInstance()
{
	CMyFrameWnd*pFrame =  new CMyFrameWnd;
	pFrame->Create(NULL,"Xiaoxi");
	m_pMainWnd = pFrame;
    m_pMainWnd->ShowWindow(SW_SHOW);
	m_pMainWnd->UpdateData();
	return TRUE;
	
}
技术分享



版权声明:本文为博主原创文章,未经博主允许不得转载。

MFC消息分类及处理方式

标签:mfc

原文地址:http://blog.csdn.net/meetings/article/details/46866447

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!