c - 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)
”在有或没有括号的情况下都不起作用,在s
or上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);
但我希望所有的窗口都支持相同的字符......问题出在哪里?
解决方案
一种方法是 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);
推荐阅读
- r - 使用 httr 连接到特定的 IP 地址
- c# - ASP.NET MVC:将查询数据传输到控制器中的模型类对象
- javascript - 如何操作 Dom 只选择标签内的文本内容?
- vb.net - AppendAllText 不会将 textbox1.text 中的新行保存到 .txt 文件中
- python - 两个 2 秩张量的 TensorFlow 外积
- java - 电话号码到java中多个数字的单词的功能
- rest - 格式化日期以响应 POST REST API
- c# - 如何在我们单击游戏对象的同一位置实例化粒子系统?
- c# - StackExchange.Redis.RedisConnectionException:无法连接到 redis 服务器;连接超时
- html - 如果父级兄弟的子级具有指定类,则 CSS 设置元素样式