Use of loadstring

Source: Internet
Author: User
Tags drawtext

Use of loadstring



If the program involves multiple languages, we cannot use drawtext (HDC, l "test",...) when writing on the screen ",...) instead, use the loadstring function to load string resources from the resource. In this way, you only need to replace the string resources when changing the language, without modifying the code.

The following is the prototype of the loadstring function:

Int Loadstring (

Hinstance Hinstance,

Uint UID,

Lptstr Lpbuffer,

Int Cchbuffermax

);

Parameter description:

Hinstance:
The instance handle of the resource.

NID: ID of the string resource.

Lpbuffer: the buffer used to store strings.

Cchbuffermax: buffer size. If cchbuffermax is greater than the string length, the string will be truncated and ended with/0. If the string length is greater than or equal to cchbuffermax, the program will crash due to buffer overflow.

For example, the following code: (ids_str_test = l "under test ")

Wchar temp [2]; <br/>: loadstring (null, ids_str_test, temp, sizeof (temp); <br/>: drawtext (HDC, temp, -1, & rect, dt_center | dt_vcenter); <br/>



This program will crash due to buffer overflow.

Loadstring also has a usage. You do not need to pass in the buffer, but directly set lpbuffer to null, and then forcibly convert the return value to lpwstr to obtain the pointer to the resource file. Because it is directly directed to the resource file, therefore, the obtained string is read-only.

Usage: (ids_str_test = l "testing ")

Lpwstr lpstr = (lpwstr): loadstring (null, ids_str_test, null, 0); <br/>:: drawtext (HDC, lpstr,-1, & rect, dt_center | dt_vcenter); <br/>



However, before using this method, we must add the-n flag to the resource compiler to ensure that the resulting string is ended with "/0. The settings in vs2005 are as follows:

 

 

 

In fact, the cstring function is almost encapsulated for us. We can also directly call the cstring member function to load the resource string.

Cstring s; <br/> S. loadstring (ids_str_test );

 

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.