问题描述
HWND wndHandle; //global variable
// code snipped
WNDCLASSEX wcex;
// code snipped
wcex.lpszClassName = (LPCWSTR) "MyTitleName";
// code snipped
wndHandle = CreateWindow(
(LPCWSTR)"MyTitleName", //the window class to use
(LPCWSTR)"MyTitleName", //the title bar text
...
...
我跟着一个Win32窗口应用程序的教程上面的代码用于设置窗口屏幕的标题栏的名称编译器喊我:不能从'const char [12]'转换为'LPCWSTR '所以没关系,我用(LPCWSTR)打字我的字符串MyTitleName,一切都编译得很好,但在运行时,窗口标题的标题是汉字,我尝试改变字符串,字符总是根据我的字符串不断变化我使用XP Visual C ++ 2008速成版和我有英语(美国)作为非unicode程序的设置,我不知道,字符串如何成为中文? / p>
I am following a tutorial for Win32 Window application. The code above is used to set the name of the title bar of the window screen. The compiler yells at me : "cannot convert from 'const char [12]' to 'LPCWSTR'" so okay, I typecasted my string "MyTitleName" with (LPCWSTR), and everything compiled just fine. However, during runtime, the title of the window screen turns out to be Chinese characters. I tried change the string around and the Chinese characters always change according to my string somehow. I am using XP Visual C++ 2008 Express Edition and I got English (United States) as a setting for non-unicode programs. I don't get it. How come the string become Chinese?
推荐答案
您的应用程序正在编译为unicode应用程序(这在项目设置中定义)。这意味着传递给Windows API函数的字符串需要是宽字符字符串,如下所示: LMyTitleName
。你不能转换到LPCWSTR,因为它不会实际改变字符串类型,它只是试图传递字符串作为它不是东西。
Your application is being compiled as a unicode application (this is defined in the project settings). This means that strings you pass to Windows API functions need to be wide-character strings, specified like this: L"MyTitleName"
. You can't cast to LPCWSTR because that won't actually change the string type, it will just try to pass the string off as something it isn't.
代码应该工作:
wcex.lpszClassName = L"MyTitleName";
// code snipped
wndHandle = CreateWindow(
L"MyTitleName", //the window class to use
L"MyTitleName", //the title bar text
...
如果要使用原始代码教程而不修改它,可以禁用unicode模式:在项目属性中转到常规选项卡,并将字符集
设置为字节字符集
。不要对有可能支持其他语言的任何程序执行此操作。
If you want to use the original code from the tutorial without modifying it, you can disable unicode mode: In the project properties go to 'General' tab, and set Character Set
to Use Multi-Byte Character Set
. Don't do this for any program which might have to support additional languages someday.
这篇关于创建一个带有英文标题栏的Win32窗口应用程序,但标题栏变成中文。怎么来的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!