次の方法で共有


ScrollBar クラス

スクロール バー コントロールの基本機能を実装します。

この型のすべてのメンバの一覧については、ScrollBar メンバ を参照してください。

System.Object
   System.MarshalByRefObject
      System.ComponentModel.Component
         System.Windows.Forms.Control
            System.Windows.Forms.ScrollBar
               System.Windows.Forms.HScrollBar
               System.Windows.Forms.VScrollBar

MustInherit Public Class ScrollBar
   Inherits Control
[C#]
public abstract class ScrollBar : Control
[C++]
public __gc __abstract class ScrollBar : public Control
[JScript]
public abstract class ScrollBar extends Control

スレッドセーフ

この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。

解説

通常、 ScrollBar から直接継承することはありません。独自のスクロール バー クラスを作成するには、 VScrollBar クラスまたは HScrollBar クラスから継承します。

スクロール バー コントロールの値の範囲を調整するには、 Minimum プロパティと Maximum プロパティを設定します。スクロール ボックスの移動距離を調整するには、 SmallChange プロパティと LargeChange プロパティを設定します。スクロール ボックスの開始点を調整するには、コントロールが最初に表示されるときに Value プロパティを設定します。

メモ   スクロール ボックスはスクロールつまみとも呼ばれます。

使用例

PictureBox コントロールに水平スクロール バーと垂直スクロール バーを追加し、コントロールで DoubleClick イベントが発生したときにピクチャ ボックスに Image を読み込む例を次に示します。ピクチャ ボックスをダブルクリックすると、いつでも新しいイメージを読み込むことができます。イメージのサイズがコントロールより大きい場合は、スクロール バーが表示されます。そのスクロール バーをスクロールして、コントロール内に収まらないイメージを表示させることができます。この例では、 VScrollBarHScrollBar が端にドッキングされた PictureBoxForm 上に作成されていることを前提にしています。この例は、 System.Drawing 名前空間への参照が追加されていることも前提にしています。 VScrollBarHScrollBar の両方に対して HandleScroll メソッドが Scroll イベント ハンドラのデリゲートとして設定されていることを確認してください。この例を拡張するための追加コードについては、 LargeChangeSmallChangeMaximumMinimumValue の各メンバを参照してください。スクロール バーのプロパティを、その親コントロールのプロパティに基づいて設定することもできます。たとえば、 Maximum の値を、 PictureBox に割り当てられた ImageHeight または Width の値に合わせて設定するメソッドを、この例に追加することもできます。また、 LargeChange の値を PictureBox の高さまたは幅に設定します。なお、スクロール バーの高さまたは幅はこの値から差し引きます。この設定により、イメージの範囲を越えてスクロールすることができなくなり、 LargeChange は、イメージの表示領域をピクチャ ボックスに表示された領域と同じ距離だけ移動します。

 
Private Sub pictureBox1_DoubleClick(sender As [Object], e As EventArgs) _
  Handles pictureBox1.DoubleClick
   ' Open the dialog box so the user can select a new image.
   If openFileDialog1.ShowDialog() <> DialogResult.Cancel Then
      ' Display the image in the PictureBox.
      pictureBox1.Image = Image.FromFile(openFileDialog1.FileName)
      Me.DisplayScrollBars()
      Me.SetScrollBarValues()
   End If
End Sub      
      
Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize
   ' If the PictureBox has an image, see if it needs
   ' scrollbars and refresh the image. 
   If Not (pictureBox1.Image Is Nothing) Then
      Me.DisplayScrollBars()
      Me.SetScrollBarValues()
      Me.Refresh()
   End If
End Sub 

Public Sub DisplayScrollBars()
   ' If the image is wider than the PictureBox, show the HScrollBar.
   If pictureBox1.Width > pictureBox1.Image.Width - _
     Me.vScrollBar1.Width Then
      hScrollBar1.Visible = False
   Else
      hScrollBar1.Visible = True
   End If
         
   ' If the image is taller than the PictureBox, show the VScrollBar.
   If pictureBox1.Height > pictureBox1.Image.Height - _
     Me.hScrollBar1.Height Then
      vScrollBar1.Visible = False
   Else
      vScrollBar1.Visible = True
   End If
End Sub
      
      
Private Sub HandleScroll(sender As [Object], se As ScrollEventArgs) _
  Handles vScrollBar1.Scroll, hScrollBar1.Scroll
   ' Create a graphics object and draw a portion 
   ' of the image in the PictureBox. 
   Dim g As Graphics = pictureBox1.CreateGraphics()
   
   g.DrawImage(pictureBox1.Image, New Rectangle(0, 0, pictureBox1.Right - vScrollBar1.Width, _
     pictureBox1.Bottom - hScrollBar1.Height), _
     New Rectangle(hScrollBar1.Value, vScrollBar1.Value, pictureBox1.Right - vScrollBar1.Width, _
     pictureBox1.Bottom - hScrollBar1.Height), GraphicsUnit.Pixel)
         
   pictureBox1.Update()
End Sub
      
      
Public Sub SetScrollBarValues()
   ' Set the Maximum, Minimum, LargeChange and SmallChange properties.
   Me.vScrollBar1.Minimum = 0
   Me.hScrollBar1.Minimum = 0
   
   ' If the offset does not make the Maximum less than zero, set its value.
   If Me.pictureBox1.Image.Size.Width - pictureBox1.ClientSize.Width > 0 Then
      Me.hScrollBar1.Maximum = Me.pictureBox1.Image.Size.Width - _
        pictureBox1.ClientSize.Width
   End If
   ' If the VScrollBar is visible, adjust the Maximum of the 
   ' HSCrollBar to account for the width of the VScrollBar.
   If Me.vScrollBar1.Visible Then
      Me.hScrollBar1.Maximum += Me.vScrollBar1.Width
   End If
   Me.hScrollBar1.LargeChange = Me.hScrollBar1.Maximum / 10
   Me.hScrollBar1.SmallChange = Me.hScrollBar1.Maximum / 20
   ' Adjust the Maximum value to make the raw Maximum value attainable by user interaction.
   Me.hScrollBar1.Maximum += Me.hScrollBar1.LargeChange
         
   ' If the offset does not make the Maximum less than zero, set its value.
   If Me.pictureBox1.Image.Size.Height - pictureBox1.ClientSize.Height > 0 Then
      Me.vScrollBar1.Maximum = Me.pictureBox1.Image.Size.Height - _
        pictureBox1.ClientSize.Height
   End If
   ' If the HScrollBar is visible, adjust the Maximum of the 
   ' VSCrollBar to account for the width of the HScrollBar.
   If Me.hScrollBar1.Visible Then
      Me.vScrollBar1.Maximum += Me.hScrollBar1.Height
   End If
   Me.vScrollBar1.LargeChange = Me.vScrollBar1.Maximum / 10
   Me.vScrollBar1.SmallChange = Me.vScrollBar1.Maximum / 20
   ' Adjust the Maximum value to make the raw Maximum value attainable by user interaction.
   Me.vScrollBar1.Maximum += Me.vScrollBar1.LargeChange
End Sub

[C#] 
private void pictureBox1_DoubleClick (Object sender, EventArgs e)
{
   // Open the dialog box so the user can select a new image.
   if(openFileDialog1.ShowDialog() != DialogResult.Cancel)
   {
      // Display the image in the PictureBox.
      pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
      this.DisplayScrollBars();
      this.SetScrollBarValues();
   }
}
 
protected void Form1_Resize (Object sender, EventArgs e)
{
   /* If the PictureBox has an image, see if it needs 
      scrollbars and refresh the image. */
   if(pictureBox1.Image != null)
   {
      this.DisplayScrollBars();
      this.SetScrollBarValues();
      this.Refresh();
   }
}
 
public void DisplayScrollBars()
{
   // If the image is wider than the PictureBox, show the HScrollBar.
   if (pictureBox1.Width > pictureBox1.Image.Width - this.vScrollBar1.Width)
   {
      hScrollBar1.Visible = false;
   }
   else
   {
      hScrollBar1.Visible = true;
   }
 
   // If the image is taller than the PictureBox, show the VScrollBar.
   if (pictureBox1.Height > pictureBox1.Image.Height - this.hScrollBar1.Height)
   {
      vScrollBar1.Visible = false;
   }
   else
   {
      vScrollBar1.Visible = true;
   }
}
 
private void HandleScroll(Object sender, ScrollEventArgs se)
{
   /* Create a graphics object and draw a portion 
      of the image in the PictureBox. */
   Graphics g = pictureBox1.CreateGraphics();
   
   g.DrawImage(pictureBox1.Image, 
     new Rectangle(0, 0, pictureBox1.Right - vScrollBar1.Width, 
     pictureBox1.Bottom - hScrollBar1.Height), 
     new Rectangle(hScrollBar1.Value, vScrollBar1.Value, 
     pictureBox1.Right - vScrollBar1.Width,
     pictureBox1.Bottom - hScrollBar1.Height), 
     GraphicsUnit.Pixel);

     pictureBox1.Update();
}

public void SetScrollBarValues() 
{
   // Set the Maximum, Minimum, LargeChange and SmallChange properties.
   this.vScrollBar1.Minimum = 0;
   this.hScrollBar1.Minimum = 0;
   // If the offset does not make the Maximum less than zero, set its value. 
   if( (this.pictureBox1.Image.Size.Width - pictureBox1.ClientSize.Width) > 0)
   {
      this.hScrollBar1.Maximum = this.pictureBox1.Image.Size.Width - pictureBox1.ClientSize.Width;
   }
   /* If the VScrollBar is visible, adjust the Maximum of the 
      HSCrollBar to account for the width of the VScrollBar. */
   if(this.vScrollBar1.Visible)
   {
      this.hScrollBar1.Maximum += this.vScrollBar1.Width;
   }
   this.hScrollBar1.LargeChange = this.hScrollBar1.Maximum / 10;
   this.hScrollBar1.SmallChange = this.hScrollBar1.Maximum / 20;
   // Adjust the Maximum value to make the raw Maximum value attainable by user interaction.
   this.hScrollBar1.Maximum += this.hScrollBar1.LargeChange;
     
   // If the offset does not make the Maximum less than zero, set its value.    
   if( (this.pictureBox1.Image.Size.Height - pictureBox1.ClientSize.Height) > 0)
   {
      this.vScrollBar1.Maximum = this.pictureBox1.Image.Size.Height - pictureBox1.ClientSize.Height;
   }
   /* If the HScrollBar is visible, adjust the Maximum of the 
      VSCrollBar to account for the width of the HScrollBar.*/
   if(this.hScrollBar1.Visible)
   {
      this.vScrollBar1.Maximum += this.hScrollBar1.Height;
   }
   this.vScrollBar1.LargeChange = this.vScrollBar1.Maximum / 10;
   this.vScrollBar1.SmallChange = this.vScrollBar1.Maximum / 20;
   // Adjust the Maximum value to make the raw Maximum value attainable by user interaction.
   this.vScrollBar1.Maximum += this.vScrollBar1.LargeChange;
}

[C++] 
private:
    void pictureBox1_DoubleClick (Object *sender, EventArgs *e)    {
       // Open the dialog box so the user can select a new image.
       if (openFileDialog1->ShowDialog() != DialogResult::Cancel) {
       // Display the image in the PictureBox.
       pictureBox1->Image = System::Drawing::Image::FromFile(openFileDialog1->FileName);
       this->DisplayScrollBars();
           this->SetScrollBarValues();
       }
    };

    void Form1_Resize (Object *sender, EventArgs *e) {
           /* If the PictureBox has an image, see if it needs
          scrollbars and refresh the image. */
       if (pictureBox1->Image != null) {
          this->SetScrollBarValues();
          this->DisplayScrollBars();
          this->Refresh();
       }
    };

    void DisplayScrollBars() {
           // If the image is wider than the PictureBox, show the HScrollBar.
       if (pictureBox1->Width > pictureBox1->Image->Width - this->vScrollBar1->Width) {
          hScrollBar1->Visible = false;
       } else     {
          hScrollBar1->Visible = true;
       }

           // If the image is taller than the PictureBox, show the VScrollBar.
       if (pictureBox1->Height > pictureBox1->Image->Height - this->hScrollBar1->Height) {
          vScrollBar1->Visible = false;
       } else {
       vScrollBar1->Visible = true;
       }
    };

    void HandleScroll(Object *sender, ScrollEventArgs *se) {
       /* Create a graphics object and draw a portion
          of the image in the PictureBox. */
       Graphics __gc *g = pictureBox1->CreateGraphics();
       g->DrawImage(pictureBox1->Image, Rectangle(0, 0, pictureBox1->Right - vScrollBar1->Width,
             pictureBox1->Bottom - hScrollBar1->Height),
             Rectangle(hScrollBar1->Value, vScrollBar1->Value, pictureBox1->Right - vScrollBar1->Width,
             pictureBox1->Bottom - hScrollBar1->Height), GraphicsUnit::Pixel);
    };



public:
    void SetScrollBarValues() {
       // Set the Maximum, Minimum, LargeChange and SmallChange properties.
           this->vScrollBar1->Minimum = 0;
           this->hScrollBar1->Minimum = 0;
           // If the offset does not make the Maximum less than zero, set its value.
           if( (this->pictureBox1->Image->Size.Width - pictureBox1->ClientSize.Width) > 0)           {
              this->hScrollBar1->Maximum = this->pictureBox1->Image->Size.Width - pictureBox1->ClientSize.Width;
           }
           /* If the VScrollBar is visible, adjust the Maximum of the
              HSCrollBar to account for the width of the VScrollBar. */
           if(this->vScrollBar1->Visible)
           {
              this->hScrollBar1->Maximum += this->vScrollBar1->Width;
           }
           this->hScrollBar1->LargeChange = this->hScrollBar1->Maximum / 10;
           this->hScrollBar1->SmallChange = this->hScrollBar1->Maximum / 20;
           // Adjust the Maximum value to make the raw Maximum value attainable by user interaction.
           this->hScrollBar1->Maximum += this->hScrollBar1->LargeChange;

          // If the offset does not make the Maximum less than zero, set its value.
          if( (this->pictureBox1->Image->Size.Height - pictureBox1->ClientSize.Height) > 0)
          {
             this->vScrollBar1->Maximum = this->pictureBox1->Image->Size.Height - pictureBox1->ClientSize.Height;
          }
          /* If the HScrollBar is visible, adjust the Maximum of the
             VSCrollBar to account for the width of the HScrollBar.*/
         if(this->hScrollBar1->Visible)
         {
            this->vScrollBar1->Maximum += this->hScrollBar1->Height;
         }
         this->vScrollBar1->LargeChange = this->vScrollBar1->Maximum / 10;
         this->vScrollBar1->SmallChange = this->vScrollBar1->Maximum / 20;
         // Adjust the Maximum value to make the raw Maximum value attainable by user interaction.
         this->vScrollBar1->Maximum += this->vScrollBar1->LargeChange;
    };

[JScript] 
private function pictureBox1_DoubleClick (sender : Object, 
                                            e : EventArgs)
 {
    // Open the dialog so the user can select a new image.
    if(openFileDialog1.ShowDialog() != System.Windows.Forms.DialogResult.Cancel)
    {
       // Display the image in the PictureBox.
       pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
       this.DisplayScrollBars();
       this.SetScrollBarValues();
    }
 }
 
 private function Form1_Resize (sender : Object, 
                                  e : EventArgs)
 {
    /* If the PictureBox has an image, see if it needs 
       scrollbars and refresh the image. */
    if(pictureBox1.Image != null)
    {
       this.SetScrollBarValues();
       this.DisplayScrollBars();
       this.Refresh();
    }
 }
 
 public function DisplayScrollBars()
 {
    // If the image is wider than the PictureBox, show the HScrollBar.
    if (pictureBox1.Width > pictureBox1.Image.Width - this.vScrollBar1.Width)
    {
       hScrollBar1.Visible = false;
    }
    else
    {
       hScrollBar1.Visible = true;
    }
 
    // If the image is taller than the PictureBox, show the VScrollBar.
       if (pictureBox1.Height > pictureBox1.Image.Height - this.hScrollBar1.Height)
    {
       vScrollBar1.Visible = false;
    }
    else
    {
       vScrollBar1.Visible = true;
    }
 }
 
 private function HandleScroll(sender : Object, 
                              se : ScrollEventArgs)
 {
   /* Create a graphics object and draw a portion 
      of the image in the PictureBox. */
   Graphics g = pictureBox1.CreateGraphics();
   
   g.DrawImage(pictureBox1.Image, 
     new Rectangle(0, 0, pictureBox1.Right - vScrollBar1.Width, 
     pictureBox1.Bottom - hScrollBar1.Height), 
     new Rectangle(hScrollBar1.Value, vScrollBar1.Value, 
     pictureBox1.Right - vScrollBar1.Width,
     pictureBox1.Bottom - hScrollBar1.Height), 
     GraphicsUnit.Pixel);

     pictureBox1.Update();
 }



public function SetScrollBarValues() {
   // Set the Maximum, Minimum, LargeChange and SmallChange properties.
   this.vScrollBar1.Minimum = 0;
   this.hScrollBar1.Minimum = 0;
   // If the offset does not make the Maximum less than zero, set its value. 
   if( (this.pictureBox1.Image.Size.Width - pictureBox1.ClientSize.Width) > 0)
   {
      this.hScrollBar1.Maximum = this.pictureBox1.Image.Size.Width - pictureBox1.ClientSize.Width;
   }
   /* If the VScrollBar is visible, adjust the Maximum of the 
      HSCrollBar to account for the width of the VScrollBar. */
   if(this.vScrollBar1.Visible)
   {
      this.hScrollBar1.Maximum += this.vScrollBar1.Width;
   }
   this.hScrollBar1.LargeChange = this.hScrollBar1.Maximum / 10;
   this.hScrollBar1.SmallChange = this.hScrollBar1.Maximum / 20;
   // Adjust the Maximum value to make the raw Maximum value attainable by user interaction.
   this.hScrollBar1.Maximum += this.hScrollBar1.LargeChange;
     
   // If the offset does not make the Maximum less than zero, set its value.    
   if( (this.pictureBox1.Image.Size.Height - pictureBox1.ClientSize.Height) > 0)
   {
      this.vScrollBar1.Maximum = this.pictureBox1.Image.Size.Height - pictureBox1.ClientSize.Height;
   }
   /* If the HScrollBar is visible, adjust the Maximum of the 
      VSCrollBar to account for the width of the HScrollBar.*/
   if(this.hScrollBar1.Visible)
   {
      this.vScrollBar1.Maximum += this.hScrollBar1.Height;
   }
   this.vScrollBar1.LargeChange = this.vScrollBar1.Maximum / 10;
   this.vScrollBar1.SmallChange = this.vScrollBar1.Maximum / 20;
   // Adjust the Maximum value to make the raw Maximum value attainable by user interaction.
   this.vScrollBar1.Maximum += this.vScrollBar1.LargeChange;
 }

必要条件

名前空間: System.Windows.Forms

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

アセンブリ: System.Windows.Forms (System.Windows.Forms.dll 内)

参照

ScrollBar メンバ | System.Windows.Forms 名前空間 | VScrollBar | HScrollBar