以下使用 Unicode 字符集的书籍示例 [英] following book examples using Unicode Character Set

查看:26
本文介绍了以下使用 Unicode 字符集的书籍示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读一本名为使用 DirectX 9.0c 进行 3D 游戏编程简介:着色器方法"的书,我正在按照那里的代码进行操作,但该应用程序使用了多字节字符集,我从某处读到它不是使用它的好习惯,并且在创建窗口时出现错误.这是我出错的代码.

I'm reading a book called "Introduction to 3D Game Programming with DirectX 9.0c: A Shader Approach" and I was following the codes there but the application used Multi-Byte Character Set and I read from somewhere that it's not a good practice to use that and im having error when creating a window. here is the code that im having error.

mhMainWnd = CreateWindow(L"D3DWndClassName", mMainWndCaption.c_str(), WS_OVERLAPPEDWINDOW, 
            GetSystemMetrics(SM_CXSCREEN)/2 - width/2, 
            GetSystemMetrics(SM_CYSCREEN)/2 - height/2,
            R.right, R.bottom, 0, 0, mhAppInst, 0); 

那么错误是:

error C2664: 'CreateWindowExW' : cannot convert parameter 2 from 'const char [16]' to 'LPCWSTR'

希望有人能帮我

推荐答案

您听说 Unicode 优于 ANSI/MBCS 是完全正确的.所有新的 Windows 代码都应编写为使用 Unicode.为了实现这一点,您必须确保两件事:

What you heard about the preferability of Unicode over the ANSI/MBCS is entirely correct. All new Windows code should be written to work with Unicode. In order to make this happen, you have to ensure two things:

  1. UNICODE_UNICODE 符号都需要全局定义,以确保调用 API 函数的 Unicode 版本,即使您忘记了 W 后缀.

  1. Both the UNICODE and _UNICODE symbols need to be defined globally to ensure that the Unicode versions of the API functions are called, even if you forget the W suffix.

您可以在预编译头文件的顶部执行此操作

You can either do this at the top of your precompiled header

#define UNICODE
#define _UNICODE

或在 Visual Studio 中项目的属性"窗口中.只需将这两个值添加到列表中即可.

or in your project's Properties window within Visual Studio. Simply add both of the values to the list.

您的所有字符串(文字和其他)都必须是 Unicode 字符串.

对于文字,您可以通过在它们前面加上 L 来实现这一点,就像您在示例中所做的那样:L"D3DWndClassName"

With literals, you accomplish this by prefixing them with L, just as you've done in the example: L"D3DWndClassName"

对于在运行时分配的字符串,您需要使用 wchar_t 类型.由于您使用的是 C++,您显然应该使用字符串类,而不是像在 C 中那样使用原始字符数组.因此您需要使用字符串类,将字符串中的字符视为 wchar_t.这将是 std::wstring 或 MFC/ATL/WTL 的 CStringW 类.

With strings that are allocated at runtime, you need to use the wchar_t type. Since you're using C++, you should obviously be using a string class rather than raw character arrays like you would in C. So you need to use a string class that treats the characters in the string as wchar_t. This would either be std::wstring or MFC/ATL/WTL's CStringW class.

看起来您已经完成了大部分工作.罪魁祸首是mMainWndCaption.c_str().您正在使用 std::string(它返回一个以 nul 结尾的 char 数组)而不是 std::wstring(它返回一个wchar_ts 的空终止数组).

It looks like you've got most of this down already. The culprit is mMainWndCaption.c_str(). You are using std::string (which returns a nul-terminated array of chars) instead of std::wstring (which returns a nul-terminated array of wchar_ts).

这篇关于以下使用 Unicode 字符集的书籍示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆