English 中文(简体)
为什么是AfxMessageBox显示的chinese印特征
原标题:why is the AfxMessageBox Displaying chinese characters

该代码以印章显示。

char buffer[10]="hello";
AfxMessageBox((LPCTSTR)buffer);

而《刑法》则在国务厅展示。

AfxMessageBox(L"hello");

如果在我的法典中存在问题,有人可以告诉我,如果是这样的话,那是怎样把缓冲变量恰当地推给我。

最佳回答

The LPCTSTR type is a Long Pointer to a Transparent STRing. What s important here is the T, Transparent (at least I think the T stands for Transparent).

If your application is compiled as an ASCII application, all T types (like TCHAR) are redefined as their ASCII-counterpart. So TCHAR will become simply char.

If your application is compiled as Unicode, all T types are redefined as Unicode types. TCHAR becomes wchar_t.

所有Windows(和MFC)功能也是如此。 所有Windows功能均以2种变量、ASCII版本(如电文BoxA)和Unicode版本(例如电文BoxW)组成。 宣传 方框本身只不过是对电文BoxA或电文BoxW(取决于你如何汇编)的定义。

In your example, buffer is defined as a char-vector type, but you convert it to a pointer to a transparent type. I assume that your application is compiled in Unicode, so LPCTSTR is actually a "wchar_t *". So this cast is incorrect.

Prepending the "hello" string with L, tells the compiler to theat the constant "hello" as a Unicode string, which makes it the correct type to be passed to the Unicode version of AfxMessageBox.

问题回答

If you have _UNICODE and UNICODE flag defined in your compiler options then casting the string LPCTSTR will treat the string as an unicode string. In case windows, it will treat every 16 bytes as a single character and tries to find the corresponding unicode character. To make it display english character, use TCHAR while defining the array.





相关问题
Problem statically linking MFC libraries

I have a Visual Studio 6 workspace I m trying to convert to a Visual Studio 2008 solution. The output of said solution is a .dll. It has to be a .dll and it needs to statically link MFC as I can t ...

Switching to WPF. Is it time?

I m considering switching from MFC to WPF. My first concern is that there are too many users who don t have .NET with WPF installed yet. Can anybody point to a source containing the WPF penetration ...

Crash within CString

I am observing a crash within my application and the call stack shows below mfc42u!CString::AllocBeforeWrite+5 mfc42u!CString::operator=+22 No idea why this occuring. This does not occur ...

C# for UI, c++ for library

I have a numerical library coded in C++. I am going to make a UI for the library. I know some MFC. So one solution is to use MFC and make a native application. The alternative is C#. I know nothing ...

Search by using the keyboard in a list/grid - algorithm

I need to implement a custom search in a grid and I would like to find some user interface guidelines that explain the standard way to implement it. I mean this kind of search that is initiated by ...

MFC List Control

In MFC, I can edit the text of items in the list control but only for the first column by setting the Edit Labels to true. Now when I click the first column item to change its text, I m able to change ...

热门标签