为modaldialogbox 实现pretranslatemessage

atl 中modaldialogbox实现的CDialogImpl 是没有PreTranslateMessage的,就算增加对应实现也是没有作用的

原因:PreTranslateMessage是框架部分的代码并非系统部分modaldialogbox的独立消息循环并不能调用PreTranslateMessage


解决方法:设置系统钩子捕获消息


static HHOOK g_hHook_GetMessage = 0;
static DWORD g_hHook_GetMessage_TargetThreadId = 0;

BOOL g_PreTranslateMessage(MSG* pMsg)
{
	if (g_pMessageLoop)
	{
		return g_pMessageLoop->PreTranslateMessage(pMsg);
	}
	return FALSE;
}

static LRESULT CALLBACK s_MessageProc(int nCode, WPARAM wParam, LPARAM lParam)
{
	if (nCode < 0)
	{
		return CallNextHookEx(g_hHook_GetMessage, nCode, wParam, lParam);
	}

	if(
		MSGF_DIALOGBOX == nCode ||
		MSGF_MENU == nCode ||
		MSGF_SCROLLBAR == nCode
		)
	{
		MSG* pMsg = (MSG*)lParam;
		if (pMsg)
		{
			if (g_PreTranslateMessage(pMsg))
			{
				return TRUE;
			}
		}
	}

	return CallNextHookEx(g_hHook_GetMessage, nCode, wParam, lParam);
}

void InstallPreTranslateMessage(HINSTANCE hInstance,DWORD dwThreadId)
{

	//HHOOK SetWindowsHookEx(int idHook,
	//	HOOKPROC lpfn,
	//	HINSTANCE hMod,
	//	DWORD dwThreadId
	//	);

	g_hHook_GetMessage = ::SetWindowsHookEx(WH_MSGFILTER, s_MessageProc, hInstance, dwThreadId);





}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章