Windows Programming learning notes-first Windows program and wide character set (learn more)

Source: Internet
Author: User
Tags microsoft c


1. "Hello World" C language version

#include <stdio.h>int main (){           printf ("hello, world\n") ;           return 0 ;}

(From Mr. P's book)

/*------------------------------------------------------------------HelloMsg.c -- Displays "Hello, Windows 98!" in a message box    (c) Charles Petzold, 1998--------------------------------------------------------------------*/#include <windows.h>int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,                   PSTR szCmdLine, int iCmdShow){MessageBox (NULL, TEXT ("Hello, Windows 98!"), TEXT ("HelloMsg"), 0);return 0 ;}

It has an include declaration, a program entry point, a function call, and a return statement.


WINDOWS. H is a main file that contains other Windows header files. Some of these header files also contain other header files.The most important and basic of these header files are:


Program entry point

Just as the entry point in the C program is the function main, the entry point of the Windows program is WinMain, which always appears like this:

 
 

This is the first Windows program.

Classic Hello World.


3. About character sets

C language version:

 

 
 
 
 
 
 
 
 
 
Windows version:

 

Wide character

The wide character in C is based on the wchar_t data type. It is defined in several header files, including WCHAR. H., Such:

      

       

      

Note the uppercase letter L ("long") next to the first quotation mark 」). This tells the compiler to save the string by wide characters-that is, each character occupies 2 bytes. Generally, the pointer Variable p occupies 4 bytes, while the string variable requires 14 bytes-each character requires 2 bytes, and the end 0 requires 2 bytes.

   

, Sizeof (a) returns 14.Index Array a to obtain separate characters. The value of a [1] is the wide character "e", or 0x0065.

You can also use the L prefix before a single character to indicate that they should be interpreted as wide characters.. As follows:

 

Minor issues:


String handler for the wide character set:
size_t __cdecl strlen (const char *) ;        

The wcslen function is described as follows:

size_t __cdecl wcslen (const wchar_t *) ;        

At this time, we know that to obtain the length of a wide string, you can call

iLength = wcslen (pw) ; 

The function returns 6 Characters in the string. Remember, the character length of the character string is not changed after the character segment is changed to a wide character segment, but the length of the bit group is changed.

All the C execution period linked library functions you are familiar with have wide character versions. For example, wprintf is a wide character version of printf. These functions are described in WCHAR. H and in the header file containing the standard function description.


Then "L" Problem:

Now we will discuss the question of L in string text. If _ UNICODE identifier is defined, a macro called _ T is defined as follows:

#define __T(x) L##x        

 

 

Which macro is used in the Win32 console program depends on whether you prefer to be concise or detailed. Basically, the string TEXT must be defined in the _ T or _ TEXT macro as follows:

_TEXT ("Hello!")        

In this case, if _ UNICODE is defined, the string is interpreted as a combination of wide characters, otherwise it is interpreted as an 8-bit character string.


Finally:


As mentioned earlier, Microsoft C includes all common versions of the linked library functions that require string parameters in C Language execution periods. However, some of them are copied in Windows. For example, the following is a set of string functions defined by Windows. These functions are used to calculate the string length, copy the string, connect the string, and compare the string:

ILength = lstrlen (pString) ;    pString = lstrcpy (pString1, pString2) ;    pString = lstrcpyn (pString1, pString2, iCount) ;     pString = lstrcat (pString1, pString2) ;     iComp = lstrcmp (pString1, pString2) ;      iComp = lstrcmpi (pString1, pString2) ;     

These functions are the same as those in the C-Linked Library. If UNICODE identifiers are defined, these functions accept wide strings. Otherwise, only regular strings are accepted. The wide string version of The lstrlenW function can be executed in Windows 98.

Although Windows programs can use the link library for most C execution periods-in fact, many program writers prefer to use C memory management and file I/O functions instead of equivalent functions in Windows-Windows has no concept of standard input and standard output. In Windows, you can use fprintf instead of printf.

The good news is that other functions in the sprintf and sprintf series can still be used to display text.Apart from formatting the content and outputting it to the string buffer provided by the first parameter of the function, these functions have the same function as printfI. Then you can operate the string (for example, pass it to MessageBox ).

The SCRNSIZE program shown in program 2-1 shows how to implement the MessageBoxPrintf function. This function has many parameters and can orchestrate their formats like printf.

Program 2-1 SCRNSIZE. c/* --------------------------------------------------------------------------- SCRNSIZE. C -- Displays screen size in a message box (c) Charles Petzold, 1998 ---------------------------------------------------------------------- */# include <windows. h> # include <tchar. h> # include <stdio. h> int CDECL MessageBoxPrintf (TCHAR * szCaption, TCHAR * szFormat ,...) {TCHAR szBuffer [1024]; va_list pArgList; // The va_start macro (defined in STDARG. h) is usually equivalent to: // pArgList = (char *) & szFormat + sizeof (szFormat); va_start (pArgList, szFormat ); // The last argument to wvsprintf points to the arguments _ vsntprintf (szBuffer, sizeof (szBuffer)/sizeof (TCHAR), szFormat, pArgList ); // The va_end macro just zeroes out pArgList for no good reason va_end (pArgList); return MessageBox (NULL, szBuffer, szCaption, 0);} int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {int cxScreen, cyScreen; cxScreen = GetSystemMetrics (SM_CXSCREEN); cyScreen = GetSystemMetrics (SM_CYSCREEN); parse (TEXT ("ScrnSize "), TEXT ("The screen is % I pixels wide by % I pixels high. "), cxScreen, cyScreen); return 0 ;}















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.