首页 > REDM基础教程 > REDM基础教程6-主窗口消息分发
2016十月30

REDM基础教程6-主窗口消息分发

[隐藏]

REDM消息分发的流程是trunk过程(流程图位于Docs\DUI主流程图.vsdx

blob.png

trunk的核心思想是把窗口过程中的hWnd值换成了我们的this指针,比如MyTest的Main窗口就被trunk换成了我们的CMainWnd指针

DMCWnd::WindowProc函数就接管了所有的消息分发(SetWindowLongPtr

LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

前面提到了,hWnd被换成了this,所以在WindowProc中有如下代码:

DMCWnd* pThis = (DMCWnd*)hWnd; // 强转为对象指针
BOOL bRet = pThis->ProcessWindowMessage(pThis->m_hWnd, uMsg, wParam, lParam, lRes, 0);

分发的流程为:

blob.png

所以要处理主窗口的消息分发,只需处理ProcessWindowMessage即可,ProcessWindowMessage在DM库内部使用了宏定义来声明

#define DECLARE_MESSAGE_MAP() \
public: \
	BOOL IsMsgHandled() const; \
	void SetMsgHandled(BOOL bHandled); \
	BOOL ProcessWindowMessage(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT& lResult, DWORD dwMsgMapID = 0); \
	BOOL _ProcessWindowMessage(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT& lResult, DWORD dwMsgMapID);

当然,你可以不使用宏,直接使用函数的方式来处理它的消息,本质是一样的

     

   

1.使用MacroTool小工具

宏是提供了易用性,但难于调试,因为VS在转换宏时,会把所有宏转换成一行!,所以提供了个小工具方便转换宏,打开Tools\MacroTool\已编译的bin\DMMacro.exe,把DMMsgCrack.h、DMXmlCrack.h、DUIMsgCrack.hDMEventCrack.h拷贝到同目录下

把要转换的宏粘贴到小工具里,如我们的MainWnd.cpp中的

BEGIN_MSG_MAP(CMainWnd)
	MSG_WM_INITDIALOG(OnInitDialog)
	MSG_WM_SIZE(OnSize)
	CHAIN_MSG_MAP(DMHWnd)// 将未处理的消息交由DMHWnd处理
END_MSG_MAP()

点击Convert,即转换成功(注释去掉)

17.gif

转换后的代码如下:

BOOL CMainWnd::IsMsgHandled() const 
{ 
	return m_bMsgHandled; 
} 
void CMainWnd::SetMsgHandled(BOOL bHandled) 
{ 
	m_bMsgHandled = bHandled; 
} 
BOOL CMainWnd:: ProcessWindowMessage(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT& lResult, DWORD dwMsgMapID/* = 0*/) 
{ 
	BOOL bOldMsgHandled = m_bMsgHandled; 
	BOOL bRet = _ProcessWindowMessage(hWnd, uMsg, wParam, lParam, lResult, dwMsgMapID); 
	m_bMsgHandled = bOldMsgHandled; 
	return bRet; 
} 
BOOL CMainWnd::_ProcessWindowMessage(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT& lResult, DWORD dwMsgMapID) 
{ 
	BOOL bHandled = TRUE; 
	hWnd; 
	uMsg; 
	wParam; 
	lParam; 
	lResult; 
	bHandled; 
	switch(dwMsgMapID) 
	{ 
	case 0:
		if (uMsg == WM_INITDIALOG) 
		{ 
			SetMsgHandled(TRUE); 
			lResult = (LRESULT)OnInitDialog((HWND)wParam, lParam); 
			if(IsMsgHandled()) 
				return TRUE; 
		}
		if (uMsg == WM_SIZE) 
		{ 
			SetMsgHandled(TRUE); 
			OnSize((UINT)wParam, _WTYPES_NS::CSize(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam))); 
			lResult = 0; 
			if(IsMsgHandled()) 
				return TRUE; 
		}
		{ 
			if(DMHWnd::ProcessWindowMessage(hWnd, uMsg, wParam, lParam, lResult,dwMsgMapID)) 
				return TRUE; 
		}

		break; 
	default: 
		DMASSERT_EXPR(0, L"msgMapID不对,消息未被处理");
		break; 
	} 
	return FALSE; 
}

从转换后的代码我们可以看出,主消息分发,实际对应的是if(uMsg == XX消息)这种模式

如果处理完成,就直接返回TRUE,

if(IsMsgHandled()) 
	return TRUE;

未处理,继续向下走,最后交由父类处理

CHAIN_MSG_MAP(DMHWnd)// 将未处理的消息交由DMHWnd处理--->
if(DMHWnd::ProcessWindowMessage(hWnd, uMsg, wParam, lParam, lResult,dwMsgMapID)) 
	return TRUE;

所以如果我们想处理完某个消息后,又想让父类来接着处理

可以使用

SetMsgHandled(FALSE);  // 由DMHWnd继续处理OnSize消息

如CMainWnd的OnSize消息处理:

void CMainWnd::OnSize(UINT nType, CSize size)
{
	DUIWindow* pMaxBtn = FindChildByName(L"maxbutton");
	DUIWindow* pRestoreBtn = FindChildByName(L"restorebutton");
	if (0 != size.cx&&0 != size.cy&&pMaxBtn&&pRestoreBtn)
	{
		if (SIZE_MAXIMIZED == nType)
		{
			pMaxBtn->DM_SetVisible(false);
			pRestoreBtn->DM_SetVisible(true);
		}
		else if (SIZE_RESTORED == nType)
		{
			pMaxBtn->DM_SetVisible(true);
			pRestoreBtn->DM_SetVisible(false);
		}
	}
	SetMsgHandled(FALSE);  // 由DMHWnd继续处理OnSize消息
}

    

   

2.增加主窗口消息函数

经过前面的介绍,增加主窗口的消息函数是非常简单的

1.在头文件中声明

	DECLARE_MESSAGE_MAP()						// 仿MFC消息映射宏,也可以使用BEGIN_MSG_MAPT宏使消息处理在头文件

2.在cpp文件中定义,消息函数的定义可以在DMMsgCrack.h中找到,使用的就是atlcrack.h宏,不同的是,DM的一个主窗口只有一份m_bMsgHandled,而atlcrack.h中有多份

BEGIN_MSG_MAP(CMainWnd)
	....
	CHAIN_MSG_MAP(DMHWnd)// 将未处理的消息交由DMHWnd处理
END_MSG_MAP()

3.有些消息需要自己处理完后交由父窗口处理,使用

SetMsgHandled(FALSE);  // 由DMHWnd继续处理OnSize消息

 

  下一节教程:REDM基础教程7-DUI子控件消息分发

    

文章作者:hgy413
本文地址:https://hgy413.com/3611.html
版权所有 © 转载时必须以链接形式注明作者和原始出处!

23 Responses to “REDM基础教程6-主窗口消息分发”

  1. #1 minecraft 回复 | 引用 Post:2018-10-04 20:04

    I like the valuable information you supply to your articles.

    I will bookmark your blog and take a look at once more right here frequently.
    I am rather sure I will learn plenty of new
    stuff proper right here! Good luck for the next!

  2. #2 minecraft 回复 | 引用 Post:2018-10-07 02:31

    Howdy would you mind letting me know which webhost you’re working with?
    I’ve loaded your blog in 3 different internet browsers and
    I must say this blog loads a lot quicker
    then most. Can you recommend a good web hosting provider at a
    honest price? Many thanks, I appreciate it!

  3. Hey just wanted to give you a quick heads up. The text
    in your post seem to be running off the screen in Firefox.
    I’m not sure if this is a format issue or something to do with web browser compatibility
    but I figured I’d post to let you know. The design and style look great though!

    Hope you get the issue solved soon. Many thanks

  4. I have been exploring for a little bit for any high-quality articles or blog posts in this sort of area .
    Exploring in Yahoo I ultimately stumbled upon this site.
    Reading this info So i’m satisfied to exhibit that I’ve
    a very just right uncanny feeling I came upon just what I
    needed. I such a lot indisputably will make sure to don?t forget this website and provides it a glance on a constant basis.

  5. I really love your blog.. Great colors & theme.
    Did you create this site yourself? Please reply back as I’m hoping to create
    my very own blog and would love to know where you
    got this from or just what the theme is named. Cheers!

  6. #6 Coconut Oil Benefits 回复 | 引用 Post:2018-10-19 02:29

    Very good blog! Do you have any recommendations for aspiring writers?
    I’m planning to start my own site soon but I’m a little lost on everything.
    Would you suggest starting with a free platform like WordPress or go for a paid option? There are so many choices out there that I’m totally overwhelmed ..
    Any recommendations? Appreciate it!

  7. #7 Benefits of Coconut Oil 回复 | 引用 Post:2018-10-21 08:16

    Greetings, There’s no doubt that your site might be having web browser compatibility issues.
    When I take a look at your website in Safari, it looks fine however when opening in IE, it has some overlapping issues.
    I merely wanted to give you a quick heads up!
    Aside from that, excellent website!

  8. #8 descargar facebook 回复 | 引用 Post:2018-11-02 03:02

    Write more, thats all I have to say. Literally, it seems as though you relied on the video to make your point.
    You obviously know what youre talking about, why waste your intelligence on just posting videos to your site when you could be giving us
    something informative to read?

  9. #9 quest bars cheap 回复 | 引用 Post:2018-11-03 04:18

    I blog frequently and I truly appreciate your information. This article has truly peaked my
    interest. I’m going to take a note of your website and keep checking
    for new information about once a week. I opted in for your RSS feed too.

  10. #10 quest bars cheap 回复 | 引用 Post:2018-11-04 14:46

    Quest bars cheap fitnesstipsnew1 quest bars cheap 516999410492780544 quest
    bars cheap
    Ahaa, its good conversation about this paragraph here at this weblog, I have read all that, so at this time me also
    commenting here. Quest bars cheap fitnesstipsnew1 quest bars cheap 516999410492780544 quest bars cheap

  11. #11 Sling TV 回复 | 引用 Post:2018-11-10 20:00

    Hey there would you mind letting me know which hosting
    company you’re using? I’ve loaded your blog in 3 different browsers and I must say
    this blog loads a lot faster then most. Can you suggest a good web hosting provider at a honest price?

    Many thanks, I appreciate it!

  12. #12 Sling TV 回复 | 引用 Post:2018-11-11 23:37

    I was pretty pleased to find this site. I wanted to thank
    you for ones time just for this fantastic read!! I definitely appreciated every bit
    of it and i also have you saved as a favorite to check out
    new information in your site.

  13. #13 Sling TV 回复 | 引用 Post:2018-11-14 03:58

    I love your blog.. very nice colors & theme. Did you design this website
    yourself or did you hire someone to do it for you? Plz respond as I’m looking to design my
    own blog and would like to know where u got this from.
    kudos

  14. #14 Sling TV 回复 | 引用 Post:2018-11-15 01:23

    Every weekend i used to go to see this web site, for the reason that i want enjoyment, for the reason that this this website conations truly pleasant funny information too.

  15. Wow, that’s what I was looking for, what a data!
    present here at this weblog, thanks admin of this web page.

  16. bookmarked!!, I like your site!

  17. #17 his g 回复 | 引用 Post:2020-06-10 18:05

    Hi, I wish for to subscribe for this web site to get newest updates, so where can i do it please assist.

  18. #18 g year 回复 | 引用 Post:2020-06-10 20:51

    Hi there everyone, it’s my first go to see at this web site, and piece of writing is genuinely fruitful in support of me, keep
    up posting these types of articles or reviews.

  19. #19 just g 回复 | 引用 Post:2020-06-11 12:01

    Hey! I understand this is kind of off-topic but I had to ask.
    Does operating a well-established website like yours take a massive amount
    work? I’m brand new to operating a blog but I do write in my diary
    everyday. I’d like to start a blog so I can share my experience and thoughts online.

    Please let me know if you have any kind of recommendations or tips for
    new aspiring bloggers. Appreciate it!

  20. #20 there g 回复 | 引用 Post:2020-06-11 15:33

    I’m gone to tell my little brother, that he should also pay a visit this webpage on regular basis to obtain updated from latest
    gossip.

  21. #21 be g 回复 | 引用 Post:2020-06-12 11:00

    I know this if off topic but I’m looking into starting my
    own blog and was curious what all is required to get setup?

    I’m assuming having a blog like yours would cost a pretty penny?

    I’m not very web savvy so I’m not 100% positive. Any recommendations or advice would be
    greatly appreciated. Kudos

  22. #22 tinyurl.com 回复 | 引用 Post:2020-06-14 12:23

    Greetings! Very useful advice in this particular post! It’s the little changes that make the most significant changes.
    Many thanks for sharing!

  23. #23 lacantinadeisapori.com 回复 | 引用 Post:2020-06-14 12:40

    What i don’t understood is in reality how you are now not actually much
    more smartly-preferred than you might be right now. You’re very intelligent.
    You realize thus considerably when it comes to this matter, produced me
    individually consider it from so many varied angles.

    Its like women and men aren’t interested unless it’s
    something to do with Woman gaga! Your own stuffs excellent.
    Always deal with it up!

发表评论