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. 此範例是在假設您有一個Form Panel包含,其Button上含有具有的。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.


如果X控制項Y已從其開始位置(0,0)中滾動,則所抓取的和座標值為負數。The X and Y coordinate values retrieved are negative if the control has scrolled away from its starting position (0,0). 當您設定這個屬性時,一定要指派正值XY values 來設定相對於開始位置的捲軸位置。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,則會將 scroll 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 , {-100,0}會傳回 {-200,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.