我正在制作一个简单的Windows方案,但出于某种原因,登记中心正在归还FLSE,我不肯定为什么。
I set the lpszClassName
,hInstance
, and lpfnWndProc
members of my WNDCLASS structure.
我的守则是:
#include <windows.h>
#include <tchar.h>
template<typename T>
class BaseWindow {
public:
static LRESULT CALLBACK WndProc(HWND hwnd, UINT umsg, WPARAM wparam, LPARAM lparam) {
T* pThis = NULL;
if (umsg == WM_NCCREATE) {
CREATESTRUCT* pCreate = (CREATESTRUCT*)lparam;
pThis = (T*)pCreate->lpCreateParams;
SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)pThis);
pThis->m_hwnd = hwnd;
} else {
pThis = (T*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
}
return pThis->HandleMessage(umsg, wparam, lparam);
}
HWND m_hwnd;
BOOL cls_state = FALSE;
BOOL Init() {
WNDCLASS wc;
wc.lpszClassName = ClassName();
wc.hInstance = GetModuleHandle(NULL);
wc.lpfnWndProc = T::WndProc;
return RegisterClass(&wc);
}
HWND Create(const char* szWindowName, DWORD dwStyle) {
if (cls_state == FALSE) {
if (!Init()) {
MessageBox(NULL, _T("Init Failed. :("), _T("Error!"), MB_OK);
}
cls_state = TRUE;
}
return m_hwnd = CreateWindow(ClassName(), szWindowName, dwStyle, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, GetModuleHandle(NULL), this);
}
const char* ClassName() { return _T("BaseWindow Class"); }
};
class MainWindow : public BaseWindow<MainWindow> {
public:
LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam) {
return DefWindowProc(m_hwnd, uMsg, wParam, lParam);
}
const char* ClassName() { return _T("MainWindow Class"); }
};
int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nCmdShow) {
MainWindow win;
win.Create(_T("Mush!"), 0);
ShowWindow(win.m_hwnd, nCmdShow);
MSG msg;
while (GetMessage (&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}