Windows desktop application development-string usage

Source: Internet
Author: User
Tags microsoft c

Original english on msdn

Windows native supports Unicode strings such as UI (User Interface) elements and file names. Unicode is the preferred character encoding, because it supports all character sets and languages, Windows uses UTF-16-encoded characters, each of which is a 16-bit value encoding, to distinguish it from an 8-bit ANSI character, UTF-16 characters are called wide characters. The Visual C ++ compiler has built-in data types.Wchart_tTo support wide characters, the following types are defined in the header file winnt. h:

typedef wchar_t WCHAR;

In the msdn sample code, two versions are displayed. To declare a wide character or string'L'Put in front:

wchar_t a=L’a’;wchar_t *str=L”hello”;

The following are types related to some other strings:

Type Definition

Explanation

Char

Char

Pstr/lpstr

Char *

Pcstr/lpcstr

Const char *

Pwstr/lpwstr

Wchar_t *

Pcwstr/lpcwstr

Const wchar_t

 

Unicode and ANSI Functions

When Microsoft launched windows that supports Unicode, it provided two sets of parallel APIs for transition, one for ANSI strings and the other for Unicode strings. For example, the following two functions set the text of the Form title bar:

  • SetwindowtextaUse an ANSI string
  • SetwindowtextwUses a unicode string

 

In the system, ANSI strings are converted to Unicode. In the Windows header file, a macro is defined to parse unicode preprocessing symbols to determine whether the ANSI or Unicode version is used.

#ifdef UNICODE#define SetWindowText setWindowTextW#else#define SetWindowText SetWindowTextA#endif

In msdn, the name in the function document isSetwindowstext, Even if it is a macro name rather than a real function name.

 

New applications should always call the Unicode version. Many languages in the world require Unicode. If you use an ANSI string, it cannot localize the application. The ANSI version is inefficient because the system must convert the ANSI string to Unicode during running. Depending on your preferences, You can explicitly call the Unicode version, such as setwindowtextw or macro. In the msdn example, macro is called, but these two forms are equivalent. Most of the newer Windows APIs only support the Unicode version but not the ANSI version.

 

Tchar

When the application needs to support WindowsNT and Windows95/Windows98/windowsme, it is quite useful to compile the same code into an ANSI or Unicode string based on the target platform. To this end, the Windows SDK provides a macro that maps strings to Unicode or ANSI. This depends on the Target Platform

Macro

Unicode

ANSI

Tchar

Wchar_t

Char

Text ("X ")

L "x"

"X"

 

Sample Code:

 

SetWindowText(TEXT(“My Application”));

Resolve to one of the following operations:

Setwindowtextw (L "my application"); // Unicode function setwindowtexta ("My application") for wide character strings; // ANSI Function

 

TcharAndTextMacros are not very useful today because Unicode should be used for all applications. However, you may see them in older code or the msdn sample code.

 

The header file of Microsoft C Runtime Library defines a group of similar macros. For example ,_TcslenResolvedStrlenOtherwise, it is parsedWcslen, This isStrlen.

#ifdef _NUICODE#define _tcslen wcslen#else#define _tcslen strlen#endif

 

Note: Some headers use the pre-processor character Unicode, and others use the underscore prefix _ Unicode. When you create a new project, two symbols are always defined by default.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.