Draw text
Windows text is a GUI object. Each character is actually composed of many pixels. These points are displayed in the place of strokes, so that the characters will appear. That's why I say "Draw" characters, not write characters. Generally, you "Draw" strings in the customer area of your application (although you can also "Draw" outside the customer area "). In Windows, the "Draw" string method is very different from that in Dos. in Dos, you can think of the screen as a plane of 85x25, in Windows, because there are several applications on the screen at the same time, you must strictly follow the specifications. Windows does this by limiting each application to its customer zone. Of course, the size of the customer zone is variable and you can adjust it at any time.
Before you "Draw" a string in the customer area, you must obtain the size of your customer area from Windows, indeed, you cannot "Draw" anywhere on the screen as you would in DOS. You must be allowed by Windows before drawing, and Windows will tell you the size and font of the customer zone, color and other GUI object attributes. You can use these to "Draw" in the customer zone ".
What is "device environment" (DC? It is actually a Data Structure maintained internally by Windows. A "device environment" is connected to a specific device. Like printers and monitors. For a display, the "device environment" is connected to a specific window.
Some properties in "device environment" are related to plotting, such as color and font. You can change the default values at any time to save the default values for convenience. You can think of the "device environment" as a drawing environment for Windows, and you can change some default properties at any time as needed.
When the application needs to be drawn, you must get a "device environment" handle. There are usually several methods.
- Use BeginPaint in the WM_PAINT message
- Use GetDC in other messages
- CreateDC build your own DC
You must note that after processing a single message, you must release the "device environment" handle. Do not obtain the "device environment" handle in one message processing, but release it in another message processing.
When Windows sends the WM_PAINT message, it processes the drawing in the customer zone. Windows does not save the content in the customer zone, it uses the "re-painting" mechanism (for example, when the customer area is just overwritten by the customer area of another application), Windows will put the WM_PAINT message into the message queue of the application. The customer area of the redrawing window is the responsibility of each window. What you need to do is to know what to draw and what to draw in the WM_PAINT section of the window process.
Another concept that you must understand is "invalid region ". In Windows, the smallest Square area to be repainted is called "invalid area ". When Windows finds an "invalid region", it sends a WM_PAINT message to the application. During WM_PAINT processing, the window first obtains a struct about the drawing, including the coordinates of the invalid zone. You can call ininpaint to make the "invalid zone" valid. If you do not process the WM_PAINT message, at least call the default window processing function DefWindowProc, or call ValidateRect to make the "invalid zone" valid. Otherwise, your application will receive endless WM_PAINT messages. The following are the steps to respond to the message:
- Obtain the "device environment" Handle
- Draw customer zones
- Release the "device environment" Handle
Note: you do not need to explicitly make the "invalid zone" valid. This action is automatically completed by BeginPaint. You can call all the painting functions between BeginPaint and Endpaint. Almost all GDI functions require a "device environment" handle as a parameter.
Font color
Windows color system uses RGB values. R indicates red, G indicates green, and B Indicates blue. If you want to specify a color, you must assign an RGB value to the color. The RGB value ranges from 0 to 255. For example, if you want to obtain a pure red color, you must assign values to RGB (255,255,255, 0, 0). The value of pure white is ). From the examples below, you can see that it is not easy to use this digital-based color system, which requires you to have a good sense of mixed color and face color matching.
You can use the SetTextColor and SetBkColor functions to "Draw" the background color and character color, but you must pass a "device environment" handle and RGB value as the parameter. The COLORREF type is used to depict an RGB color. It is defined as follows:
Typedef dword colorref;
Typedef DWORD * LPCOLORREF;
The COLORREF type variable value depicts a color corresponding to the following hexadecimal format: 0x00bbggrr
You can use such a struct to describe.
RGB_value struct
{
Byte unused;
Byte blue;
Byte green;
Byte red;
};
The first byte is 0 and is always 0. The other three bytes represent blue, green, and red, which is in the opposite order of RGB. This struct is quite awkward to use. For COLORREF, we usually use macro RGB to assign values to it. Macro is defined as follows:
COLORREF RGB
(
BYTE byRed, // red component of color
BYTE byGreen, // green component of color
BYTE byBlue // blue component of color
);
You can call CreateFont and CreateFontIndirect to create your own font. The difference between the two functions is that the former requires you to pass a series of parameters, and then you only need to pass a pointer pointing to the LOGFONT structure. This makes the latter easier to use, especially when you need to frequently create fonts. In our example, CreateFont is enough to create a font. After this function is called, The created font handle is returned, and the handle is selected as the "device environment" to make it the current font, then all the functions that "Draw" text strings must pass the handle as a parameter when called.
Key instance code
// Function: WndProc (HWND, UINT, WPARAM, LPARAM) // objective: to process messages in the main window. /// WM_COMMAND-process application menu // WM_PAINT-draw the main window // WM_DESTROY-Send the Exit message and return /// TCHAR szText [] = _ T ("Win32 program, simple and powerful! "); TCHAR FontName [] = _ T (" script "); lresult callback WndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {int wmId, wmEvent; PAINTSTRUCT ps; HDC hDC; RECT stRect; HFONT hFont, hOldFont; COLORREF oldTextCol, oldBGCol; switch (message) {case WM_COMMAND: wmId = LOWORD (wParam ); wmEvent = HIWORD (wParam); // select switch (wmId) {case IDM_ABOUT: DialogBox (hInst, MAKEINTRESOURCE (IDD_ABOUTBOX), hWnd, About) from the analysis menu; bre Ak; case IDM_EXIT: DestroyWindow (hWnd); break; default: return DefWindowProc (hWnd, message, wParam, lParam);} break; case WM_PAINT: // you can think of the "device environment" as a drawing environment for Windows, and you can change some default properties at any time as needed. HDC = BeginPaint (hWnd, & ps); // get the device environment handle // TODO: add any drawing code here... getClientRect (hWnd, & stRect); DrawText (hDC, szText,-1, & stRect, DT_SINGLELINE | DT_CENTER | DT_VCENTER); // EndPaint (hWnd, & ps ); // hDC = BeginPaint (hWnd, & ps); hFont = CreateFont (24, 16, OEM_CHARSET, character, expiration, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SCRIPT, FontName ); hOldFont = (HFONT) SelectObject (hDC, hFont); // select the new font The current font of the device environment and returns the oldTextCol = SetTextColor (hDC, RGB (200,200, 50); // sets the Character Color of the device environment, returns the oldBGCol = SetBkColor (hDC, RGB (255,); // sets the background color of the device environment, and returns the previous background color TextOut (hDC, 0, 0, szText, sizeof (szText); // draw a string // restore the previous settings of the device environment, otherwise it will affect other characters SelectObject (hDC, hOldFont ); setTextColor (hDC, oldTextCol); SetBkColor (hDC, oldBGCol); EndPaint (hWnd, & ps); // release the device environment handle break; case WM_DESTROY: PostQuitMessage (0); break; default: return DefW IndowProc (hWnd, message, wParam, lParam);} return 0;}/*. PAINTSTRUCT stPs; RECT stRect; HDC hDC; these local variables are called by the GDI function in the WM_PAINT message. Hdc is used to store the "device environment" handle returned by calling BeginPaint. Ps is a PAINTSTRUCT data type variable. Generally, you do not need to use many of these values. It is passed to BeginPaint by Windows and passed to EndPaint after the painting is finished. Rect is a RECT struct type parameter, which is defined as follows: typedef struct tagRECT {LONG left; LONG top; LONG right; LONG bottom;} RECT; left and top are the coordinates of the upper left corner of the square. Right and bottom are coordinates in the lower right corner of the square. The coordinates in the upper left corner of the customer area are x = 0 and y = 0. In this way, the coordinates of x = 0 and y = 10 are under it. HDC = BeginPaint (hWnd, & stPs); GetClientRect (hWnd, & stRect); DrawText (hDC, szText,-1, & stRect, DT_SINGLELINE | DT_CENTER | DT_VCENTER ); endPaint (hWnd, & stPs); when processing the WM_PAINT message, you call the BeginPaint function and pass it a window handle and uninitialized PAINTSTRUCT parameters. After the call is successful, the "device environment" handle is returned in hDC. Next time, call GetClientRect to get the size of the customer zone, put the size in the rect, and pass it to DrawText. The DrawText syntax is as follows: WINUSERAPI int WINAPI DrawTextA (HDC hDC, LPCSTR lpString, int nCount, LPRECT lpRect, UINT uFormat); DrawText is a high-level call function. It can automatically handle such chores as line breaks and text placement in the middle of the customer zone. So you just need to focus on "Drawing" the string. In the next lesson, we will explain the lower-level function TextOut, which is used to format a text string in a square area. It uses the selected font, color, and background color. It handles line breaks to fit the square area. It returns the height of the text measured in the unit of device logic. Here the unit of measurement is pixel. Let's take a look at the function parameter hdc: "device environment" handle. LpString: The text string to be displayed. The text string must either end with NULL or indicate its length in nCount. NCount: the length of the text to be output. If it ends with NULL, this parameter must be-1. LpRect: pointer to the square area of the text string to be output. The square must be a cropping area, that is, the characters exceeding the area cannot be displayed. UFormat: Specifies how to display. We can use or to merge the following sign into one: DT_SINGLELINE: whether to display a single line. DT_CENTER: whether to center horizontally. DT_VCENTER: whether to center vertically. After painting, you must call EndPaint to release the "device environment" handle. Now, let's summarize the main points of "painting" text strings as follows: You must call BeginPaint and EndPaint respectively at the beginning and end; call all the painting functions between BeginPaint and EndPaint; if you re-draw the customer area in other message processing, you can have two options: (1) Use GetDC and ReleaseDC to replace BeginPaint and EndPaint; (2) calling InvalidateRect or UpdateWindow will invalidate the customer zone, which forces WINDOWS to put WM_PAINT into the application message queue, so that the customer zone is repainted. B. The CreateFont function generates a logical font that is as close as possible to the values specified in the parameter. This function is probably one of the most parameters in all Windows API functions. It returns a handle pointing to the logical font for calling the SelectObject function. HOldFont = (HFONT) SelectObject (hDC, hFont); after we get the handle pointing to the logical font, we must call the SelectObject function to select it as "device environment ", we can also call this function to select such GDI objects, such as colors, pens, and image brushes, into the "device environment ". This function returns a handle of the old "device environment. You must save the handle so that you can select it after "Drawing. After the SelectObject function is called, all the plotting functions are for this "device environment. SetTextColor (hDC, RGB (200,200, 50); SetBkColor (hDC, RGB (255,); we use macro RGB to generate colors, and then call SetTextColor and SetBkColor respectively. TextOut (hDC, szText, sizeof (szText); we call TextOut to "Draw" text strings with the fonts and colors we selected earlier in the customer zone. SelectObject (hDC, hOldFont); after we "Draw", we must restore the "device environment ". We must do this every time. */