首页 > 解决方案 > winAPI 中 L 前缀(LPCWSTR 类型转换)的问题

问题描述

我是 winAPI 的新手,遇到了一个我似乎无法解决的问题……谷歌也找不到解决方案。

我的程序有几个大小相似的按钮,所以我做了一个宏来隐藏所有的混乱。原来的宏是:

#define _BUTTON(s, x, y, v)    CreateWindowW(L"Button", (L)s, WS_VISIBLE | WS_CHILD, x, y, 75, 25, hWnd, (HMENU)v, 0, 0);

但是,“ L(s)”在有或没有括号的情况下都不起作用,在sor上L。我也尝试用L, LPCWSTR,WCHAR*等替换_T()...编译器错误总是一样的:"L (or LPCWSTR, etc) is not declared in this scope"虽然我认为它应该是...

现在我通过使用非 Unicode 解决了这个问题:

#define _BUTTON(s, x, y, v)    CreateWindow("Button", s, WS_VISIBLE | WS_CHILD, x, y, 75, 25, hWnd, (HMENU)v, 0, 0);

但我希望所有的窗口都支持相同的字符......问题出在哪里?

标签: cwinapiwin32guilpcwstr

解决方案


一种方法是 RbMm 提到的,例如:

#define Create_Button(s, x, y, v)     CreateWindowW(L"Button", L##s, WS_VISIBLE | WS_CHILD, x, y, 75, 25, hWnd, (HMENU)v, 0, 0);

另一种方法是使用通用方法:

#define Create_ButtonA(s, x, y, v)    CreateWindowA("Button", s, WS_VISIBLE | WS_CHILD, x, y, 75, 25, hWnd, (HMENU)v, 0, 0);
#define Create_ButtonW(s, x, y, v)    CreateWindowW(L"Button", s, WS_VISIBLE | WS_CHILD, x, y, 75, 25, hWnd, (HMENU)v, 0, 0);
#ifdef _UNICODE
#define Create_Button(s, x, y, v)     Create_ButtonW(s, x, y, v)
#else
#define Create_Button(s, x, y, v)     Create_ButtonA(s, x, y, v)
#endif

用法:

Create_Button(TEXT("name"),10,10,2);
Create_ButtonA("name",10,10,2);
Create_ButtonW(L"name",10,10,2);

推荐阅读