最近看了hscrollbar和picturebox的配合使用的例程,他好像就是通过移动picturebox的位置来把整个图像逐渐显示出来的,大家在使用hscrollbar的时候都是这样的吗?有没有不移动picturebox而达到相同目标的办法?例程:using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace testScrollBarApp
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Panel MyPanel;
private System.Windows.Forms.HScrollBar MyHSBar;
private System.Windows.Forms.VScrollBar MyVSBar;
private System.Windows.Forms.PictureBox MyPicBox;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
MyHSBar.Maximum = MyPicBox.Width - MyPanel.Width + MyVSBar.Width;
MyVSBar.Maximum = MyPicBox.Height - MyPanel.Height + MyHSBar.Height;
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
} #region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.MyPanel = new System.Windows.Forms.Panel();
this.MyPicBox = new System.Windows.Forms.PictureBox();
this.MyHSBar = new System.Windows.Forms.HScrollBar();
this.MyVSBar = new System.Windows.Forms.VScrollBar();
this.MyPanel.SuspendLayout();
this.SuspendLayout();
//
// MyPanel
//
this.MyPanel.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.MyPanel.Controls.AddRange(new System.Windows.Forms.Control[] {
this.MyPicBox});
this.MyPanel.Location = new System.Drawing.Point(-1, -1);
this.MyPanel.Name = "MyPanel";
this.MyPanel.Size = new System.Drawing.Size(281, 265);
this.MyPanel.TabIndex = 0;
//
// MyPicBox
//
this.MyPicBox.Image = ((System.Drawing.Bitmap)(resources.GetObject("MyPicBox.Image")));
this.MyPicBox.Name = "MyPicBox";
this.MyPicBox.Size = new System.Drawing.Size(630, 439);
this.MyPicBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.MyPicBox.TabIndex = 0;
this.MyPicBox.TabStop = false;
//
// MyHSBar
//
this.MyHSBar.Location = new System.Drawing.Point(0, 260);
this.MyHSBar.Name = "MyHSBar";
this.MyHSBar.Size = new System.Drawing.Size(288, 16);
this.MyHSBar.TabIndex = 1;
this.MyHSBar.Scroll += new System.Windows.Forms.ScrollEventHandler(this.MyHSBar_Scroll);
//
// MyVSBar
//
this.MyVSBar.Location = new System.Drawing.Point(281, 0);
this.MyVSBar.Name = "MyVSBar";
this.MyVSBar.Size = new System.Drawing.Size(16, 278);
this.MyVSBar.TabIndex = 2;
this.MyVSBar.Scroll += new System.Windows.Forms.ScrollEventHandler(this.MyVSBar_Scroll);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(296, 277);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.MyVSBar,
this.MyHSBar,
this.MyPanel});
this.MaximizeBox = false;
this.Name = "Form1";
this.Text = "testScrollBar";
this.MyPanel.ResumeLayout(false);
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void MyHSBar_Scroll(object sender, System.Windows.Forms.ScrollEventArgs e)
{
MyPicBox.Left = -MyHSBar.Value;
} private void MyVSBar_Scroll(object sender, System.Windows.Forms.ScrollEventArgs e)
{
MyPicBox.Top = -MyVSBar.Value;
}
}
}
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace testScrollBarApp
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Panel MyPanel;
private System.Windows.Forms.HScrollBar MyHSBar;
private System.Windows.Forms.VScrollBar MyVSBar;
private System.Windows.Forms.PictureBox MyPicBox;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
MyHSBar.Maximum = MyPicBox.Width - MyPanel.Width + MyVSBar.Width;
MyVSBar.Maximum = MyPicBox.Height - MyPanel.Height + MyHSBar.Height;
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
} #region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.MyPanel = new System.Windows.Forms.Panel();
this.MyPicBox = new System.Windows.Forms.PictureBox();
this.MyHSBar = new System.Windows.Forms.HScrollBar();
this.MyVSBar = new System.Windows.Forms.VScrollBar();
this.MyPanel.SuspendLayout();
this.SuspendLayout();
//
// MyPanel
//
this.MyPanel.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.MyPanel.Controls.AddRange(new System.Windows.Forms.Control[] {
this.MyPicBox});
this.MyPanel.Location = new System.Drawing.Point(-1, -1);
this.MyPanel.Name = "MyPanel";
this.MyPanel.Size = new System.Drawing.Size(281, 265);
this.MyPanel.TabIndex = 0;
//
// MyPicBox
//
this.MyPicBox.Image = ((System.Drawing.Bitmap)(resources.GetObject("MyPicBox.Image")));
this.MyPicBox.Name = "MyPicBox";
this.MyPicBox.Size = new System.Drawing.Size(630, 439);
this.MyPicBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.MyPicBox.TabIndex = 0;
this.MyPicBox.TabStop = false;
//
// MyHSBar
//
this.MyHSBar.Location = new System.Drawing.Point(0, 260);
this.MyHSBar.Name = "MyHSBar";
this.MyHSBar.Size = new System.Drawing.Size(288, 16);
this.MyHSBar.TabIndex = 1;
this.MyHSBar.Scroll += new System.Windows.Forms.ScrollEventHandler(this.MyHSBar_Scroll);
//
// MyVSBar
//
this.MyVSBar.Location = new System.Drawing.Point(281, 0);
this.MyVSBar.Name = "MyVSBar";
this.MyVSBar.Size = new System.Drawing.Size(16, 278);
this.MyVSBar.TabIndex = 2;
this.MyVSBar.Scroll += new System.Windows.Forms.ScrollEventHandler(this.MyVSBar_Scroll);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(296, 277);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.MyVSBar,
this.MyHSBar,
this.MyPanel});
this.MaximizeBox = false;
this.Name = "Form1";
this.Text = "testScrollBar";
this.MyPanel.ResumeLayout(false);
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void MyHSBar_Scroll(object sender, System.Windows.Forms.ScrollEventArgs e)
{
MyPicBox.Left = -MyHSBar.Value;
} private void MyVSBar_Scroll(object sender, System.Windows.Forms.ScrollEventArgs e)
{
MyPicBox.Top = -MyVSBar.Value;
}
}
}
在Windows 窗体快速入门教程
http://chs.gotdotnet.com/quickstart/winforms/
可查詢HScrollBar或VScrollBar即有使用的詳細例子,代碼如下:
http://chs.gotdotnet.com/quickstart/util/srcview.aspx?path=/quickstart/winforms/Samples/ControlReference/ScrollBarCtl/ScrollBarCtl.src