自定义类处理消息循环HWND_MESSAGE

LRESULT CALLBACK CCustomMsgObject::__WindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
	CCustomMsgObject* pThis = NULL;

	if (uMsg == WM_NCCREATE) {
		LPCREATESTRUCT lpCS = reinterpret_cast<LPCREATESTRUCT>(lParam);
		pThis = static_cast<CCustomMsgObject*>(lpCS->lpCreateParams);
		ATLASSERT(NULL != pThis);
		::SetWindowLongPtr(hwnd, GWLP_USERDATA, reinterpret_cast<LONG>(pThis));
	} else {
		pThis = reinterpret_cast<CCustomMsgObject*>(::GetWindowLongPtr(hwnd, GWLP_USERDATA));
		if (NULL == pThis || pThis->m_hWnd != hwnd) {
			return ::DefWindowProc(hwnd, uMsg, wParam, lParam);
		}
		return pThis->HandleMessage(uMsg, wParam, lParam);
	}

	return ::DefWindowProc(hwnd, uMsg, wParam, lParam);
}

LRESULT CCustomMsgObject::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	if (uMsg == WM_COPYDATA) {
		if (OnCopyData((HWND)wParam, (COPYDATASTRUCT*)lParam)) {
			return S_OK;
		}
	}

	return ::DefWindowProc(m_hWnd, uMsg, wParam, lParam);	
}

void CCustomMsgObject::CreateMsgPumpWnd()
{
	if (m_hWnd != NULL) return;

	HINSTANCE hInst = GetModuleHandle(0);
	LPCTSTR lpWndClsName = _T("wndClsMsgPumpWnd");
	WNDCLASS wc = {0};
	wc.lpszClassName = lpWndClsName;
	wc.style = CS_NOCLOSE;
	wc.hInstance = hInst;
	wc.lpfnWndProc = __WindowProc;
	ATLVERIFY(RegisterClass(&wc));

	m_hWnd = CreateWindow(lpWndClsName, 0, 0, 0, 0, 0, 0, HWND_MESSAGE, 0, hInst, this);
}

你可能感兴趣的:(HWND_MESSAGE)