次の方法で共有


方法 : フォームの内容をスクロールする

更新 : 2007 年 11 月

Panel コントロールを使用して、このコントロールの AutoScroll プロパティを true に設定すると、その内容を垂直方向にスクロールできます。ただし、垂直方向と水平方向の両方にスクロールするには、スクロール バーを追加して、それらのプロパティを定義する必要があります。

使用例

水平と垂直のスクロール バー コントロールを使用して、フォーム上のイメージを上下左右にスクロールする方法をこの例で示します。スクロール バーをクリックすると、スクロール バーの値に従って、パネルの Left および Left プロパティが変更されます。その結果、スクロール効果が生まれます。

この例では、フォーム内に大きなイメージを作成します。このイメージは、直交する対角線が引かれた四角形です。

ValueChanged イベント用のイベント処理コードでは、変更後のスクロール バーの値によって、PanelLeft または Left プロパティが変更されます。水平スクロール バーで右方向にスクロールしたとき、および垂直スクロール バーで下方向にスクロールしたときに、これらのプロパティの値は小さくなります。逆に、反対方向へ動かすと、値が大きくなります。

Imports System
Imports System.Drawing
Imports System.Windows.Forms

Public Class Scrolling
   Inherits System.Windows.Forms.Form
   Private WithEvents HScrollBar1 As System.Windows.Forms.HScrollBar
   Private WithEvents VScrollBar1 As System.Windows.Forms.VScrollBar
   Private Panel1 As System.Windows.Forms.Panel
   Private Label1 As System.Windows.Forms.Label
   Private Label2 As System.Windows.Forms.Label
   Private Label3 As System.Windows.Forms.Label
   Private Label4 As System.Windows.Forms.Label
   Private PictureBox1 As System.Windows.Forms.PictureBox
   Private MainMenu1 As System.Windows.Forms.MainMenu


   Public Sub New()

      InitializeComponent()

      Dim bmp As New Bitmap(PictureBox1.Width, PictureBox1.Height)
      Dim blackpen As New Pen(Color.Black)
      Dim g As Graphics = Graphics.FromImage(bmp)
      g.FillRectangle(New SolidBrush(Color.Gainsboro), 0, 0, bmp.Width, bmp.Height)
      g.DrawLine(blackpen, 0, 0, bmp.Width, bmp.Height)
      g.DrawLine(blackpen, 0, bmp.Height, bmp.Width, 0)
      g.Dispose()

      Me.PictureBox1.Image = bmp
      Me.PictureBox1.Size = bmp.Size
      Me.VScrollBar1.Maximum = bmp.Height + Me.ClientSize.Height
      Me.HScrollBar1.Maximum = bmp.Width + Me.ClientSize.Width
      Me.HScrollBar1.Value = 0

      Me.Text = "x = 0, y = 0"
    End Sub

    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        MyBase.Dispose(disposing)
    End Sub

    Private Sub InitializeComponent()
        Me.MainMenu1 = New System.Windows.Forms.MainMenu
        Me.HScrollBar1 = New System.Windows.Forms.HScrollBar
        Me.VScrollBar1 = New System.Windows.Forms.VScrollBar
        Me.Panel1 = New System.Windows.Forms.Panel
        Me.PictureBox1 = New System.Windows.Forms.PictureBox
        Me.Label1 = New System.Windows.Forms.Label
        Me.Label2 = New System.Windows.Forms.Label
        Me.Label3 = New System.Windows.Forms.Label
        Me.Label4 = New System.Windows.Forms.Label
        ' 
        ' HScrollBar1
        ' 
        Me.HScrollBar1.LargeChange = 240
        Me.HScrollBar1.Location = New System.Drawing.Point(0, 255)
        Me.HScrollBar1.Maximum = 493
        Me.HScrollBar1.Size = New System.Drawing.Size(227, 13)
        ' 
        ' VScrollBar1
        ' 
        Me.VScrollBar1.LargeChange = 268
        Me.VScrollBar1.Location = New System.Drawing.Point(227, 0)
        Me.VScrollBar1.Maximum = 549
        Me.VScrollBar1.Size = New System.Drawing.Size(13, 255)
        ' 
        ' Panel1
        ' 
        Me.Panel1.Controls.Add(Me.PictureBox1)
        Me.Panel1.Controls.Add(Me.Label1)
        Me.Panel1.Controls.Add(Me.Label2)
        Me.Panel1.Controls.Add(Me.Label3)
        Me.Panel1.Controls.Add(Me.Label4)
        Me.Panel1.Size = New System.Drawing.Size(480, 536)
        ' 
        ' PictureBox1
        ' 
        Me.PictureBox1.Location = New System.Drawing.Point(88, 36)
        Me.PictureBox1.Size = New System.Drawing.Size(304, 464)
        ' 
        ' Label1
        ' 
        Me.Label1.Location = New System.Drawing.Point(8, 8)
        Me.Label1.Size = New System.Drawing.Size(82, 20)
        Me.Label1.Text = "Top Left"
        ' 
        ' Label2
        ' 
        Me.Label2.Location = New System.Drawing.Point(400, 8)
        Me.Label2.Size = New System.Drawing.Size(82, 20)
        Me.Label2.Text = "Top Right"
        ' 
        ' Label3
        ' 
        Me.Label3.Location = New System.Drawing.Point(8, 508)
        Me.Label3.Size = New System.Drawing.Size(82, 20)
        Me.Label3.Text = "Bottom Left"
        ' 
        ' Label4
        ' 
        Me.Label4.Location = New System.Drawing.Point(400, 508)
        Me.Label4.Size = New System.Drawing.Size(82, 20)
        Me.Label4.Text = "Bottom Right"
        ' 
        ' Scrolling
        ' 
        Me.Controls.Add(VScrollBar1)
        Me.Controls.Add(HScrollBar1)
        Me.Controls.Add(Panel1)
        Me.Menu = Me.MainMenu1
        Me.Text = "Scrolling"
    End Sub 


    Shared Sub Main()
        Application.Run(New Scrolling)
    End Sub


    Private Sub HScrollBar1_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles HScrollBar1.ValueChanged
        Me.Panel1.Left = -Me.HScrollBar1.Value

        ' Display the current values in the title bar.
        Me.Text = "x = " + Me.Panel1.Location.X.ToString + ", y = " + Me.Panel1.Location.Y.ToString
    End Sub


    Private Sub VScrollBar1_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles VScrollBar1.ValueChanged
        Me.Panel1.Top = -Me.VScrollBar1.Value

        ' Display the current values in the title bar.          
        Me.Text = "x = " + Me.Panel1.Location.X.ToString + ", y = " + Me.Panel1.Location.Y.ToString
    End Sub
End Class
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Reflection;

namespace Scrolling
{

    public class Scrolling : System.Windows.Forms.Form
    {
        private System.Windows.Forms.HScrollBar hScrollBar1;
        private System.Windows.Forms.VScrollBar vScrollBar1;
        private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.PictureBox pictureBox1;
        private System.Windows.Forms.MainMenu mainMenu1;

        public Scrolling()
        {

            InitializeComponent();

            Bitmap bmp = new Bitmap(pictureBox1.Width,pictureBox1.Height);
            Pen blackpen = new Pen(Color.Black);
            Graphics g = Graphics.FromImage(bmp);
            g.FillRectangle(new SolidBrush(Color.Gainsboro),0,0,bmp.Width,bmp.Height);
            g.DrawLine(blackpen,0,0, bmp.Width, bmp.Height);
            g.DrawLine(blackpen,0,bmp.Height,bmp.Width,0);
            g.Dispose();

            this.pictureBox1.Image = bmp;
            this.pictureBox1.Size = bmp.Size;
            this.vScrollBar1.Maximum = bmp.Height + this.ClientSize.Height;
            this.hScrollBar1.Maximum = bmp.Width + this.ClientSize.Width;
            this.hScrollBar1.Value = 0;

            this.Text = "x = 0, y = 0";
        }

        protected override void Dispose( bool disposing )
        {
            base.Dispose( disposing );
        }

        private void InitializeComponent()
        {
            this.mainMenu1 = new System.Windows.Forms.MainMenu();
            this.hScrollBar1 = new System.Windows.Forms.HScrollBar();
            this.vScrollBar1 = new System.Windows.Forms.VScrollBar();
            this.panel1 = new System.Windows.Forms.Panel();
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            //
            // hScrollBar1
            //
            this.hScrollBar1.LargeChange = 240;
            this.hScrollBar1.Location = new System.Drawing.Point(0, 255);
            this.hScrollBar1.Maximum = 493;
            this.hScrollBar1.Size = new System.Drawing.Size(227, 13);
            this.hScrollBar1.ValueChanged += new System.EventHandler(this.hScrollBar1_ValueChanged);
            //
            // vScrollBar1
            //
            this.vScrollBar1.LargeChange = 268;
            this.vScrollBar1.Location = new System.Drawing.Point(227, 0);
            this.vScrollBar1.Maximum = 549;
            this.vScrollBar1.Size = new System.Drawing.Size(13, 255);
            this.vScrollBar1.ValueChanged += new System.EventHandler(this.vScrollBar1_ValueChanged);
            //
            // panel1
            //
            this.panel1.Controls.Add(this.pictureBox1);
            this.panel1.Controls.Add(this.label1);
            this.panel1.Controls.Add(this.label2);
            this.panel1.Controls.Add(this.label3);
            this.panel1.Controls.Add(this.label4);
            this.panel1.Size = new System.Drawing.Size(480, 536);
            //
            // pictureBox1
            //
            this.pictureBox1.Location = new System.Drawing.Point(88, 36);
            this.pictureBox1.Size = new System.Drawing.Size(304, 464);
            //
            // label1
            //
            this.label1.Location = new System.Drawing.Point(8, 8);
            this.label1.Size = new System.Drawing.Size(82, 20);
            this.label1.Text = "Top Left";
            //
            // label2
            //
            this.label2.Location = new System.Drawing.Point(400, 8);
            this.label2.Size = new System.Drawing.Size(82, 20);
            this.label2.Text = "Top Right";
            //
            // label3
            //
            this.label3.Location = new System.Drawing.Point(8, 508);
            this.label3.Size = new System.Drawing.Size(82, 20);
            this.label3.Text = "Bottom Left";
            //
            // label4
            //
            this.label4.Location = new System.Drawing.Point(400, 508);
            this.label4.Size = new System.Drawing.Size(82, 20);
            this.label4.Text = "Bottom Right";
            //
            // Scrolling
            //
            this.Controls.Add(this.vScrollBar1);
            this.Controls.Add(this.hScrollBar1);
            this.Controls.Add(this.panel1);
            this.Menu = this.mainMenu1;
            this.Text = "Scrolling";

        }

        static void Main()
        {
            Application.Run(new Scrolling());
        }

        private void hScrollBar1_ValueChanged(object sender, System.EventArgs e)
        {
            this.panel1.Left = -this.hScrollBar1.Value;

            // Display the current values in the title bar.
            this.Text = "x = " + this.panel1.Location.X + ", y = " + this.panel1.Location.Y;
        }

        private void vScrollBar1_ValueChanged(object sender, System.EventArgs e)
        {
            this.panel1.Top = -this.vScrollBar1.Value;

            // Display the current values in the title bar.
            this.Text = "x = " + this.panel1.Location.X + ", y = " + this.panel1.Location.Y;
        }
    }
}

コードのコンパイル方法

この例は、次の名前空間への参照を必要とします。

参照

処理手順

方法 : 画面の向きと解像度を変更する

その他の技術情報

.NET Compact Framework の Windows フォーム コントロール