"Windows programming" learning notes (four)

zhaozj2021-02-16  46

Chapter 4 Output Text

Need very to know a concept - CLIENT AREA: The entire application window is not subject to the title bar, window border, and optional menu bar, toolbar, status bar, and scroll bar. In Windows, we can draw text and graphics on the customer area of ​​the window, and when the customer area of ​​the window is changed, Windows cannot retain the original drawn text and graphics. If you want to restore the original text and graphics, you must re-draw, This process is called refresh. If Windows sends a WM_PAINT message to the window, the window's client area needs 'refresh'. When will Windows send WM_PAINT messages to the window, or when is the customer area of ​​the window changed? Mainly a few cases: 1) When the user moves the window or the display window, the previously hidden area is re-visible; 2) The user changes the size of the window. 3) The program uses the ScrollWindow or ScrollDC function to scroll part of the client area; 4) The program explicitly generates WM_PAINT messages using the InvalIDateRect or InValidaterGn function. Released; 7) Display Tool Tips; 8) The mouse cursor moves in the client area; 9) The icon is homged in the customer area. The above cases are actually made to enable all or some of the original text and graphics information of the window customer area, which is overwritable ', changing the' invalid 'window customer area, when the all or part of the window client area' is invalid ' When Windows sends a WM_PAINT message to the window, it is the window 'valid', and the window will be resemble the window. Draw on the window of the window, use the Windows Graphics Device Interface (GDI) function, the DrawText used by the previous chapter is a GDI function, which can draw the corresponding line of words in the window. Almost each graphics device interface function requires a device environment (DC: Device Context) handle as a parameter, DC is a data structure required to save the plot (such as text font, text color, cable thickness, etc.), we You can change these properties with some GDI functions. We only get the DC handle to draw text or graphics on the screen. There are two methods for obtaining a DC handle. One is when processing the WM_PAINT message (there is an invalid rectangle (window)), use BeginPaint and Endpaint two functions: Case WM_Paint: HCD HDC; // Defines a DC handle variable PainTstruct PS; HDC = BeginPaint (hwnd, & ps ); Make the invalid window becomes effective, and obtain a DC handle [use GDI function] endpaint (hwnd, & ps); release the DC handle must be done in the process of processing the WM_PAINT message, otherwise the program will be wrong! This is because the BeginPaint function is the window 'valid', and then you can draw the window.

If there is no BeginPaint function, the window is still 'invalid', and Windows will continue to send a WM_Paint message to the window process, which is likely to cause the system crash! Another way to get a DC handle at any time: DHC HDC; HDC = getdc (hwnd); get a DC handle [use GDI functions] ReleaseDC (HWnd, HDC); release DC handle Now we can use GDI functions in the window The customer district draws text or graphics, and this chapter gives an example: Output text. The examples in the book use the GetSystemMetrics function to obtain information of the user interface component size (such as: screen width, height, height of the title bar in the window), and use the Textout's GDI function to display this information by row in the client area of ​​the window. The book is gradually extended by three steps, and finally gets a relatively perfect procedure. This process is very useful in our practical work, developing a software to implement the main function of the software, then gradually expand the function of this software as needed, and is also our programming knowledge and capabilities. Scroll Bars is one of the best features of the graphical user interface. When the displayed text, graphics, the space required, which exceeds the space that the window can provide, you can use the scroll bar. In the application, the horizontal or vertical scroll bar is included, just in the third parameter of the CreateWindow function, you can include window style WS_VScroll (vertical) or WS_HSCROLL (horizontal). When you click the scroll bar or hit the scroll, Windows sends a WM_VSCROLL and WM_HSCROLL message to the window procedure, and "Notification Code" indicating the mouse action in the low 16-bit byte in the message parameter wPARAM. With the notification code, we can process the various operations of the mouse. .... Case WM_VSCROLL: SWITCH (WPARAM) Number of low 16-bit bytes in WPARAM {CASE SB_LINEUP: "Notification Code" Notify us of the mouse to scroll up rolling a row in the row ... CASE SB_LINEDown : "Notice" Notify us of the mouse to scroll down the row next to the row ... RETURN 0;

How to do corresponding processing for scroll bars? The three functions of SetscrollInfo, GetScrollinfo and ScrollWindow can perform various processes, and the meaning and use of these functions can refer to the third example in the book and MSDN. It is worth noting that Windows automatically handles all mouse operations of the scroll bar, but does not process the operation of the keyboard, which requires the access code to support the keyboard operation, will learn in the next chapter.

转载请注明原文地址:https://www.9cbs.com/read-28249.html

New Post(0)