爲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);





}


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