gpt4 book ai didi

c++ - 当从类构造函数中用于窗口类时,为什么winapi坚持使用PCSTR而不是PCWSTR?

转载 作者:行者123 更新时间:2023-12-03 07:38:12 24 4
gpt4 key购买 nike

我正在使用WINAPI编程一个简单的UI。尽管我有UNICODE定义,但仍坚持使用A版本的函数进行编译时遇到问题。我有两个版本的代码,一个工作正常,但不理想,而另一个封装在类的构造函数中。提供的代码有编译器错误,无法构建。
抱歉,如果问题很长且代码很充实,我们将不提供示例,不知道如何解释,但我希望太多总比总好。
在默认的Visual Studio 2017编译器上编译。
MainWindow类是从BaseWindow模板类继承的。
具有默认构造函数的工作版本如下:

MainWindow mainWindow;
if (!mainWindow.Create(L"Awesome Erpidzi", WS_VISIBLE | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX, 0, CW_USEDEFAULT, CW_USEDEFAULT, g_WindowWidth, g_WindowHeight))
{
return 0;
}
ShowWindow(mainWindow.Window(), ncmdShow);
和从父类创建函数:
BOOL Create(
PCWSTR lpWindowName,
DWORD dwStyle,
DWORD dwExStyle = 0,
int x = CW_USEDEFAULT,
int y = CW_USEDEFAULT,
int nWidth = CW_USEDEFAULT,
int nHeight = CW_USEDEFAULT,
HWND hWndParent = 0,
HMENU hMenu = 0
)
{
WNDCLASS wc = { 0 };

wc.lpfnWndProc = DERIVED_WINDOW_TYPE::WindowProc;
wc.hInstance = GetModuleHandle(NULL);
wc.lpszClassName = ClassName();

RegisterClass(&wc);

m_hwnd = CreateWindowEx(
dwExStyle, ClassName(), lpWindowName, dwStyle, x, y,
nWidth, nHeight, hWndParent, hMenu, GetModuleHandle(NULL), this
);

return (m_hwnd ? TRUE : FALSE);
}
和ClassName函数很简单:
PCWSTR  MainWindow::ClassName() const
{
return L"MainWindow";
}
但是,当我将函数打包到MainWindow类的构造函数中时,如下所示:
MainWindow::MainWindow(PCWSTR windowName, DWORD style, int width, int height)
{
Create(windowName, style, 0, CW_USEDEFAULT, CW_USEDEFAULT, width, height);
}
我收到编译错误

CreateWindowExA(DWORD,LPCSTR,LPCSTR,DWORD,int,int,int,int,HWND,HMENU,HINSTANCE,LPVOID)':cannot convert argument 2 from 'PCWSTR' to 'LPCSTR'


通过调试,我发现 wc.lpszClassName的Create函数需要LPCSTR类型。
我的问题是为什么当我将函数放到构造函数中时突然需要ASCII类型的字符串而不是UNICODE?我该如何解决它以适应宽字符串?
这是main,basewindow和mainwindow的完整cpp文件代码。
#ifndef UNICODE
#define UNICODE
#endif // !UNICODE

#include <Windows.h>
#include "IncludeAll.h"

const int g_WindowWidth = 1024;
const int g_WindowHeight = 768;

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR args, int ncmdShow)
{


MainWindow mainWindow;
if (!mainWindow.Create(L"Awesome Erpidzi", WS_VISIBLE | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX, 0, CW_USEDEFAULT, CW_USEDEFAULT, g_WindowWidth, g_WindowHeight))
{
return 0;
}
ShowWindow(mainWindow.Window(), ncmdShow);

MSG msg = { };
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return 0;
}

#ifndef WIN_UI_BASE_INCLUDE
#define WIN_UI_BASE_INCLUDE

#include <Windows.h>

template <class DERIVED_WINDOW_TYPE>
class BaseWindow
{
public:
static LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
DERIVED_WINDOW_TYPE *pThis = NULL;

if (uMsg == WM_NCCREATE)
{
CREATESTRUCT* pCreate = (CREATESTRUCT*)lParam;
pThis = (DERIVED_WINDOW_TYPE*)pCreate->lpCreateParams;
SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)pThis);

pThis->m_hwnd = hwnd;
}
else
{
pThis = (DERIVED_WINDOW_TYPE*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
}
if (pThis)
{
return pThis->HandleMessage(uMsg, wParam, lParam);
}
else
{
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
}

BaseWindow() : m_hwnd(NULL) { }

BOOL Create(
PCWSTR lpWindowName,
DWORD dwStyle,
DWORD dwExStyle = 0,
int x = CW_USEDEFAULT,
int y = CW_USEDEFAULT,
int nWidth = CW_USEDEFAULT,
int nHeight = CW_USEDEFAULT,
HWND hWndParent = 0,
HMENU hMenu = 0
)
{
WNDCLASS wc = { 0 };

wc.lpfnWndProc = DERIVED_WINDOW_TYPE::WindowProc;
wc.hInstance = GetModuleHandle(NULL);
wc.lpszClassName = ClassName();

RegisterClass(&wc);

m_hwnd = CreateWindowEx(
dwExStyle, ClassName(), lpWindowName, dwStyle, x, y,
nWidth, nHeight, hWndParent, hMenu, GetModuleHandle(NULL), this
);

return (m_hwnd ? TRUE : FALSE);
}

HWND Window() const { return m_hwnd; }


protected:

virtual PCWSTR ClassName() const = 0;
virtual LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam) = 0;

HWND m_hwnd;
};
#endif
#ifndef WIN_UI_MAINWINDOW_INCLUDE
#define WIN_UI_MAINWINDOW_INCLUDE

#include "WIN_UI_Base.h"

class MainWindow : public BaseWindow<MainWindow>
{
public:
MainWindow();
MainWindow(PCWSTR windowName,DWORD style, int width, int height);
PCWSTR ClassName() const;
LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam);

};

#endif
#include "../IncludeAll.h"

LRESULT MainWindow::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_DESTROY:
{
PostQuitMessage(0);
}
return 0;

case WM_PAINT:
{
PAINTSTRUCT ps;

HDC hdc = BeginPaint(m_hwnd, &ps);
ColourPicker& rColourPicker = UIHelpers::GetColourPicker();
const HBRUSH brush = rColourPicker.GetBrushWithColour(eColour::eWhite);
FillRect(hdc, &ps.rcPaint, brush);
EndPaint(m_hwnd, &ps);
}
return 0;

default:
return DefWindowProcW(m_hwnd, uMsg, wParam, lParam);
}
return TRUE;
}

MainWindow::MainWindow()
{
}

MainWindow::MainWindow(PCWSTR windowName, DWORD style, int width, int height)
{
Create(windowName, style, 0, CW_USEDEFAULT, CW_USEDEFAULT, width, height);
}

PCWSTR MainWindow::ClassName() const
{
return L"MainWindow";
}

最佳答案

似乎主要问题是您仅在一个源文件中而不是所有源文件中定义了UNICODE。在Visual Studio中避免此问题的一种方法是在项目设置中将UNICODE定义为预处理程序定义。
设置是Project Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions确保为所有配置而不是当前配置添加此设置。

关于c++ - 当从类构造函数中用于窗口类时,为什么winapi坚持使用PCSTR而不是PCWSTR?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64862614/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com