The program described in the previous chapter only creates one window, but it is not used to display other information except the window name displayed in the window title bar. Applications can display text and graphic information in the user area of the window. This chapter only describes how to display text lines. Through the program examples in this chapter, we will introduce several Windows functions for text output and the concepts of valid and invalid rectangle areas. This section also describes the basic concepts of Device objects and font sizes. Here, we will introduce several important Windows messages. Applications can use these messages to initialize and terminate programs and complete application tasks.
2.1. Display Information
The following program displays "Hello, Welcome to Windows" in the upper left corner of the user area ".
// 2-1.c display information
# Include <windows. h>
Lresult callback WndProc (HWND, UINT, WPARAM, LPARAM );
Int PASCAL WinMain (
HINSTANCE hInstance, // Application instance handle
HINSTANCE hPrevInstance, // Handle of the previous instance of the application
LPSTR lpszCmdLine, // Command line parameter string
Int nCmdShow) // How to display the window during initialization
{
Char szAppName [] = "DispText ";
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
If (! HPrevInstance ){
// This instance is the first instance of the program, registration window class
Wndclass. style = CS_VREDRAW | CS_HREDRAW;
Wndclass. lpfnWndProc = WndProc;
Wndclass. cbClsExtra = 0;
Wndclass. cbWndExtra = 0;
Wndclass. hInstance = hInstance;
Wndclass. hIcon = LoadIcon (hInstance, IDI_APPLICATION );
Wndclass. hCursor = LoadCursor (NULL, IDC_ARROW );
Wndclass. hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH );
Wndclass. lpszMenuName = NULL;
Wndclass. lpszClassName = szAppName;
If (! RegisterClass (& wndclass ))
// If the registration fails
Return FALSE;
}
// Create a window object for each instance
Hwnd = CreateWindow (
SzAppName,
"Display Text ",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL,
NULL,
HInstance,
NULL );
ShowWindow (hwnd, nCmdShow );
UpdateWindow (hwnd );
While (GetMessage (& msg, NULL, 0, 0 )){
TranslateMessage (& msg );
DispatchMessage (& msg );
}
Return msg. wParam;
}
Lresult callback WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
Char msg [] = "Hello, Welcome to Windows! ";
PAINTSTRUCT ps;
HDC hDC;
Switch (message)
{
Case WM_PAINT:
HDC = BeginPaint (hwnd, & ps );
TextOut (hDC, 0, 0, msg, sizeof (msg)-1 );
EndPaint (hwnd, & ps );
Return 0;
Case WM_DESTROY:
PostQuitMessage (0 );
Return 0;
}
Return DefWindowProc (hwnd, message, wParam, lParam );
}
First, we will introduce the Windows data types used in this program, then introduce the Windows functions of these types, and finally introduce the meaning of the message WM_PAINT.
HDC is in Windows. A data type defined in h, similar to HWND, is a handle used to identify a device object. Many drawing functions require this handle, which plot on the object identified by this handle.
The information contained in the PAINTSTRUCT structure variables can be used to redraw the user area of the window. The following definition only gives the domain that the application cares about, other excluded domains are used by Windows.
Typedef struct tagPAINTSTRUCT {
HDC hdc; // Display the handle of the device object
BOOL fErase; // If it is non-zero. Indicates that the user background has been repainted. Otherwise, the background has not been repainted.
RECT rcPaint; // Specify the coordinates in the upper-left corner and lower-right corner of the rectangle that requires re-painting (coloring ).
//......
} PAINTSTRUCT;
Description of the RECT type is as follows:
Typedef struct tagRECT {
Int left; // X coordinate value in the upper left corner of the rectangle
Int top; // Y coordinate value in the upper left corner of the rectangle
Int right; // X coordinate value in the upper-right corner of the rectangle
Int bottom; // Y coordinate value in the upper-right corner of the rectangle
} RECT;
The BeginPaint () function uses a window handle and a pointer to a PAINTSTRUCT-type variable as a parameter. In addition to filling the fields of the variables pointed to by the pointer, at the same time, a display device is used to draw a user area from Windows. This function returns the handle of the device object. With this handle, the application can draw user zones at will. The EndPaint () function is the same as the parameter used by the BeginPaint () function. It must be paired with the BeginPaint () function and must be used in the WM_PAINT message at the same time. The function EndPaint () has two functions. The first is to return the display device object borrowed by BeginPaint. Windows manages five common Display Device objects, which are used by the provisioner for text and graphic output on the display device. To draw a user area of a window object, it must first borrow a display device object from Windows. The public display device object is limited. Therefore, after the user area of the window object is drawn, the borrowed device object must be returned in time, to use the public display device object for other objects. The general principle is that the borrowed display device object should be returned before returning from the window function. The second function of EndPaint () is to eliminate the WM_PAINT message in the application message queue. We will discuss the WM_PAINT message separately later.
The function TextOut () uses the display device object borrowed by BeginPaint () to display a message in the user area. Table 2-1 describes how to use the function.
Table 2-1 TextOut Function
Chinamoocs |
This function draws a string using the selected font. The x and y parameters are the coordinates in the upper left corner of the first character in the string. |
Original Type |
BOOL TextOut( |
|
HDC hDC |
Handle of the device object |
Int x, |
Logical x coordinate value of the string start point (relative to the upper left corner of the user zone) |
Int y, |
Logical y coordinate value of the string start point (relative to the upper left corner of the user zone) |
LPSTR lpString, |
String to be drawn |
Int nCount |
Number of characters to be drawn |
); |
|
|