In Windows Components, it can be said that the scroll bar is the most difficult. It is difficult to set the scrolling information.
First, you need to set the scrollinfo structure during initialization, including Nmin, Nmax, page, and Pos information.
Nmin and NPOs are generally set to 0. There are some tips for Nmax and page settings. The key is to understand the page. Page is actually the size of the visible part. For example, for a window with a scroll bar, the page size is the client area (for a vertical scroll bar, it is the height of the window, for a horizontal scroll bar, it is the width of the window ). While Nmax is the area to be displayed. For example, for a program that displays an article, if an article requires a height of 1000 pixels while the window is only 300 pixels, you can set Nmax to 1000 and npage to 300. In fact, the slider we see on the scroll bar represents a page, that is, the current window we see. The position of the slider corresponds to NPOs and the size of the npage.
The scroll bar must handle the rolling process by itself, and the rolling position and size of the window may change, so it is also troublesome to handle, but there are still good skills.
In Windows, wm_hscroll or wm_vscroll messages are sent to allow the program to process scrolling. We can use getscrollinfo to obtain the current pos: that is, the position before the rolling occurs. Based on the scroll action: scroll a row, a page, or a drag, we can determine a new location, use this location minus the old location, and then get the offset, this offset, it is called for the scrollwindow function.
For scrolling rows, you must specify the size of the rows separately.
Take the preceding example as an example to briefly introduce the vertical scrolling example (pseudo code, cannot be compiled ):
Int line = 10; // the size of the row defined by yourself. It can be any reasonable value or formula.
Case wm_hscroll:
Scrollinfo Si;
Si. fmask = sif_all:
Si. cbsize = sizeof (SI );
Getscrollinfo (hwnd, sb_vret, & Si); // obtain the information of the scroll bar.
Int NPOs = Si. NPOs;
Switch (loword (wparam) // The lower word of wparam is the action of the scroll bar.
{
Case sb_lineup:
NPOs-= line;
Break;
Case sb_linedown:
NPOs + = line;
Break;
Case sb_pageup:
NPOs-= Si. npage;
Break;
Case sb_pagedown:
NPOs + = Si. npage;
Break;
Case sb_thumbposition:
NPOs = hiword (wparam); // the high position of wparam is the address dragged by the user.
Break;
}
// Prevent cross-border attacks
If (NPOs> Si. Nmax) NPOs = Si. Nmax;
If (NPOs <Si. Nmin) NPOs = Si. Nmin;
// Scroll window
Scrollwindow (hwnd, 0, Si. NPOs-NPOs, null, null); // if it is downward scrolling, the offset is a negative value, and the scroll up is a positive value.
Si. NPOs = NPOs; // set the new value
Setscrollinfo (hwnd, sb_vret, & Si, false );
If the window size changes, scrollinfo may need to be adjusted. However, the position of NPOs may not be 0. In fact, there is no need to change the POs value. Because we use pixels, no matter how the window size changes, that is, how npage changes, Nmax remains unchanged: unless the article changes, NPOs does not need to change at all. For some variable-edited articles, the length of the article changes from time to time, that is, Nmax is changing, so we only need to ensure that NPOs will not exceed Nmin ~ Nmax is enough.