上下滚动条vScrollBar怎么使用呀?我把滚动条放在PictureBox 里面,怎么让它们里面的内容发生滚动呢?

2024-12-27 01:00:47
推荐回答(1个)
回答1:

单就VScrollBar控件的使用来说,楼主可以参考下面的代码,但这可能并不能解决你的问题因为这还需要做很多其它的工作。

不过我还是愿意帮你解决问题而写了如下的一段代码,这是一个控件,可以放到窗体上运行后查看滚动条的效果,但或许有人能给楼主一更为完美的解决方案:

C# code

public partial class UserControl1 : UserControl { [StructLayout(LayoutKind.Sequential)] internal struct RECT { public int left; public int top; public int right; public int bottom; public RECT(int left, int top, int right, int bottom) { this.left = left; this.top = top; this.right = right; this.bottom = bottom; } } [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] private static extern bool ScrollWindow(IntPtr hWnd, int nXAmount, int nYAmount, ref RECT rectScrollRegion, ref RECT rectClip); private System.Windows.Forms.VScrollBar vScrollBar1; private int m_Top; public UserControl1() { this.InitializeComponent(); this.SetStyle(ControlStyles.ResizeRedraw, true); m_Top = 0; this.vScrollBar1.Scroll += new ScrollEventHandler(vScrollBar1_Scroll); } private void InitializeComponent() { this.vScrollBar1 = new System.Windows.Forms.VScrollBar(); this.SuspendLayout(); // // vScrollBar1 // this.vScrollBar1.Location = new System.Drawing.Point(275, 57); this.vScrollBar1.Name = "vScrollBar1"; this.vScrollBar1.Size = new System.Drawing.Size(17, 80); this.vScrollBar1.TabIndex = 0; // // UserControl1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.Controls.Add(this.vScrollBar1); this.Name = "UserControl1"; this.Size = new System.Drawing.Size(304, 174); this.ResumeLayout(false); } private void setScrollBar() { this.vScrollBar1.Top = 0; this.vScrollBar1.Left = this.Width - this.vScrollBar1.Width; this.vScrollBar1.Height = this.Height; this.vScrollBar1.Maximum = 1000 - this.Height; this.vScrollBar1.LargeChange = this.Height; this.vScrollBar1.Maximum += this.vScrollBar1.LargeChange + 1; this.m_Top = this.vScrollBar1.Value; } private RECT getBodyRect() { int left = 0; int top = 0; int width = this.Width-this.vScrollBar1.Width; int height = this.Height; RECT rect = new RECT(left, top, left + width, top+height); return rect; } void vScrollBar1_Scroll(object sender, ScrollEventArgs e) { RECT vSRect = this.getBodyRect(); ScrollWindow(this.Handle, 0, -(e.NewValue - m_Top), ref vSRect, ref vSRect); m_Top = e.NewValue; } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); Rectangle rect = new Rectangle(0, -m_Top, this.Width - this.vScrollBar1.Width, 1000); e.Graphics.DrawEllipse(SystemPens.ControlDark, rect); } protected override void OnLayout(LayoutEventArgs e) { if (e.AffectedControl == this) { this.setScrollBar(); } base.OnLayout(e); } }
百度把缩进都给屏蔽了。你将就看吧,或者复制到ide里格式化一下吧