CScrollBar 类CScrollBar Class

提供 Windows 滚动条控件功能。Provides the functionality of a Windows scroll-bar control.

语法Syntax

class CScrollBar : public CWnd

成员Members

公共构造函数Public Constructors

名称Name 描述Description
CScrollBar::CScrollBarCScrollBar::CScrollBar 构造 CScrollBar 对象。Constructs a CScrollBar object.

公共方法Public Methods

“属性”Name 描述Description
CScrollBar:: CreateCScrollBar::Create 创建 Windows 滚动条并将其附加到 CScrollBar 对象。Creates the Windows scroll bar and attaches it to the CScrollBar object.
CScrollBar::EnableScrollBarCScrollBar::EnableScrollBar 启用或禁用滚动条的一个或两个箭头。Enables or disables one or both arrows of a scroll bar.
CScrollBar:: GetScrollBarInfoCScrollBar::GetScrollBarInfo 使用结构检索有关滚动条的信息 SCROLLBARINFORetrieves information about the scroll bar using a SCROLLBARINFO structure.
CScrollBar::GetScrollInfoCScrollBar::GetScrollInfo 检索有关滚动条的信息。Retrieves information about the scroll bar.
CScrollBar::GetScrollLimitCScrollBar::GetScrollLimit 检索滚动条的限制Retrieves the limit of the scroll bar
CScrollBar::GetScrollPosCScrollBar::GetScrollPos 检索滚动框的当前位置。Retrieves the current position of a scroll box.
CScrollBar::GetScrollRangeCScrollBar::GetScrollRange 检索给定滚动条的当前最小和最大滚动条位置。Retrieves the current minimum and maximum scroll-bar positions for the given scroll bar.
CScrollBar::SetScrollInfoCScrollBar::SetScrollInfo 设置有关滚动条的信息。Sets information about the scroll bar.
CScrollBar::SetScrollPosCScrollBar::SetScrollPos 设置滚动框的当前位置。Sets the current position of a scroll box.
CScrollBar::SetScrollRangeCScrollBar::SetScrollRange 设置给定滚动条的最小和最大位置值。Sets minimum and maximum position values for the given scroll bar.
CScrollBar:: ShowScrollBarCScrollBar::ShowScrollBar 显示或隐藏滚动条。Shows or hides a scroll bar.

备注Remarks

您可以通过两个步骤创建一个滚动条控件。You create a scroll-bar control in two steps. 首先,调用构造函数 CScrollBar 来构造 CScrollBar 对象,然后调用create成员函数以创建 Windows 滚动条控件,并将其附加到 CScrollBar 对象。First, call the constructor CScrollBar to construct the CScrollBar object, then call the Create member function to create the Windows scroll-bar control and attach it to the CScrollBar object.

如果在 CScrollBar 对话框中创建对象(通过对话资源),则 CScrollBar 当用户关闭对话框时,将自动销毁。If you create a CScrollBar object within a dialog box (through a dialog resource), the CScrollBar is automatically destroyed when the user closes the dialog box.

如果在 CScrollBar 窗口中创建对象,则可能还需要销毁它。If you create a CScrollBar object within a window, you may also need to destroy it.

如果在 CScrollBar 堆栈上创建对象,则该对象会自动销毁。If you create the CScrollBar object on the stack, it is destroyed automatically. 如果 CScrollBar 使用函数在堆上创建对象 new ,则必须对对象调用以在 delete 用户终止 Windows 滚动条时销毁该对象。If you create the CScrollBar object on the heap by using the new function, you must call delete on the object to destroy it when the user terminates the Windows scroll bar.

如果在对象中分配任何内存 CScrollBar ,请重写 CScrollBar 析构函数以释放分配。If you allocate any memory in the CScrollBar object, override the CScrollBar destructor to dispose of the allocations.

有关使用的相关信息 CScrollBar ,请参阅控件For related information about using CScrollBar, see Controls.

继承层次结构Inheritance Hierarchy

CObjectCObject

CCmdTargetCCmdTarget

CWndCWnd

CScrollBar

要求Requirements

标头: afxwin.hHeader: afxwin.h

CScrollBar:: CreateCScrollBar::Create

创建 Windows 滚动条并将其附加到 CScrollBar 对象。Creates the Windows scroll bar and attaches it to the CScrollBar object.

virtual BOOL Create(
    DWORD dwStyle,
    const RECT& rect,
    CWnd* pParentWnd,
    UINT nID);

参数Parameters

dwStyledwStyle
指定滚动条的样式。Specifies the scroll bar's style. 滚动条样式的任意组合应用于滚动条。Apply any combination of scroll-bar styles to the scroll bar.

rectrect
指定滚动条的大小和位置。Specifies the scroll bar's size and position. 可以是 RECT 结构或 CRect 对象。Can be either a RECT structure or a CRect object.

pParentWndpParentWnd
指定滚动条的父窗口,通常为 CDialog 对象。Specifies the scroll bar's parent window, usually a CDialog object. 值不得为 NULL。It must not be NULL.

nIDnID
滚动条的控件 ID。The scroll bar's control ID.

返回值Return Value

如果成功,则不为 0;否则为 0。Nonzero if successful; otherwise 0.

备注Remarks

可以通过 CScrollBar 两个步骤构造对象。You construct a CScrollBar object in two steps. 首先,调用构造对象的构造函数, CScrollBar 然后调用 Create ,它创建并初始化关联的 Windows 滚动条,并将其附加到 CScrollBar 对象。First, call the constructor, which constructs the CScrollBar object; then call Create, which creates and initializes the associated Windows scroll bar and attaches it to the CScrollBar object.

将以下窗口样式应用于滚动条:Apply the following window styles to a scroll bar:

  • 始终 WS_CHILDWS_CHILD Always

  • WS_VISIBLE 通常WS_VISIBLE Usually

  • 很少 WS_DISABLEDWS_DISABLED Rarely

  • WS_GROUP 分组控件WS_GROUP To group controls

示例Example

// Example 1:
// Create a horizontal CScrollBar control as a child window of CMyDialog
// class (a CDialog-derived class). The scroll bar is NOT visible until the
// call ShowScrollBar() is made. m_ScrollBarHorz is of type CScrollBar class,
// and it is a member variable in CMyDialog class.
VERIFY(m_ScrollBarHorz.Create(SBS_HORZ | SBS_TOPALIGN | WS_CHILD,
                              CRect(5, 5, 100, 30), this, IDC_SCROLLBARCTRL));

m_ScrollBarHorz.ShowScrollBar();

// Example 2:
// Create a vertical CScrollBar control as a child window of CMyDialog
// class (a CDialog-derived class). m_ScrollBarVert is of type CScrollBar
// class, and it is a member variable in CMyDialog class.
VERIFY(m_ScrollBarVert.Create(SBS_VERT | SBS_LEFTALIGN | WS_CHILD |
                                  WS_VISIBLE,
                              CRect(5, 30, 30, 130), this, IDC_SCROLLBARCTRL));

CScrollBar::CScrollBarCScrollBar::CScrollBar

构造 CScrollBar 对象。Constructs a CScrollBar object.

CScrollBar();

备注Remarks

构造对象后,调用 Create 成员函数以创建并初始化 Windows 滚动条。After constructing the object, call the Create member function to create and initialize the Windows scroll bar.

示例Example

CScrollBar m_ScrollBarHorz;

CScrollBar::EnableScrollBarCScrollBar::EnableScrollBar

启用或禁用滚动条的一个或两个箭头。Enables or disables one or both arrows of a scroll bar.

BOOL EnableScrollBar(UINT nArrowFlags = ESB_ENABLE_BOTH);

参数Parameters

nArrowFlagsnArrowFlags
指定是启用还是禁用滚动箭头以及启用或禁用的箭头。Specifies whether the scroll arrows are enabled or disabled and which arrows are enabled or disabled. 此参数可以是下列值之一:This parameter can be one of the following values:

  • ESB_ENABLE_BOTH 启用滚动条的两个箭头。ESB_ENABLE_BOTH Enables both arrows of a scroll bar.

  • ESB_DISABLE_LTUP 禁用水平滚动条的左箭头或垂直滚动条的向上箭头。ESB_DISABLE_LTUP Disables the left arrow of a horizontal scroll bar or the up arrow of a vertical scroll bar.

  • ESB_DISABLE_RTDN 禁用水平滚动条的向右箭头或垂直滚动条的向下箭头。ESB_DISABLE_RTDN Disables the right arrow of a horizontal scroll bar or the down arrow of a vertical scroll bar.

  • ESB_DISABLE_BOTH 禁用滚动条的两个箭头。ESB_DISABLE_BOTH Disables both arrows of a scroll bar.

返回值Return Value

如果按指定启用或禁用箭头,则为非零值;否则为0,指示箭头已经处于请求状态或发生错误。Nonzero if the arrows are enabled or disabled as specified; otherwise 0, which indicates that the arrows are already in the requested state or that an error occurred.

示例Example

请参阅CScrollBar:: SetScrollRange的示例。See the example for CScrollBar::SetScrollRange.

CScrollBar:: GetScrollBarInfoCScrollBar::GetScrollBarInfo

检索 SCROLLBARINFO 结构维护的有关滚动条的信息。Retrieves the information that the SCROLLBARINFO structure maintains about a scroll bar.

BOOL GetScrollBarInfo(PSCROLLBARINFO pScrollInfo) const;

参数Parameters

pScrollInfopScrollInfo
指向SCROLLBARINFO结构的指针。A pointer to the SCROLLBARINFO structure.

返回值Return Value

如果成功,则返回 TRUE,否则返回 FALSE。Returns TRUE on success, FALSE on failure.

备注Remarks

此成员函数模拟SBM_SCROLLBARINFO消息的功能,如 Windows SDK 中所述。This member function emulates the functionality of the SBM_SCROLLBARINFO message, as described in the Windows SDK.

CScrollBar::GetScrollInfoCScrollBar::GetScrollInfo

检索 SCROLLINFO 结构维护的有关滚动条的信息。Retrieves the information that the SCROLLINFO structure maintains about a scroll bar.

BOOL GetScrollInfo(
    LPSCROLLINFO lpScrollInfo,
    UINT nMask = SIF_ALL);

参数Parameters

lpScrollInfolpScrollInfo
指向SCROLLINFO结构的指针。A pointer to a SCROLLINFO structure. 有关此结构的详细信息,请参阅 Windows SDK。See the Windows SDK for more information about this structure.

nMasknMask
指定要检索的滚动条参数。Specifies the scroll bar parameters to retrieve. 典型用法 SIF_ALL,指定 SIF_PAGE、SIF_POS、SIF_TRACKPOS 和 SIF_RANGE 的组合。Typical usage, SIF_ALL, specifies a combination of SIF_PAGE, SIF_POS, SIF_TRACKPOS, and SIF_RANGE. SCROLLINFO有关 nMask 值的详细信息,请参阅。See SCROLLINFO for more information on the nMask values.

返回值Return Value

如果消息检索到了任何值,则返回值为 TRUE。If the message retrieved any values, the return is TRUE. 否则为 FALSE。Otherwise, it is FALSE.

备注Remarks

GetScrollInfo使应用程序能够使用32位滚动位置。GetScrollInfo enables applications to use 32-bit scroll positions.

SCROLLINFO结构包含滚动条的相关信息,包括最小和最大滚动位置、页面大小和滚动框(滚动块)的位置。The SCROLLINFO structure contains information about a scroll bar, including the minimum and maximum scrolling positions, the page size, and the position of the scroll box (the thumb). 有关 SCROLLINFO 更改结构默认值的详细信息,请参阅 Windows SDK 中的结构主题。See the SCROLLINFO structure topic in the Windows SDK for more information about changing the structure defaults.

指示滚动条位置的 MFC Windows 消息处理程序 [CWnd:: OnHScroll 和CWnd:: OnVScroll仅提供16位的位置数据。The MFC Windows message handlers that indicate scroll bar position, [CWnd::OnHScroll, and CWnd::OnVScroll, provide only 16 bits of position data. GetScrollInfoSetScrollInfo 提供32位的滚动条位置数据。GetScrollInfo and SetScrollInfo provide 32 bits of scroll bar position data. 因此,应用程序可以 GetScrollInfo 在处理或时 CWnd::OnHScroll 调用 CWnd::OnVScroll 以获取32位滚动条位置数据。Thus, an application can call GetScrollInfo while processing either CWnd::OnHScroll or CWnd::OnVScroll to obtain 32-bit scroll bar position data.

示例Example

请参阅CWnd:: OnHScroll的示例。See the example for CWnd::OnHScroll.

CScrollBar::GetScrollLimitCScrollBar::GetScrollLimit

检索滚动条的最大滚动位置。Retrieves the maximum scrolling position of the scroll bar.

int GetScrollLimit();

返回值Return Value

指定滚动条成功的最大位置;否则为0。Specifies the maximum position of a scroll bar if successful; otherwise 0.

示例Example

请参阅CWnd:: OnHScroll的示例。See the example for CWnd::OnHScroll.

CScrollBar::GetScrollPosCScrollBar::GetScrollPos

检索滚动框的当前位置。Retrieves the current position of a scroll box.

int GetScrollPos() const;

返回值Return Value

如果成功,则指定滚动框的当前位置;否则为0。Specifies the current position of the scroll box if successful; otherwise 0.

备注Remarks

当前位置是依赖于当前滚动范围的相对值。The current position is a relative value that depends on the current scrolling range. 例如,如果滚动范围为100到200,滚动框处于条形的中间,则当前位置为150。For example, if the scrolling range is 100 to 200 and the scroll box is in the middle of the bar, the current position is 150.

示例Example

请参阅CWnd:: OnHScroll的示例。See the example for CWnd::OnHScroll.

CScrollBar::GetScrollRangeCScrollBar::GetScrollRange

将给定滚动条的当前最小和最大滚动条位置复制到lpMinPoslpMaxPos指定的位置。Copies the current minimum and maximum scroll-bar positions for the given scroll bar to the locations specified by lpMinPos and lpMaxPos.

void GetScrollRange(
    LPINT lpMinPos,
    LPINT lpMaxPos) const;

参数Parameters

lpMinPoslpMinPos
指向要接收最小位置的整数变量。Points to the integer variable that is to receive the minimum position.

lpMaxPoslpMaxPos
指向要接收最大位置的整数变量。Points to the integer variable that is to receive the maximum position.

备注Remarks

滚动条控件的默认范围为空(这两个值均为0)。The default range for a scroll-bar control is empty (both values are 0).

示例Example

请参阅CWnd:: OnHScroll的示例。See the example for CWnd::OnHScroll.

CScrollBar::SetScrollInfoCScrollBar::SetScrollInfo

设置 SCROLLINFO 结构维护的有关滚动条的信息。Sets the information that the SCROLLINFO structure maintains about a scroll bar.

BOOL SetScrollInfo(
    LPSCROLLINFO lpScrollInfo,
    BOOL bRedraw = TRUE);

参数Parameters

lpScrollInfolpScrollInfo
指向SCROLLINFO结构的指针。A pointer to a SCROLLINFO structure.

bRedrawbRedraw
指定是否应重绘滚动条以反映新的信息。Specifies whether the scroll bar should be redrawn to reflect the new information. 如果bRedraw为 TRUE,则将重绘滚动条。If bRedraw is TRUE, the scroll bar is redrawn. 如果该值为 FALSE,则不会重新绘制。If it is FALSE, it is not redrawn. 默认情况下,将重新绘制滚动条。The scroll bar is redrawn by default.

返回值Return Value

如果成功,则返回 TRUE。If successful, the return is TRUE. 否则为 FALSE。Otherwise, it is FALSE.

备注Remarks

必须提供结构参数所需的值 SCROLLINFO ,包括标志值。You must provide the values required by the SCROLLINFO structure parameters, including the flag values.

SCROLLINFO 结构包含滚动条的相关信息,包括最小和最大滚动位置、页面大小和滚动框(滚动块)的位置。The SCROLLINFO structure contains information about a scroll bar, including the minimum and maximum scrolling positions, the page size, and the position of the scroll box (the thumb). 有关更改结构默认值的详细信息,请参阅 Windows SDK 中的SCROLLINFO结构主题。See the SCROLLINFO structure topic in the Windows SDK for more information about changing the structure defaults.

示例Example

// Set SCROLLINFO for the scroll bar. m_ScrollBarHorz is of type
// CScrollBar class, and it is a member variable in CMyDialog class.
SCROLLINFO info;
info.cbSize = sizeof(SCROLLINFO);
info.fMask = SIF_ALL;
info.nMin = 0;
info.nMax = 10;
info.nPage = 2;
info.nPos = 5;
info.nTrackPos = 2;
m_ScrollBarHorz.SetScrollInfo(&info);

CScrollBar::SetScrollPosCScrollBar::SetScrollPos

将滚动框的当前位置设置为nPos指定的位置,如果指定,则重绘滚动条以反映新位置。Sets the current position of a scroll box to that specified by nPos and, if specified, redraws the scroll bar to reflect the new position.

int SetScrollPos(
    int nPos,
    BOOL bRedraw = TRUE);

参数Parameters

nPosnPos
指定滚动框的新位置。Specifies the new position for the scroll box. 它必须在滚动范围内。It must be within the scrolling range.

bRedrawbRedraw
指定是否应重绘滚动条以反映新位置。Specifies whether the scroll bar should be redrawn to reflect the new position. 如果bRedraw为 TRUE,则将重绘滚动条。If bRedraw is TRUE, the scroll bar is redrawn. 如果该值为 FALSE,则不会重新绘制。If it is FALSE, it is not redrawn. 默认情况下,将重新绘制滚动条。The scroll bar is redrawn by default.

返回值Return Value

如果成功,则指定滚动框的上一个位置。否则为0。Specifies the previous position of the scroll box if successful; otherwise 0.

备注Remarks

每当对另一个函数的后续调用重绘滚动条时,将bRedraw设置为 FALSE,以避免滚动条在短时间间隔内重绘两次。Set bRedraw to FALSE whenever the scroll bar will be redrawn by a subsequent call to another function to avoid having the scroll bar redrawn twice within a short interval.

示例Example

请参阅CScrollBar:: SetScrollRange的示例。See the example for CScrollBar::SetScrollRange.

CScrollBar::SetScrollRangeCScrollBar::SetScrollRange

设置给定滚动条的最小和最大位置值。Sets minimum and maximum position values for the given scroll bar.

void SetScrollRange(
    int nMinPos,
    int nMaxPos,
    BOOL bRedraw = TRUE);

参数Parameters

nMinPosnMinPos
指定最小滚动位置。Specifies the minimum scrolling position.

nMaxPosnMaxPos
指定最大滚动位置。Specifies the maximum scrolling position.

bRedrawbRedraw
指定是否应重绘滚动条以反映更改。Specifies whether the scroll bar should be redrawn to reflect the change. 如果bRedraw为 TRUE,则将重绘滚动条;如果为 FALSE,则不重新绘制。If bRedraw is TRUE, the scroll bar is redrawn; if FALSE, it is not redrawn. 默认情况下,将重绘此值。It is redrawn by default.

备注Remarks

nMinPosnMaxPos设置为0,以隐藏标准滚动条。Set nMinPos and nMaxPos to 0 to hide standard scroll bars.

不调用此函数可在处理滚动条通知消息时隐藏滚动条。Do not call this function to hide a scroll bar while processing a scroll-bar notification message.

如果对 SetScrollRange 成员函数的调用立即调用 SetScrollPos ,请将bRedraw中的设置 SetScrollPos 为0,以防滚动条被两次重绘。If a call to SetScrollRange immediately follows a call to the SetScrollPos member function, set bRedraw in SetScrollPos to 0 to prevent the scroll bar from being redrawn twice.

NMinPosnMaxPos指定的值之间的差异不得大于32767。The difference between the values specified by nMinPos and nMaxPos must not be greater than 32,767. 滚动条控件的默认范围为空( nMinPosnMaxPos均为0)。The default range for a scroll-bar control is empty (both nMinPos and nMaxPos are 0).

示例Example

// Sets minimum (0) and maximum (10) position values for the
// CScrollBar control. m_ScrollBarVert is of type CScrollBar class,
// and it is a member variable in CMyDialog class.
m_ScrollBarVert.SetScrollRange(0, 10);

// Set the position of the scroll box.
m_ScrollBarVert.SetScrollPos(5);

// Disable the down arrow of the scroll bar. By default, both arrows
// are enabled.
m_ScrollBarVert.EnableScrollBar(ESB_DISABLE_DOWN);

CScrollBar:: ShowScrollBarCScrollBar::ShowScrollBar

显示或隐藏滚动条。Shows or hides a scroll bar.

void ShowScrollBar(BOOL bShow = TRUE);

参数Parameters

bShowbShow
指定滚动条是显示还是隐藏。Specifies whether the scroll bar is shown or hidden. 如果此参数为 TRUE,则显示滚动条;否则为隐藏状态。If this parameter is TRUE, the scroll bar is shown; otherwise it is hidden.

备注Remarks

在处理滚动条通知消息时,应用程序不应调用此函数隐藏滚动条。An application should not call this function to hide a scroll bar while processing a scroll-bar notification message.

示例Example

请参阅CScrollBar:: Create的示例。See the example for CScrollBar::Create.

另请参阅See also

CWnd 类CWnd Class
层次结构图Hierarchy Chart
CWnd 类CWnd Class
CButton 类CButton Class
CComboBox 类CComboBox Class
CEdit 类CEdit Class
CListBox 类CListBox Class
CStatic 类CStatic Class
CDialog 类CDialog Class