c++ - c++ winapi 代码块有效,Visual Studio 社区无效
问题描述
在代码块中,它可以正常工作。但在 Visual Studio 中,它说:
类型的 C++ 参数与类型 LPCWSTR 的参数不兼容
这是我的代码:
#if defined(UNICODE) && !defined(_UNICODE)
#define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
#define UNICODE
#endif
#include <windows.h>
int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrev,LPSTR lpCmdLine, int iCmdShow){
int s = MessageBox(NULL,"Hi This is my First c++ GUI","Win32 API Say:",6);
if(s == 10){
MessageBox(NULL,"Hi This is my First c++ GUI","Win32 API Say:",6);
}else if(s == 11){
MessageBox(NULL,"Yes GO On","Win32 API Say:",MB_ICONINFORMATION);
}else{
MessageBox(NULL,"GoodBey !!","Win32 API Say:",MB_OK);
}
return 0;
}
解决方案
您正在使用 的TCHAR
版本MessageBox()
,它实际上是一个预处理器宏,分别映射到MessageBoxW()
或MessageBoxA()
取决于是否UNICODE
定义。
在 CodeBlocks 的情况下,如果未定义它就可以工作UNICODE
,因此MessageBox()
映射到MessageBoxA()
,它需要LPCSTR
( const char *
) 字符串作为输入。您正在传递char
基于 - 的字符串文字,所以一切都很好。
在 Visual Studio 的情况下,如果UNICODE
定义了 is 则失败,因此MessageBox()
映射到MessageBoxW()
,它需要LPCWSTR
( const wchar_t *
) 字符串作为输入。由于您传递的是char
基于 - 的字符串文字,并且char*
不能隐式转换为wchar_t*
,因此会出现错误。
如果您要继续使用TCHAR
功能1,则应将字符串文字包装在TEXT()
预处理器宏中,以根据编译器设置使它们使用MessageBox()
(或任何其他TCHAR
基于 API)期望的相同字符编码。
1:你真的不应该依赖TCHAR
现代代码。它的存在只是为了向后兼容旧代码,早于微软推动将 Win9x/ME 应用程序迁移到 NT4+/Win2K/XP 上的 Unicode。 TCHAR
被发明来帮助此类代码从 ANSIchar
字符串迁移到 Unicodewchar_t
字符串。
此外,您也不应该依赖“幻数”。它使代码更难阅读和理解。请改用预定义的命名常量。您已经在第 3 次MB_ICONINFORMATION
和MB_OK
第 4MessageBox()
次调用MessageBox()
.
试试这个:
#if defined(UNICODE) && !defined(_UNICODE)
#define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
#define UNICODE
#endif
#include <windows.h>
int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrev,LPSTR lpCmdLine, int iCmdShow){
int s = MessageBox(NULL, TEXT("Hi This is my First c++ GUI"), TEXT("Win32 API Say:"), MB_CANCELTRYCONTINUE);
if(s == IDTRYAGAIN){
MessageBox(NULL, TEXT("Hi This is my First c++ GUI"), TEXT("Win32 API Say:"), MB_CANCELTRYCONTINUE);
}else if(s == IDCONTINUE){
MessageBox(NULL, TEXT("Yes GO On"), TEXT("Win32 API Say:"), MB_ICONINFORMATION);
}else{
MessageBox(NULL, TEXT("GoodBey !!"), TEXT("Win32 API Say:"), MB_OK);
}
return 0;
}
也就是说,您根本不应该依赖TCHAR
API,而是根据需要直接调用MessageBoxA()
或MessageBoxW()
(或其他窄/宽 API),例如:
#if defined(UNICODE) && !defined(_UNICODE)
#define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
#define UNICODE
#endif
#include <windows.h>
int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrev,LPSTR lpCmdLine, int iCmdShow){
int s = MessageBoxA(NULL, "Hi This is my First c++ GUI", "Win32 API Say:", MB_CANCELTRYCONTINUE);
if(s == IDTRYAGAIN){
MessageBoxA(NULL, "Hi This is my First c++ GUI", "Win32 API Say:", MB_CANCELTRYCONTINUE);
}else if(s == IDCONTINUE){
MessageBoxA(NULL, "Yes GO On", "Win32 API Say:", MB_ICONINFORMATION);
}else{
MessageBoxA(NULL, "GoodBey !!", "Win32 API Say:", MB_OK);
}
return 0;
}
#if defined(UNICODE) && !defined(_UNICODE)
#define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
#define UNICODE
#endif
#include <windows.h>
int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrev,LPSTR lpCmdLine, int iCmdShow){
int s = MessageBoxW(NULL, L"Hi This is my First c++ GUI", L"Win32 API Say:", MB_CANCELTRYCONTINUE);
if(s == IDTRYAGAIN){
MessageBoxW(NULL, L"Hi This is my First c++ GUI", L"Win32 API Say:", MB_CANCELTRYCONTINUE);
}else if(s == IDCONTINUE){
MessageBoxW(NULL, L"Yes GO On", L"Win32 API Say:", MB_ICONINFORMATION);
}else{
MessageBoxW(NULL, L"GoodBey !!", L"Win32 API Say:", MB_OK);
}
return 0;
}
推荐阅读
- ruby-on-rails - 如何在 rspec rails 中测试 if else 条件
- r - 轴标签的重叠
- angular - 从另一个组件更改组件属性
- flutter - 如何从网站点击按钮启动 PWA?
- javascript - 无法在角度 7 中触发文本区域的 Enter 键事件
- c++ - 并非所有调用都在运行
- java - 为什么我无法在 Spring Boot 应用程序中使用 @Getter 和 @Setter Annotation 访问 get 和 set 方法?|| 龙目岛
- hazelcast - Hazelcast 基于 HTTP 的健康检查行为
- visual-studio-code - 在 VSCode Latex Workshop 中指定要编译的文件
- android - 将 gradle 构建工具从 3.6.1 升级到 4.1.0 时,特权应用程序的 dlopen 失败