Text02:初级滚动条 函数集(MSDN)

本文详细介绍了Windows API中用于控制滚动条的三个关键函数:SetScrollRange、SetScrollPos 和 InvalidateRect。这些函数允许开发者设置滚动条的最大最小值、当前位置,并标记窗口区域以进行更新重绘。

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

SetScrollRange
The SetScrollRange function sets the minimum and maximum scroll box positions for the specified scroll bar.

Note  The SetScrollRange function is provided for backward compatibility. New applications should use the SetScrollInfo function.

BOOL SetScrollRange(
  HWND hWnd,    // handle to window
  int nBar,     // scroll bar
  int nMinPos,  // minimum scrolling position
  int nMaxPos,  // maximum scrolling position
  BOOL bRedraw  // redraw flag
);
Parameters
hWnd
[in] Handle to a scroll bar control or a window with a standard scroll bar, depending on the value of the nBar parameter.
nBar
[in] Specifies the scroll bar to be set. This parameter can be one of the following values. Value Meaning
SB_CTL Sets the range of a scroll bar control. The hWnd parameter must be the handle to the scroll bar control.
SB_HORZ Sets the range of a window's standard horizontal scroll bar.
SB_VERT Sets the range of a window's standard vertical scroll bar.


nMinPos
[in] Specifies the minimum scrolling position.
nMaxPos
[in] Specifies the maximum scrolling position.
bRedraw
[in] Specifies whether the scroll bar should be redrawn to reflect the change. If this parameter is TRUE, the scroll bar is redrawn. If it is FALSE, the scroll bar is not redrawn.
Return Values
If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks
You can use SetScrollRange to hide the scroll bar by setting nMinPos and nMaxPos to the same value. An application should not call the SetScrollRange function to hide a scroll bar while processing a scroll bar message. New applications should use the ShowScrollBar function to hide the scroll bar.

If the call to SetScrollRange immediately follows a call to the SetScrollPos function, the bRedraw parameter in SetScrollPos must be zero to prevent the scroll bar from being drawn twice.

The default range for a standard scroll bar is 0 through 100. The default range for a scroll bar control is empty (both the nMinPos and nMaxPos parameter values are zero). The difference between the values specified by the nMinPos and nMaxPos parameters must not be greater than the value of MAXLONG.

Because the messages that indicate scroll bar position, WM_HSCROLL and WM_VSCROLL, are limited to 16 bits of position data, applications that rely solely on those messages for position data have a practical maximum value of 65,535 for the SetScrollRange function's nMaxPos parameter.

However, because the SetScrollInfo, SetScrollPos, SetScrollRange, GetScrollInfo, GetScrollPos, and GetScrollRange functions support 32-bit scroll bar position data, there is a way to circumvent the 16-bit barrier of the WM_HSCROLL and WM_VSCROLL messages. See GetScrollInfo for a description of the technique.

Example Code
For an example, see Using the Owner-Display Clipboard Format.

Requirements
  Windows NT/2000/XP: Included in Windows NT 3.1 and later.
  Windows 95/98/Me: Included in Windows 95 and later.
  Header: Declared in Winuser.h; include Windows.h.
  Library: Use User32.lib.

See Also
Scroll Bars Overview, Scroll Bar Functions, GetScrollInfo, GetScrollPos, GetScrollRange, SetScrollInfo, SetScrollPos, ShowScrollBar

SetScrollPos
The SetScrollPos function sets the position of the scroll box (thumb) in the specified scroll bar and, if requested, redraws the scroll bar to reflect the new position of the scroll box.

Note  The SetScrollPos function is provided for backward compatibility. New applications should use the SetScrollInfo function.

int SetScrollPos(
  HWND hWnd,     // handle to window
  int nBar,      // scroll bar
  int nPos,      // new position of scroll box
  BOOL bRedraw   // redraw flag
);
Parameters
hWnd
[in] Handle to a scroll bar control or a window with a standard scroll bar, depending on the value of the nBar parameter.
nBar
[in] Specifies the scroll bar to be set. This parameter can be one of the following values. Value Meaning
SB_CTL Sets the position of the scroll box in a scroll bar control. The hWnd parameter must be the handle to the scroll bar control.
SB_HORZ Sets the position of the scroll box in a window's standard horizontal scroll bar.
SB_VERT Sets the position of the scroll box in a window's standard vertical scroll bar.


nPos
[in] Specifies the new position of the scroll box. The position must be within the scrolling range. For more information about the scrolling range, see the SetScrollRange function.
bRedraw
[in] Specifies whether the scroll bar is redrawn to reflect the new scroll box position. If this parameter is TRUE, the scroll bar is redrawn. If it is FALSE, the scroll bar is not redrawn.
Return Values
If the function succeeds, the return value is the previous position of the scroll box.

Windows XP: If the desktop is themed and the parent window is a message-only window, the function returns an incorrect value.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks
If the scroll bar is redrawn by a subsequent call to another function, setting the bRedraw parameter to FALSE is useful.

Because the messages that indicate scroll bar position, WM_HSCROLL and WM_VSCROLL, are limited to 16 bits of position data, applications that rely solely on those messages for position data have a practical maximum value of 65,535 for the SetScrollPos function's nPos parameter.

However, because the SetScrollInfo, SetScrollPos, SetScrollRange, GetScrollInfo, GetScrollPos, and GetScrollRange functions support 32-bit scroll bar position data, there is a way to circumvent the 16-bit barrier of the WM_HSCROLL and WM_VSCROLL messages. See GetScrollInfo for a description of the technique.

Requirements
  Windows NT/2000/XP: Included in Windows NT 3.1 and later.
  Windows 95/98/Me: Included in Windows 95 and later.
  Header: Declared in Winuser.h; include Windows.h.
  Library: Use User32.lib.

See Also
Scroll Bars Overview, Scroll Bar Functions, GetScrollInfo, GetScrollPos, GetScrollRange, SetScrollInfo, SetScrollRange

InvalidateRect
The InvalidateRect function adds a rectangle to the specified window's update region. The update region represents the portion of the window's client area that must be redrawn.

BOOL InvalidateRect(
  HWND hWnd,           // handle to window
  CONST RECT* lpRect,  // rectangle coordinates
  BOOL bErase          // erase state
);
Parameters
hWnd
[in] Handle to the window whose update region has changed. If this parameter is NULL, the system invalidates and redraws all windows, and sends the WM_ERASEBKGND and WM_NCPAINT messages to the window procedure before the function returns.
lpRect
[in] Pointer to a RECT structure that contains the client coordinates of the rectangle to be added to the update region. If this parameter is NULL, the entire client area is added to the update region.
bErase
[in] Specifies whether the background within the update region is to be erased when the update region is processed. If this parameter is TRUE, the background is erased when the BeginPaint function is called. If this parameter is FALSE, the background remains unchanged.
Return Values
If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero.

Windows NT/2000/XP: To get extended error information, call GetLastError.

Remarks
The invalidated areas accumulate in the update region until the region is processed when the next WM_PAINT message occurs or until the region is validated by using the ValidateRect or ValidateRgn function.

The system sends a WM_PAINT message to a window whenever its update region is not empty and there are no other messages in the application queue for that window.

If the bErase parameter is TRUE for any part of the update region, the background is erased in the entire region, not just in the specified part.

Example Code
For an example, see Invalidating the Client Area.

Requirements
  Windows NT/2000/XP: Included in Windows NT 3.1 and later.
  Windows 95/98/Me: Included in Windows 95 and later.
  Header: Declared in Winuser.h; include Windows.h.
  Library: Use User32.lib.

See Also
Painting and Drawing Overview, Painting and Drawing Functions, BeginPaint, InvalidateRgn, RECT, ValidateRect, ValidateRgn, WM_ERASEBKGND, WM_NCPAINT, WM_PAINT

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值