模拟MFC自定义窗口响应编辑框输入变化事件

模仿MFC自定义实现了窗口基类,实现了通用的窗口事件响应机制。

自定义宏
 

#define SORA_MSG_MAP_BEGIN(TheClass) \
	typedef TheClass ThisClass;\
	static SORA_MSG_MAP pMessageEntries[] = \
	{

#define SORA_MSG_MAP_END \
		{(UINT)0, (UINT)0, (WORD)0, (SORA_PMSG)0}\
	};\
	m_pMessageEntries = &pMessageEntries[0];

#define MSG_COMMAND(msg, msgFunc)\
	{WM_COMMAND, \
	(UINT)msg, \
	(WORD)0, \
	(SORA_PMSG)&ThisClass::msgFunc},

#define MSG_COMMAND_EX(msg, controlid, msgFunc)\
	{WM_COMMAND, \
	(UINT)msg, \
	(WORD)controlid, \
	(SORA_PMSG)&ThisClass::msgFunc},

在窗口配置控件的事件响应时,如下配置:

void CListFilesDlg::InitMessageMap()
{
	SORA_MSG_MAP_BEGIN(CListFilesDlg)
	MSG_COMMON(WM_INITDIALOG, OnInitDialog)
	MSG_COMMAND(IDOK, OnOK)
	MSG_COMMAND(IDB_OPEN, OnOpen)
	MSG_COMMAND(IDCANCEL, OnCancel)
	MSG_NOTIFY(NM_DBLCLK, IDC_LIST_FILES, OnDbClickList)
	MSG_COMMAND_EX(EN_UPDATE, IDC_EDIT_FIND_FILE, OnInputChange)
	SORA_MSG_MAP_END
}

其中,IDC_EDIT_FIND_FILE为编辑框的ID,OnInputChange函数响应其输入变化事件。

在msdn中关于事件EN_UPDATE的描述如下:

EN_CHANGE notification code

  • ‎2018‎年‎05‎月‎31‎日
  • 2 分钟阅读时长

本文内容

  1. Parameters
  2. Remarks
  3. Requirements
  4. See also

Sent when the user has taken an action that may have altered text in an edit control. Unlike the EN_UPDATE notification code, this notification code is sent after the system updates the screen. The parent window of the edit control receives this notification code through a WM_COMMAND message.

C++复制

EN_CHANGE

    WPARAM wParam;
    LPARAM lParam;

Parameters

wParam

The LOWORD contains the identifier of the edit control. The HIWORD specifies the notification code.

lParam

A handle to the edit control.

wParam的低位表示空间的标识,高位标识事件的code。

所以,在窗口的时间循环中,处理EN_CHANGE事件,代码如下:

			if (nMsg == WM_COMMAND)
			{
				if (m_pMessageEntries[i].nControlID == 0) {
					if (m_pMessageEntries[i].nMsgID == wParam)
					{
						bFound = TRUE;
						break;					
					}
					if (HIWORD(wParam)  ==  EN_SETFOCUS && 
						m_pMessageEntries[i].nMsgID == EN_SETFOCUS) {
							bFound = TRUE;
							break;
					}
				} else if (m_pMessageEntries[i].nControlID == LOWORD(wParam)) {
					if ((UINT)(m_pMessageEntries[i].nMsgID)	== HIWORD(wParam))
					{
						bFound = TRUE;
						break;
					}
				}
			}

以上即可模拟MFC捕获编辑框的输入变化消息。

猜你喜欢

转载自blog.csdn.net/jiangbb8686/article/details/88617671