我创建了一个带有CreateWindowW
函数的窗口(在注册windows类等之后)并且它工作正常,但是不显示窗口的标题。我可以用SetWindowTextW
函数解决问题,但我仍然想知道为什么它不能正常工作。
这是我的注册功能:
ATOM MainWindow::RegisterMainWindow(HINSTANCE hInstance)
{
WNDCLASSEXW wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = MainWndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APP_ICON));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = windowsClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassExW(&wcex);
}
我的创建功能......:
BOOL MainWindow::CreateMainWindow(HINSTANCE hInstance)
{
hwnd = CreateWindowW(windowsClass, L"FLY", WS_OVERLAPPEDWINDOW,
PublicMainClass::userData.MainWindow.left, PublicMainClass::userData.MainWindow.top,
PublicMainClass::userData.MainWindow.right, PublicMainClass::userData.MainWindow.bottom, nullptr, nullptr, hInstance, NULL/*this*/);
SetWindowTextW(hwnd, windowsTitle);
if (!hwnd)
{
return FALSE;
}
ShowWindow(hwnd, SW_NORMAL);
UpdateWindow(hwnd);
return TRUE;
}
而我的构造函数:
MainWindow::MainWindow(HINSTANCE hInstance)
{
PublicMainClass::userData.NewUser();
PublicMainClass::PublicMainClassConstructor(hInstance);
LoadStringW(hInstance, MAIN_WINDOW_CLASS, windowsClass, MAX_LOADSTRING);
LoadStringW(hInstance, APP_TITLE, windowsTitle, MAX_LOADSTRING);
}
这就是wWinMain函数调用类的方式:
MainWindow* mainWindow = new MainWindow(hInstance);
mainWindow->RegisterMainWindow(hInstance);
mainWindow->CreateMainWindow(hInstance);
那是我的窗口程序:
LRESULT CALLBACK MainWindow::MainWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_NCCREATE:
{
CREATESTRUCT *pcs = (CREATESTRUCT*)lParam;
//MainWindow* mainWindow = (MainWindow*)pcs->lpCreateParams;
//mainWindow->hwnd = hWnd;
SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG)pcs->lpCreateParams);
/*Taskbar *taskbar = new Taskbar();
taskbar->RegisterTaskbarWindow();
taskbar->CreateTaskbarWindow(hWnd);*/
/*WorkSpace *workspace = new WorkSpace();
workspace->RegisterWorkSpaceWindow();
workspace->CreateWorkSpaceWindow(hWnd);*/
return TRUE;
}
break;
case WM_CREATE:
{
ShowWindow(hWnd, SW_SHOW);
}
break;
case WM_CLOSE:
{
PostQuitMessage(0);
}
break;
case WM_DESTROY:
{
MainWindow *mainWindow = (MainWindow*)GetWindowLongPtr(hWnd, GWLP_USERDATA);
if (mainWindow) mainWindow->hwnd = 0;
return TRUE;
}
break;
case WM_LBUTTONDOWN:
{
MainWindow *mainWindow = (MainWindow*)GetWindowLongPtr(hWnd, GWLP_USERDATA);
POINT pointer;
pointer.x = GET_X_LPARAM(lParam);
pointer.y = GET_Y_LPARAM(lParam);
mainWindow->OnClk(&pointer);
return TRUE;
}
break;
case WM_RBUTTONDOWN:
{
MainWindow *mainWindow = (MainWindow*)GetWindowLongPtr(hWnd, GWLP_USERDATA);
POINT pointer;
pointer.x = GET_X_LPARAM(lParam);
pointer.y = GET_Y_LPARAM(lParam);
return TRUE;
}
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
问题是因为我在窗口的过程中处理了WM_NCCREATE
消息,所以它没有被DefWindowProc()
函数处理。应按如下方式处理WM_NCCREATE函数:
case WM_NCCREATE:
{
CREATESTRUCTW* create = (CREATESTRUCTW*)lParam;
SetWindowTextW(hWnd, create->lpszName);
return TRUE;
}