ScrollableControl.AutoScrollPosition 属性


获取或设置自动滚动定位的位置。Gets or sets the location of the auto-scroll position.

 property System::Drawing::Point AutoScrollPosition { System::Drawing::Point get(); void set(System::Drawing::Point value); };
public System.Drawing.Point AutoScrollPosition { get; set; }
member this.AutoScrollPosition : System.Drawing.Point with get, set
Public Property AutoScrollPosition As Point



表示自动滚动位置的 Point,单位为像素。A Point that represents the auto-scroll position in pixels.



下面的代码示例使用 ScrollableControl 派生类 Panel 并将一个按钮添加到可滚动区域的左上角。The following code example uses the ScrollableControl derived class Panel and adds a button to the upper left corner of the scrollable area. 该示例允许 AutoScrollPosition确定的偏移量。The example allows for the offset determined by the AutoScrollPosition. 该示例是根据假设您有一个包含具有 ButtonPanelForm 来编写的。The example was written under the assumption that you have a Form that contains a Panel with a Button on it. 若要启用自动滚动,请将按钮置于 Panel的工作区之外。To enable auto-scrolling, place the button outside of the client area of the Panel.

   void button1_Click( Object^ /*sender*/, EventArgs^ /*e*/ )
      /* Add a button to top left corner of the
            * scrollable area, allowing for the offset. */
      panel1->AutoScroll = true;
      Button^ myButton = gcnew Button;
      myButton->Location = Point(0 + panel1->AutoScrollPosition.X,0 + panel1->AutoScrollPosition.Y);
      panel1->Controls->Add( myButton );
private void button1_Click(object sender, EventArgs e)
   /* Add a button to top left corner of the 
    * scrollable area, allowing for the offset. */
   panel1.AutoScroll = true;
   Button myButton = new Button();
   myButton.Location = new Point(
      0 + panel1.AutoScrollPosition.X, 
      0 + panel1.AutoScrollPosition.Y);
Private Sub button1_Click(sender As Object, _
   e As EventArgs) Handles button1.Click
   ' Add a button to top left corner of the 
   ' scrollable area, allowing for the offset. 
   panel1.AutoScroll = True
   Dim myButton As New Button()
   myButton.Location = New Point( _
      0 + panel1.AutoScrollPosition.X, _
      0 + panel1.AutoScrollPosition.Y)
End Sub


AutoScrollPosition 属性表示可滚动控件的可见部分的位置。The AutoScrollPosition property represents the location of the visible portion of a scrollable control. 使用此属性可更改显示的控件部分。Use this property to change the portion of the control that is displayed.

以编程方式将控件添加到窗体时,请使用 AutoScrollPosition 属性将控件放置在当前可查看滚动区域的内部或外部。When adding controls programmatically to a form, use the AutoScrollPosition property to position the control either inside or outside of the current viewable scroll area.


如果控件从其开始位置(0,0)滚动,则检索到的 XY 坐标值都是负数。The X and Y coordinate values retrieved are negative if the control has scrolled away from its starting position (0,0). 设置此属性时,必须始终分配正 XY 值,以设置相对于起始位置的滚动位置。When you set this property, you must always assign positive X and Y values to set the scroll position relative to the starting position. 例如,如果有水平滚动条并将 x 和 y 设置为200,则会将滚动200像素向右移动;如果随后将 x 和 y 设置为100,则滚动将向左滚动100像素,因为你将其设置为100像素,远离起始位置。For example, if you have a horizontal scroll bar and you set x and y to 200, you move the scroll 200 pixels to the right; if you then set x and y to 100, the scroll appears to jump the left by 100 pixels, because you are setting it 100 pixels away from the starting position. 在第一种情况下,AutoScrollPosition 返回 {-200,0};在第二种情况下,它将返回 {-100,0}。In the first case, AutoScrollPosition returns {-200, 0}; in the second case, it returns {-100,0}.

若要在 AutoScrollPosition 更改时进行检测,请为 Paint 事件创建事件处理程序,将旧位置值保存在私有变量中,并将新值与后续 Paint 事件的旧值进行比较。To detect when AutoScrollPosition changes, create an event handler for the Paint event, save the old position value in a private variable, and compare the new value to the old value on subsequent Paint events.