高手赐教,PictureBox 中加入滚动条,如何实现呢? 在 PictureBox 中显示图片时,图片比 PictureBox 的可见区域大,我想在 PictureBox 中加入滚动条,如何实现呢? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 在窗口上画滚动条: private void vScrollBar1_Scroll(object sender, System.Windows.Forms.ScrollEventArgs e) { this.pbPictureBox.Top -= e.NewValue-vScrollBar1.Value; //改变控件的高度 this.pbPictureBox.Height += e.NewValue-vScrollBar1.Value; //改变控件的高度 this.vScrollBar1.Maximum = this.pbPictureBox.Height; //改变垂直的滚动条的长度 }水平改变的一样! using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;namespace V_Hscroll{ /// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.HScrollBar hScrollBar1; private System.Windows.Forms.VScrollBar vScrollBar1; private System.Windows.Forms.PictureBox pictureBox1; private System.Windows.Forms.Panel panel1; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; public Form1() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); // // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // } /// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1)); this.hScrollBar1 = new System.Windows.Forms.HScrollBar(); this.vScrollBar1 = new System.Windows.Forms.VScrollBar(); this.pictureBox1 = new System.Windows.Forms.PictureBox(); this.panel1 = new System.Windows.Forms.Panel(); this.panel1.SuspendLayout(); this.SuspendLayout(); // // hScrollBar1 // this.hScrollBar1.Dock = System.Windows.Forms.DockStyle.Top; this.hScrollBar1.Location = new System.Drawing.Point(20, 0); this.hScrollBar1.Name = "hScrollBar1"; this.hScrollBar1.Size = new System.Drawing.Size(212, 20); this.hScrollBar1.TabIndex = 0; this.hScrollBar1.Scroll += new System.Windows.Forms.ScrollEventHandler(this.hScrollBar1_Scroll); // // vScrollBar1 // this.vScrollBar1.Dock = System.Windows.Forms.DockStyle.Left; this.vScrollBar1.Location = new System.Drawing.Point(0, 0); this.vScrollBar1.Name = "vScrollBar1"; this.vScrollBar1.Size = new System.Drawing.Size(20, 184); this.vScrollBar1.TabIndex = 1; // // pictureBox1 // this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image"))); this.pictureBox1.Location = new System.Drawing.Point(24, 32); this.pictureBox1.Name = "pictureBox1"; this.pictureBox1.Size = new System.Drawing.Size(136, 96); this.pictureBox1.TabIndex = 2; this.pictureBox1.TabStop = false; // // panel1 // this.panel1.Controls.Add(this.hScrollBar1); this.panel1.Controls.Add(this.vScrollBar1); this.panel1.Controls.Add(this.pictureBox1); this.panel1.Location = new System.Drawing.Point(48, 48); this.panel1.Name = "panel1"; this.panel1.Size = new System.Drawing.Size(232, 184); this.panel1.TabIndex = 3; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(8, 18); this.ClientSize = new System.Drawing.Size(292, 268); this.Controls.Add(this.panel1); this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); this.panel1.ResumeLayout(false); this.ResumeLayout(false); } #endregion /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void Form1_Load(object sender, System.EventArgs e) { this.pictureBox1 .Width =this.pictureBox1 .Image .Width ; this.pictureBox1 .Height =this.pictureBox1 .Image .Height ; this.pictureBox1 .Top =this.pictureBox1 .Left=0; this.vScrollBar1 .Minimum =0; this.hScrollBar1 .Minimum=0; if(this.pictureBox1 .Image .Width -this.pictureBox1 .ClientSize.Width>0) { this.hScrollBar1 .Maximum =this.pictureBox1 .Image .Width -this.pictureBox1 .ClientSize.Width; } if(this.vScrollBar1 .Visible ==true) this.hScrollBar1 .Maximum +=this.vScrollBar1 .Width ; this.hScrollBar1 .LargeChange =this.hScrollBar1 .Maximum /10; this.hScrollBar1 .SmallChange =this.hScrollBar1 .Minimum /20; pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage ; } private void hScrollBar1_Scroll(object sender, System.Windows.Forms.ScrollEventArgs e) { this.pictureBox1 .Left -=e.NewValue -this.hScrollBar1 .Value ; this.pictureBox1 .Invalidate (); } }} 把你的PictuerBox放在一个Panel上面,并且设置PictuerBox的SizeMode为AutoSize还有什么问题吗?? 13880079673(CMonkey) 的方法是不行的,我已经这样做了。 回复人: wurongfei001(阿飞) ( ) 信誉:100 2003-12-25 16:55:00 得分:0 13880079673(CMonkey) 的方法是不行的,我已经这样做了。 不可能吧,有什么问题,为什么不行,说一下呢?我在我这儿都已经测试过了 回复13880079673(CMonkey),谢谢你的支持,我也不知道,这样做不会出现滚动条。也就是说不能滚动显示。sashilover(新手上路) 的方法是正确的解决办法,谢谢各位,我要结贴了。再次谢谢。 老大阿,只要设置panel的AutoScroll属性为True ,就对了,要是不对我跳楼 C#多个线程能同时访问一个方法吗? 软件打包问题 GridView问题,在线等你的答案.... 如何让弹出来的messeageBox有滚动条??? 请教Server.UrlDecode在WINFORM中的用法 关于正则表达式——项匹配的问题 问一下DAAB几个版本之间的关系? 一个经典的问题,想知道答案!!! 想知道,使用API GetWindowText 与SPY++ 里输出的 Caption值的区别 關於eval方法 求连连看算法! windows定义的一些常量在C#中怎么引用?如MB_OK, MB_YESNO等等???????
private void vScrollBar1_Scroll(object sender, System.Windows.Forms.ScrollEventArgs e)
{ this.pbPictureBox.Top -= e.NewValue-vScrollBar1.Value; //改变控件的高度
this.pbPictureBox.Height += e.NewValue-vScrollBar1.Value; //改变控件的高度 this.vScrollBar1.Maximum = this.pbPictureBox.Height; //改变垂直的滚动条的长度 }水平改变的一样!
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace V_Hscroll
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.HScrollBar hScrollBar1;
private System.Windows.Forms.VScrollBar vScrollBar1;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Panel panel1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.hScrollBar1 = new System.Windows.Forms.HScrollBar();
this.vScrollBar1 = new System.Windows.Forms.VScrollBar();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.panel1 = new System.Windows.Forms.Panel();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// hScrollBar1
//
this.hScrollBar1.Dock = System.Windows.Forms.DockStyle.Top;
this.hScrollBar1.Location = new System.Drawing.Point(20, 0);
this.hScrollBar1.Name = "hScrollBar1";
this.hScrollBar1.Size = new System.Drawing.Size(212, 20);
this.hScrollBar1.TabIndex = 0;
this.hScrollBar1.Scroll += new System.Windows.Forms.ScrollEventHandler(this.hScrollBar1_Scroll);
//
// vScrollBar1
//
this.vScrollBar1.Dock = System.Windows.Forms.DockStyle.Left;
this.vScrollBar1.Location = new System.Drawing.Point(0, 0);
this.vScrollBar1.Name = "vScrollBar1";
this.vScrollBar1.Size = new System.Drawing.Size(20, 184);
this.vScrollBar1.TabIndex = 1;
//
// pictureBox1
//
this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
this.pictureBox1.Location = new System.Drawing.Point(24, 32);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(136, 96);
this.pictureBox1.TabIndex = 2;
this.pictureBox1.TabStop = false;
//
// panel1
//
this.panel1.Controls.Add(this.hScrollBar1);
this.panel1.Controls.Add(this.vScrollBar1);
this.panel1.Controls.Add(this.pictureBox1);
this.panel1.Location = new System.Drawing.Point(48, 48);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(232, 184);
this.panel1.TabIndex = 3;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(8, 18);
this.ClientSize = new System.Drawing.Size(292, 268);
this.Controls.Add(this.panel1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.panel1.ResumeLayout(false);
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void Form1_Load(object sender, System.EventArgs e)
{
this.pictureBox1 .Width =this.pictureBox1 .Image .Width ;
this.pictureBox1 .Height =this.pictureBox1 .Image .Height ;
this.pictureBox1 .Top =this.pictureBox1 .Left=0;
this.vScrollBar1 .Minimum =0;
this.hScrollBar1 .Minimum=0;
if(this.pictureBox1 .Image .Width -this.pictureBox1 .ClientSize.Width>0)
{
this.hScrollBar1 .Maximum =this.pictureBox1 .Image .Width -this.pictureBox1 .ClientSize.Width;
}
if(this.vScrollBar1 .Visible ==true)
this.hScrollBar1 .Maximum +=this.vScrollBar1 .Width ;
this.hScrollBar1 .LargeChange =this.hScrollBar1 .Maximum /10;
this.hScrollBar1 .SmallChange =this.hScrollBar1 .Minimum /20;
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage ;
} private void hScrollBar1_Scroll(object sender, System.Windows.Forms.ScrollEventArgs e)
{
this.pictureBox1 .Left -=e.NewValue -this.hScrollBar1 .Value ;
this.pictureBox1 .Invalidate ();
}
}
}
13880079673(CMonkey) 的方法是不行的,我已经这样做了。
不可能吧,有什么问题,为什么不行,说一下呢?我在我这儿都已经测试过了