【问题标题】:Problem with the L prefix ( LPCWSTR typecast) in winAPIwinAPI 中 L 前缀(LPCWSTR 类型转换)的问题
【发布时间】:2020-05-05 16:13:34
【问题描述】:

我是 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)”在s 或L 上无论有无括号都不起作用。我也试过用LPCWSTR、WCHAR*、_T()等替换L……编译器错误总是一样的:"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);

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

【问题讨论】:

  • 与您的问题无关,但在 C 中,所有以下划线开头的符号后跟另一个下划线或大写字母(如_BUTTONS)都是保留和你不应该自己定义这样的符号。
  • CreateWindowW(WC_BUTTON, L##s, 例如
  • 最后是一种“解决”您的问题的方法:为什么不像第一个示例那样简单地使用CreateWindowW,而是让宏的用户传递正确类型的字符串? IE。将宏记录为第一个参数需要一个宽字符串。如果用户不阅读文档或只是不关心它,那么他们将得到构建错误。
  • 甚至使用#define echo(x) x和echo(L)##echo(s)
  • 参见例如this reserved identifier reference了解详情。

标签: c winapi win32gui lpcwstr


【解决方案1】:

一种方法是 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-16
    • 2016-06-21
    • 2013-04-07
    • 2011-11-17
    相关资源
    最近更新 更多