"Windows program design" reading notes

xiaoxiao2021-03-06  52

Output text

See the money

2005-3-28

The client area is part of the entire application window that is not subject to the title bar, window border, and optional menu bar, toolbar, status bar, and scroll bar, which is part of the window to write or pass visual information from the program.

The window customer area is always ready to re-draw. After a part of the window customer area is invalid, Windows refreshes the "invalid area" by sending a WM_PAINT message notification window.

Windows saves a "drawing information structure" for each window, contains the minimum rectangular coordinates and other information of "invalid area". The window can be returned to the "invalid area" rectangular range by this structure before processing the WM_PAINT message. Windows does not place multiple WM_PAINT messages into the message queue. Therefore, if the window procedure is handled by the WM_PAINT message, the other area of ​​the client area becomes "invalid area", Windows will calculate the invalid area surrounded by two regions and put the changed information into the drawing information structure.

InvalIdateRect is invalid for a rectangular area in the client area. GetUpdateRect can return "invalid area" information at any time. Calling BeginPaint enables the entire customer area to be valid, calling ValidateRect to enable any rectangular area within the client area. If there is no "invalid area" in the client area, the WM_PAINT message in the message queue will be deleted.

To draw in a window client area, you must call the Graphics Device Interface (GDI) function provided by Windows. The first parameter of each GDI function must be an HDC type handle. It is the device description table (DC) handle. You must first get the device description table handle before calling the GDI function. After use, you must release the device description table handle.

In addition to calling the device description table handle returned by CREATEDC, the program must obtain and release the handle during processing a single message.

There are two ways to return to the device description table handle:

1. Call the BeginPaint function. This method is usually used when processing the WM_PAINT message. The BeginPaint function makes the window customer area valid. The WM_ERASEBKGND message generally transmits the WM_ERASEBKGND message to the window process erase the invalid rectangular area background (if the invalidateRect is invalid, the client area is not valid, and the parameters can be set to decide whether to erase the background. By returning the PAINTSTRUCT architecture, the result flag can be viewed, if the value is false (0), indicating the invalid rectangular background has been erased; if the value is true, it means not erasing. PaintStruct structure The first member represents the device description of the device description of the table, and the RCPaint member represents the rectangular range of the invalid area. Don't forget to call Endpaint.

2. Calling getDC. Releasedc must be called after use. Also note that these two functions should be called in the same message processing. Unlike BeginPaint, the device descriptor returned by GetDC represents the entire client area, not just an invalid rectangular area. Also, GetDC does not make any invalid area to be valid. To make the invalid area valid, you can call ValidateRect. A function similar to getDC is getWindowDC. However, GETDC returns the device description table handle for drawing the window client area, and getWindowDC returns the device description table handle for drawing the entire window.

Textout is the most commonly used GDI function for displaying text. The first parameter of this function is the device description table handle. Device Description Table Attribute Controls the characteristics of the text, the default definition text font is black, the text background is white. Device Description Table text Background Colors and Definition Window Time Setting The background color is a separate part. Window Category Background Color When using a client area, text background color is used when the GDI function is used in the client area. Typically, the same color is set by default. X and Y Define the start coordinates of the string within the client area. In the default device description table, the origin (0, 0) coordinate is set to the upper left corner of the client area. In fact, our coordinates used in the GDI function are "logical coordinates", which is a mapping of the physical pixel coordinates of the display. There are a variety of coordinates "mapping methods" in Windows. Mapping mode defines in the device description table, the default mapping method is mm_text. In this mapping mode, logical units and physical units are the same. When you call TextOut to display text, the default font is a system font, which is a "dot matrix". Early system font adopts equivalent wide font, which is now widened.

The GetTextMetrics function returns the font information set in the device description table, and the return information is saved in a TextMetric structure. Here we only use the top 7 parameters of this structure:

Typedef struct tagtextMetric {

Long Temheight;

Long tMascent;

Long TMDESCENT;

Long TMINTERNALLEADING;

Long TmexternalLleading;

Long tMAVECHARWIDTH;

Long TMMAXCHARWIDTH;

...

TextMetric, * PTextMetric;

Temheight is equal to the sum of TMASCENT and TMDESCENT. The TMASCENT and TMDESCENT represent the largest longitudinal height on the baseline and below. LEADING represents the room reserved between the two lines. The internal spacing is included in TMASCENT, which is where the stress symbols often appear. If the internal spacing is set to 0, the characters of the plunder are shortened to accommodate the accent symbol. The external spacing is the space size between the text lines and is not included in the TMASCENT. TMAVECHARWIDTH indicates that lowercase letters weighted average width. TMMaxCharWidth indicates the width of the widest character in the font.

Generally we call the GetTextMetrics function in the WM_CREATE message processing after calling CREATEWINDOW.

The GetSystemMetrics function returns a variety of system information (graphical elements) and system configuration settings. This function has only one parameter called "index". 75 integral identifiers defined by Windows, getSystemMetrics returns an integer, which is usually the size of the graphic element in the function parameter. The function prototype is as follows:

INT getSystemMetrics; INT NINDEX

TEXTOUT calls Call setTalign to specify the text arrangement.

For the width font, the TmpitchandFimaly parameter in the TextMetric structure is 1, for the equippex font, which is 0. The uppercase letter width is almost 1.5 times lowercase letters. Therefore, the basic width to accommodate a character can be calculated:

CXCAPS = (TM.TMPITCHANDFAMILY & 1? 3: 2) * cxchar / 2;

Although we can call GetClientRect to return to the customer area size, it is determined that the customer's size is better to process the WM_SIZE message during the window. The LPARAM parameter of this message contains the customer area width, and the high-character bit contains the customer area height. There must be a WM_PAINT message after the WM_SIZE message. Window size changes, will inevitably lead to refreshing client districts. If the current client area cannot accommodate all content to be drawn, then we need to use "scroll bar".

Two types of rolling strips "horizontal scroll strips" and "vertical scroll bar". To include the scroll bar in the Application window, the ws_vscroll (vertical scrolling) and WS_HSCROLL (horizontal scroll) are included in the window style parameters of the CreateWindow function. The customer area does not include the space occupied by the scroll bar.

Each scroll bar has a related "range" (this is a pair of integers, representing minimum and maximum) and "location" (it is a position within which the scroll box is within this range). The rolling strip range defaults from 0 (top or the left) to 100 (bottom or right), the change range is also easy, and setscrollRange can be called. The function prototype is as follows:

Bool SetscrollRange (hwnd hwnd,

INT IBAR,

Int Iminpos,

Int iMaxpos,

Bool brewraw

);

The parameter IBAR is SB_VERT or SB_HORZ, IMIN and IMAX, respectively, respectively, respectively, respectively, and maximum. BREDRAW indicates whether the scroll bar is redrawn after the range is set. For example, as follows:

SetscrollRange (hwnd, sb_vert, 0, numlines - 1, false); // Determine the scroll bar // range according to the number of text lines

If you call the setScrollRange, you will call the function of change the scroll bar position, and BREDRAW should be set to false. Prevent multiple refreshes.

Calling the SetScrollPos function can set a new scroll bar position in the scroll bar range. The function prototype is as follows:

Int setscrollpos (hwnd hwnd,

INT IBAR,

INT IPOS,

Bool brewraw

);

Parameter IPOS represents a new location. For example, as follows:

Setscrollpos (HWND, SB_VERT, 0, TRUE); // Set the initial position of the scroll bar

The position of the rolling box is always discrete integer value. Windows will handle all scroll bar mouse events and send scroll bar messages. We are typically responsible for initializing the roll bar range and position, processing scroll bars, updating the scroll bar position, and updating the client area to respond to the change of the scroll bar.

When you click the scroll bar or drag the scroll box with a mouse, Windows sends WM_VScroll (up and down move) or WM_HSCROLL messages to the window. Each mouse action on the scroll bar generates at least two messages, and one is generated when the mouse button is pressed, and one is generated when the button is released. If the scroll bar is part of the window, ignore the LPARAM; the LPARAM parameter is only used as the scroll bar as the sub-window. The WPARAM parameter low 16-bit identification mouse for the action of the scroll bar, and the high 16 bits are used to identify the rolling frame position when the mouse drag scroll is active, and the other mouse action is ignored.

Click the arrow of the scrollbar or SB_LINELEFT or generated SB_LINEUP SB_LINEDOWN or SB_LINERIGHT message; click or scroll bar itself SB_PAGEUP SB_PAGELEFT or SB_PAGEDOWN or SB_PAGERIGHT message, drag the scroll box generating WM_THUMBTRACK message; and other mouse drag the scroll box at the end of the operation Outside the WM_THUMBPSITION message, a WM_ENDSCROLL message is generated at the end of the other mouse. Processing drag scroll box action, generally do not need to process WM_THUMBTRACK and WM_THUMBPSITION messages simultaneously. As for which message is selected, it is dependent on the situation.

As part of the application window, never receive a notification message containing SB_TOP, SB_BOTTOM, SB_LEFT, SB_RIGHT and other mice.

Responding to scroll bar mouse action code:

Case SB_LineUp:

ivscrollpos - = 1;

Case SB_LINEDown:

IvscrollPos = 1;

Case sb_thumbposition:

Ivscrollpos = HiWord (WPARAM);

...

Setscrollpos (HWND, SB_VERT, IVSCROLLPOS, TRUE);

InvalidateRect (hwnd, null, true); // Call UpdateWindow function client area refresh faster

Now we use the setscrollinfo function when we handle scroll bars, mainly two major advantages:

1. The rolling box and the display document size are proportional. The algorithm is:

2. The scroll strip range can be set to 32-bit values, and 32-bit rolling frame locations can be obtained over the GetScrollInfo function.

The setscrollinfo and getscrollinfo functions include a parameter of the ScrollInfo structure type, which is as follows:

Typedef struct tagscrollinfo {

Uint CBSIZE;

Uint fmask;

Int nmin;

Int nmax;

Uint npage;

Int npos;

Int ntrackpos;

ScrollInfo, * lpscrollinfo;

The FMask parameter contains one or more flags starting with the SIF prefix, which can be combined by bit or operation (|). When using the SIF_RANGE flag, set or return to the scroll bar range via nMIN and NMAX parameter; when using the SIF_POS flag, set or return to the scroll bar position through the NPOS parameter; when using the SIF_PAGE flag, set or return to the current page size by the NPAGE parameter; use SIF_TRACKPOS logo When dragging the scroll box, return the current scroll box position through the NTRACKPOS parameter; use the SIF_DISABLENSCROLL flag to disable the scroll bar. For example, as follows:

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);

The above setscrollinfo function call implements the following code:

Ivscrollmax = max (0, Numlines - cyclient / cychar);

SetscrollRange (HWND, SB_VERT, 0, IVScrollmax, true);

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

New Post(0)