STL string classes and Unicode and others

Source: Internet
Author: User

I want the user to hold the control key while double-clicking the program icon to start the program in a special way. However:: GetCommandLine and __ARGC have no reaction, with MFC in the ccommandlineinfo seems to be the case. Is there a way to solve this problem?

It's very simple. All you have to do is call getkeystate. When the current message you are processing is being sent, the function returns the status of the virtual key. This state may be bouncing, pressing, or pinning a noose. The lasso is used for caps locking (Caps) and conversion locks (Shift Lock), and they can be converted to states. For a generic key, such as a Control key (Vk_control), if the key is pressed, the status of the high position is 1.

Many applications use Control+f8 as a special key to start recovery mode. For example, if an application allows a user to customize a workspace, then control+f8 can restore it to the original default setting, but make sure that the user confirms it before restoring it. To do better, you can save the user's settings in a separate INI file so that users have the opportunity to recover them. Anyway, to check the control key at the start of the program, you can write as follows:

if (GetKeyState(VK_CONTROL)<0)
{
// enter special mode
}

Figure 1 gives an MFC based sample code snippet, which you can download from the link at the top of this article, which displays a message box and beeps if the user presses CTRL+F8 at the start of the program. If you just want to check the control key, you can ignore the test for the Vk_f8 key.

I often use the std::string class of the Standard Template Library (STL) in C + + programs, but I run into problems with Unicode. When using regular C-style strings, I can use TCHAR and _t macros, which can be compiled for Unicode or ASCII, but I always find that this ascii/unicode combination is difficult to use with the STL string class. Do you have any good suggestions?

Yes, once you know how TCHAR and _t work, the problem is simple. The basic idea is that TCHAR is either char or wchar_t, depending on the value of the _unicode:

// abridged from tchar.h
#ifdef _UNICODE
typedef wchar_t TCHAR;
#define __T(x) L ## x
#else
typedef char TCHAR;
#define __T(x) x
#endif

When you select the Unicode character set in the project setup, the compiler compiles with the _UNICODE definition. If you select MBCS (multibyte character set), the compiler will not have _unicode defined. Everything depends on the value of the _unicode. Similarly, each Windows API function that uses a character pointer will have a (ASCII) and a W (Wide/unicode) version, and the actual definition of these versions is based on the value of the _unicode:

#ifdef UNICODE
#define CreateFile CreateFileW
#else
#define CreateFile CreateFileA
#endif

Similarly, _tprintf and _tscanf correspond to printf and scanf. All versions with "T" use TCHARs instead of chars. So how to apply the above to the std::string? Very simple. The STL already has a wstring class defined with a wide character (defined in the xstring header file). Both string and wstring are template classes defined using typedef and, based on basic_string, can be used to create string classes of any character type. The following are the string and wstring defined by the STL:

// (from include/xstring)
typedef basic_string< char,
char_traits< char >, allocator< char > >
string;
typedef basic_string< wchar_t,
char_traits< wchar_t >, allocator< wchar_t > >
wstring;

Templates are parameterized by potential character types (char or wchar_t), so for the TCHAR version, all you have to do is use TCHAR to mimic the definition.

typedef basic_string< TCHAR,
char_traits< TCHAR >,
allocator< TCHAR > >
tstring;

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.