Chapter II Output Text
First, WM_PAINT message: Call the UpdateWindow function after the application enters the WinMain function, Windows uses this opportunity to send a WM_PAINT message to the form process, requiring an invalid area. In the following case, the form process will receive a WM_PAINT message: 1. When the user moves the window or the display window, when the area previously hidden in the window is re-visible
2, the user changes the window size (but must have CS_HREDRAW and CS_VREDRAW settings)
3, the program uses the scrollwindow or scrolldc function to scroll part of the customer area
4, the program explicitly generates WM_PAINT messages using the InvalIDateERECT or InValidateRect function. Under the way, Windows may issue a WM_PAINT message: 1. Windows erase the dialog box or message box of the partial window 2, pull it out under the menu and release 3, display tool tips
In some cases, Windows always saves what he covers the display area, then automatically recovers: 1. The mouse cursor traverses the customer area 2, and the icon dragged the client area.
Note that the unique condition for receiving the WM_PAINT message is that a part of the client area is invalid. The processing of the WM_PAINT message is now described below: 1. When the window process processes WM_PAINT, the other area is invalid before, Windows will calculate a new invalid area surrounded by two areas and write these change. In the information structure. Windows is not, please note that multiple WM_PAINT messages will not be placed in the message queue.
2, Windows The processing of the WM_Paint message by default, that is, the response of the DEFWINDOWPROC on the message is as follows:
Case WM_Paint:
Beginpaint (hwnd, & ps);
Endpaint (hwnd, & ps);
Return 0;
Second, Device Description Table 1
The programmer is usually the first three fields, and other Windows processes. HDB device description table handle. Ferase is generally false (0), meaning that Windows has erased in addition to an invalid rectangular background.
If you use the InvalIDRect (HWND, NULL, FALSE) function, the window process will be set to the FERASE field after processing the WM_PAINT message. The third parameter is RECT type: typedef struct _Rect {long LEFT; long top; long right; long bottom;} Rect; represents the location of the invalid area. 2, the handle method of obtaining the device descriptor 1: Use this method when processing WM_PAINT: PaintStruct PS; HDC HDC; Case WM_Paint: HDC = BeginPaint (HWND, & PS); Endpaint (HWND, & PS); Return 0; pay attention to this Method error: case wm_paint: return 0; don't do this! ! Windows puts a WM_Paint message into the message queue is because the customer area is partially invalid. If you do not call beginpaint with endpaint or validate, then this area will not become valid, then Windows will always send a WM_PAINT message. In addition, Windows makes the rectangle in the entire customer area through an InvalIDateRect, which can get the coordinates of the invalid rectangle by getUpdate when receiving the WM_PAINT message. After processing the WM_PAINT message, the window process calls BeginPaint, (the BeginPAint function is generally caused by the background of the invalid area when preparing to draw, the function is also filld in the PS structure) The entire client area becomes effective. The program can make any rectangular area in the customer zone to be valid by calling the ValidateRect function. If you call this command, the entire client area is valid, then all the WM_PAINT messages in the current message queue are deleted. Method 2: Generally call GETDC and ReleaseDC to react to keyboard messages or mouse messages. HDC = GETDC (HWND); ReleaseDC (HWND, HDC); Note: You must call ReleaseDC before exiting the window, do not call GetDC in a message, but in another message to call ReleaseDC with getDC, and getWindowDC, This function returns the device description handle of the entire window. Different ways: GetDC returned by the device description table has a cut area, equal to the entire client area, GETDC does not make the invalid area become effective. When Beginpaint begins, it will restore the invalid area. The InvalidateRect (HWND, NULL, TRUE) can make the entire customer zone into invalid, and erase the background, which is a simple way to redraw a client area. ValidateRect (HWND, NULL) is the opposite.
Third, Textout Detail Problem: Textout (HDC, X, Y, PSText, Ilength) PSText: Pointer Ilength, pointing to the characters to be displayed ilength: The length of the string is smaller with the WSPrintf: int ILngth; tchar szbuffer [40]; iLength = WSPrintf (Szbuffer, Text ("THE SUM OF% I and IS% I"), IA, IB, IA IB; TextOut (HDC, X, Y, Szbuffer, Ilength); Fourth, Font The program calls a getSystemMetrics function to determine the size of the user interface to call TextMericTM; HDC = GetDC (HWND); GetTextMetrics (HDC, & TM); ReleaseDC (HWND, HDC); About the TextMetric structure: typedef struct tagtextMetric {// TM Long tmHeight; LONG tmAscent; LONG tmDescent; LONG tmInternalLeading; LONG tmExternalLeading; LONG tmAveCharWidth; LONG tmMaxCharWidth; LONG tmWeight; LONG tmOverhang; LONG tmDigitizedAspectX; LONG tmDigitizedAspectY; BCHAR tmFirstChar; BCHAR tmLastChar; BCHAR tmDefaultChar; BCHAR tmBreakChar; BYTE tmItalic; BYTE tmUnderlined; Byte tmpitchandfamily; byte tmcharset;} textmetric; 2, formatted text first use two variables to store average character height width: static int cxchar, cychar; case wm_create: HDC = Getdc (hwnd); GetTextMerics (HDC, & TM); cxchar = tm.tmavecharWidth; cychar = tm.tmheight tm.tmexternalleading; releasedc (hwnd, hdc); return 0; 5, scroll bar 1, customer area: Customer area and whether the client area is maximized GetSystemMetrics can be called by using SM_CXFULLSCREEN and SM_CYFULLSCREEN to call GetSystemMetrics. You can also determine the size of the client area through the getClientRect function. This function should be called when the WM_SIZE message occurs. The low word of the LPARAM parameter transmitted to the window includes the width of the client area, and the height of the client area is included.
Save the following two variables: static int cxclient, cyclient; actually use the following: wm_size: cxclient = loword (lparam); cyclient = HiWord (lparam); return 0; the total number of critical lines of customer area: Cyclient / cychar customer area The number of lowercase letters can be displayed: cxclient / cxchar, one of the WM_SIZE messages, always follow the WM_PAINT message, because we make form-style: CS_HREDRAW | CS_VREDRAW This type tells Windows When the horizontal or vertical size changes Forced refreshing the customer area. 2, scroll bar generation: The generation of scroll bar can be laminated in the form style in CreateWindow: WS_VScroll or WS_HSCROLL3, scroll bar range and position: By default range 0 ~ 100 Function Change range: setscrollRange (hwnd, ibar, iMin , IMAX, BREDRAW; BREDRAW: If you want Windows to scroll down the roll settings according to the new range, set BredRaw to TRUE. If you call other functions that affect the rolling bar position after calling SetScrollRange, you should set your BREDRAW to false to avoid more Subtle. IBAR: Numerical is SB_VERT or SB_HORZ Sets Rolling Bar Location: Setscrollpos (HWND, IBAR, IPOS, BREDRAW); additional Windows provides GetScrollRange and GetScrollPos to get the current range and current location of the scroll bar.
Note that this function getScrollpos, the function does not change when the user drags the scroll bar or the scroll block, you must use the setScrollPos function to change its value, which is why drag the scroll block, if you do not call the setscrollpos to handle SB_thumbTrack or SB_thumPosition message After the user releases the mouse button, the scroll box will quickly jump back to the original location. 4, the scroll bar message is the processing of Windows to the scroll bar: handles all scroll mouse time When the user clicks the mouse in the scroll bar, it provides a "reverse flash" when the user drags the scroll box in the scroll bar, The mobile scroll box sends a scroll bar message to the window process containing the scroll bar window. The following is the programmer to process: the range and position processing window process respond to the scroll bar message update the position of the scrollbar of the scroll box Change the content of the client area. To respond to the scroll bar, change Windows to the window process is WM_VScroll, WM_HScroll message, WM_HSCROLL message. The message of the scroll bar is a pair, when the mouse is pressed and released. WM_VSCROLL and WM_HSCROLL are also WPARAM and LPARAM message parameters as other messages. For rolling bars from a part of the window, you can ignore LPARAM, which is only used as the scroll bar created as a sub-window. The WPARAM message parameters are divided into a low-character bit and a high-character bit. The low-character bit is a value indicating the operation of the mouse to the scroll bar (see p91), the high-character bit is the current location when the user is dragging the scroll box.
When the mouse drags the scroll box to move it, the application will receive the SB_THUMBTRACK message. However, if the SB_THUMBTRACK or SB_THUMBPSITION message is not processed by calling setscrollpos, the user releases the mouse button, the scroll box will quickly jump the original position.
If you handle the SB_THUMBTRACK message, you will need to move the content of the client when you drag the scroll box.
If the SB_THUMBPSITION message is processed, the content of the client area is moved as long as the user stops scrolling the scroll box. The WinUser.h header is also included in the SB_TOP, SB_BOTTOM, SB_LEFT, and SB_RIGHT notification codes have been moved to its largest or minimum position. But the scroll bar created as part of the application window will never accept these notices.
5, scroll bar function under Win32API
Win32API provides two scroll bar functions:
SetscrollInfo and getScrollinfo functions
SetscrollInfo (HWND, IBAR, & Si, BREDRAW);
GetscrollInfo (HWND, IBAR, & SI);
IBAR: SB_VERT or SB_HORZ or SB_CTL of the scroll bar controlled
The ScrollinFor structure is defined as follows:
Typedef struct tagscrollinfo
{
Uint cbsize; // set to sizeof (scrollInfo)
Uint fmask; // value to set or get
Int nmin; // minimum range value
Int nmax; // maximum range value
Uint npage; // page size
Int npos; // current position
INT NTRACKPOS; // Current TRACKING POSITION
}
ScrollInfo, * pscrollinfo;
In the program, you can define the following ScrollInfo structural types: ScrollInfo Si;
Si.cbsize = sizeof (si);
Si.cbsize = sizeof (scrollInfo); is consistent
The Fmask field is one or more flags at the beginning of the SIZ prefix, and can use C bit operation or function | combine these logos.
SFMASK - NMIN and NMAX fields are set to the range required for scroll bars.
Sifpos - NPOS location required
SIF_PAGE - Get the size of the page (number of lines)
Fmask sign
meaning
SiFmask
Nmin and NMAX fields are set to the range required for scroll bars.
Sifpos
NPOS location required
SIF_PAGE
Get the size of the page (number of lines)
SIF_TRACKPOS
NTRACKPOS field points out the current 32-bit scroll bar position
Sifall
All parameters above are set
SIF_DISABLENSCROLL
Disable scroll bar when the program is trying to hide the scroll bar
6, Win32 two API functions set the range of scroll bar:
Suppose the form size is 50 rows, and Numlines is equal to 75
Then the scrolling range is from: 0--50 to 25--75
Therefore, it can be set by the following parameters:
Si.cbsize = sizeof (scrollInfo);
Si.cbmask = SIF_RANGE | SIF_PAGE;
Si.nmin = 0;
Si.nmax = Numlines -1;
Si.NPAGE = cyclient / cychar;
SetscrollInfo (HWND, SB_VERT, & SI, TRUE);
After setting, Windows will limit the maximum scroll bar position to Si.NMAX - Si.NPage 1, not Si.NMAX
That is to say: Numlines is equal to 25 (Si.NMAX is equal to 74),
Si.NPAGE is equal to 50. The maximum location is limited to 74-50 1 = 25 is what we need.
When the page size is as large as the scroll bar, when NPAGE is greater than or equal to 75, Windows usually hides the scroll bar, but can disable the scroll bar through the Sif_Disablenoscroll flag instead of hiding it.
6, ScrollWindow function:
Bool scrollwindow
HWND HWND, // Handle to Window
Int Xamount, // Horizontal Scroling
Int Yamount, // Vertical Scrolling
Const Rect * LPRECT, // Client Area
Const Rect * LpclipRect // Clipping Rectangle
);
The second parameter indicates the value of the horizontal scroll client area.
The third parameter pointed out the value of the vertically scrolling the customer area, and the unit is pixel.
The last two parameters are set to null, pointing out to scroll throughout the client area. Windows automatically sets the rectangles that are not covered by the scrolling operation in the client area to be invalid. This will generate a WM_PAINT message. Never need InvalIDateRect.