windows程序设计 滚动条

本文详细介绍了如何在应用程序中包含水平或垂直滚动条,并解释了滚动条的范围和位置概念。包括设置滚动条范围、位置的方法,以及滚动条消息处理和与窗口交互的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Scroll Bars

It is easyto include a horizontal or vertical scroll bar in your application window. All you need do is include the window style (WS)identifier WS_VSCROLL (vertical scroll) or WS_HSCROLL (horizontal scroll) orboth in the third argument to CreateWindow. The scroll bars specified inthe CreateWindowfunction are always placed against the right side or bottom ofthe window and extend the full length or width of the client area. The client area does not include the space occupied bythe scroll bar. The width of the vertical scroll bar and the height ofthe horizontal scroll bar are constant for a particular video driver anddisplay resolution. If you need these values, you can obtain them (as you mayhave observed) from the GetSystemMetricscalls.

Windowstakes care of processing all mouse messages to the scroll bars. However, scrollbars do not have an automatic keyboard interface. If you want the cursor keys to duplicate some of thefunctionality of the scroll bars, you must explicitly provide logic for that.

Scroll Bar Range and Position

Everyscroll bar has an associated "range" and "position." Thescroll bar range is a pair of integers representing a minimum and maximum valueassociated with the scroll bar. The position is the location of the thumb within the range. When the thumb is at the top (or left) of the scroll bar,the position of the thumb is the minimum value of the range. At the bottom (orright) of the scroll bar, the thumb position is the maximum value of the range.

By default,the range of a scroll bar is 0 (top or left) through 100 (bottom or right), butit's easy to change the range to something that is more convenient for theprogram:

SetScrollRange (hwnd, iBar, iMin,iMax, bRedraw) ;

Thethumb position is always a discrete integral value.

You can useSetScrollPosto set a new thumb position within the scroll bar range:

SetScrollPos (hwnd, iBar, iPos,bRedraw) ;

TheiPosargument is the new position and must be within the range of iMinand iMax .Windows provides similar functions (GetScrollRangeand GetScrollPos) to obtainthe current range and position of a scroll bar.

When you usescroll bars within your program, you share responsibility with Windows formaintaining the scroll bars and updating the position of the scroll bar thumb.These are Windows' responsibilities for scroll bars:

Handle allprocessing of mouse messages to the scroll bar.

Provide areverse-video "flash" when the user clicks the scroll bar.

Move thethumb as the user drags the thumb within the scroll bar.

Send scrollbar messages to the window procedure of the window containing the scroll bar.

These arethe responsibilities of your program:

Initializethe range and position of the scroll bar.

Process thescroll bar messages to the window procedure.

Update theposition of the scroll bar thumb.

Change thecontents of the client area in response to a change in the scroll bar.

Windowssends the window procedure WM_VSCROLL (vertical scroll) and WM_HSCROLL(horizontal scroll) messages when the scroll bar is clicked with the mouse orthe thumb is dragged. Each mouse action on thescroll bar generates at least two messages, one when the mouse button ispressed and another when it is released.

Like allmessages, WM_VSCROLL and WM_HSCROLL are accompanied by the wParamand lParammessageparameters. For messages from scroll bars created as part of your window, youcan ignore lParam; that's used only for scroll bars created as child windows,usually within dialog boxes.

Scroll Bar Messages

Windowssends the window procedure WM_VSCROLL (vertical scroll) and WM_HSCROLL(horizontal scroll) messages when the scroll bar is clicked with the mouse orthe thumb is dragged. Each mouse action on the scroll bar generates at leasttwo messages, one when the mouse button is pressed and another when it isreleased.

Likeall messages, WM_VSCROLL and WM_HSCROLL are accompanied by the wParam and  lParammessage parameters. For messages from scroll barscreated as part of your window, you can ignore lParam; that's used only forscroll bars created as child windows, usually within dialog boxes.

ThewParam message parameter is divided into a low word and a high word. The lowword of wParamis a number that indicates what the mouse is doing to the scrollbar. This number isreferred to as a "notification code." Notification codes have valuesdefined by identifiers that begin with SB, which stands for "scrollbar." Here's how the notification codes are defined in WINUSER.H:

#define SB_LINEUP 0

#define SB_LINELEFT 0

#define SB_LINEDOWN 1

#define SB_LINERIGHT 1

#define SB_PAGEUP 2

#define SB_PAGELEFT 2

#define SB_PAGEDOWN 3

#define SB_PAGERIGHT 3

#define SB_THUMBPOSITION 4

#define SB_THUMBTRACK 5

#define SB_TOP 6

#define SB_LEFT 6

#define SB_BOTTOM 7

#define SB_RIGHT 7

#define SB_ENDSCROLL 8

If you holddown the mouse button on the various parts of the scroll bar, your program canreceive multiple scroll bar messages. When the mouse button is released, you'llget a message with a notification code of SB_ENDSCROLL. You can generally ignore messages with the SB_ENDSCROLL notificationcode. Windows will not change the position of the scroll bar thumb. Yourapplication does that by calling SetScrollPos.

When youposition the mouse cursor over the scroll bar thumb and press the mouse button,you can move the thumb. This generates scroll barmessages with notification codes of SB_THUMBTRACK and SB_THUMBPOSITION. Whenthe low word of wParamis SB_THUMBTRACK, the high word of  wParamis the current position of the scroll barthumb as the user is dragging it. This position is within the minimum andmaximum values of the scroll bar range. When the low word of wParamisSB_THUMBPOSITION, the high word of wParamis the final position of the scrollbar thumb when the user released the mouse button. For other scroll bar actions, the high word of wParam shouldbe ignored.

To providefeedback to the user, Windows will move the scroll bar thumb when you drag itwith the mouse as your program is receiving SB_THUMBTRACK messages. However, unless you process SB_THUMBTRACK or SB_THUMBPOSITIONmessages by calling SetScrollPos, the thumb will snap back to its originalposition when the user releases the mouse button.

Aprogram can process either the SB_THUMBTRACK or SB_THUMBPOSITION messages, butdoesn't usually process both. If you process SB_THUMBTRACK messages, you'll move the contents of yourclient area as the user is dragging the thumb. If instead you processSB_THUMBPOSITION messages, you'll move the contents of the client area only whenthe user stopsdragging the thumb. It's preferable (but more difficult) toprocess SB_THUMBTRACK messages; for some types of data your program may have ahard time keeping up with the messages.

Althoughit's not common, using 32-bit values for the scroll bar range is perfectlyvalid. However, the high word of wParam, which is only a 16-bit value, cannotproperly indicate the position for SB_THUMBTRACK and  SB_THUMBPOSITION actions. In thiscase, you need to use the function GetScrollInfo(described later in this chapter)to get this information.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值