将滚动条应用于pictureBox
本文关键字:pictureBox 应用于 滚动条 | 更新日期: 2023-09-27 18:18:53
我查看了本网站和其他网站上创建这个pictureBox的示例。但我仍然不明白,为什么滚动条不显示。我想我漏掉了一个小而重要的细节:P。下面的代码应该是完全可用的。
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
partial class Form2
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.SuspendLayout();
//
// panel1
//
//this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel1.Location = new System.Drawing.Point(0, 0);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(300, 300);
this.panel1.TabIndex = 0;
this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.mPictureBoxPaint_Paint);
//
// Form2
//
this.AutoScroll = true;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(391, 297);
this.Controls.Add(this.panel1);
this.Name = "Form2";
this.Text = "Form2";
this.ResumeLayout(false);
}
private void mPictureBoxPaint_Paint(object sender, PaintEventArgs e)
{
Pen blackPen = new Pen(Brushes.Black);
blackPen.Width = 1.0F;
blackPen.LineJoin = System.Drawing.Drawing2D.LineJoin.Bevel;
for (int i = 0; i < 100; i++)
{
for (int j = 0; j < 100; j++)
{
e.Graphics.DrawRectangle(blackPen,
new Rectangle(i*20, j*20, 2, 2));
}
}
blackPen.Dispose();
}
private System.Windows.Forms.Panel panel1;
}
}
编辑:更新了我的代码(删除了this.Controls.AddRange()和滚动条)。现在pictureBox固定在面板上,但面板不识别,如果图纸在边界之外。所以还是没有滚动条
Edit2:刚刚意识到我甚至不需要一个图片框,所以我创建了一个更容易遵循的新示例。如果this.panel1。Dock设置,我看到所有的点,但没有滚动条,如果它没有设置,我看到滚动条,但不是所有的点。我需要的是一个面板,根据应该显示的点数自动调整大小,在固定的窗口大小。所以所有的点都是可见的并且我有滚动条
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
//just write number of Points you require on X-Axis of Y-Axis
//This will also define how big the Panel gets
int PointsX = 10;
int PointsY = 5;
private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.SuspendLayout();
//
// panel1
//
//this.panel1.Dock = DockStyle.Fill;
this.panel1.Location = new System.Drawing.Point(1, 1);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size((PointsX*20), (PointsY*20));//Was 300,300
this.panel1.TabIndex = 0;
this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.mPictureBoxPaint_Paint);
//
// Form2
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.AutoScroll = true;
this.ClientSize = new System.Drawing.Size(400, 300);
this.Controls.Add(this.panel1);
this.Name = "Form2";
this.Text = "Form2";
this.ResumeLayout(false);
}
private void mPictureBoxPaint_Paint(object sender, PaintEventArgs e)
{
Pen blackPen = new Pen(Brushes.Black);
blackPen.Width = 1.0F;
blackPen.LineJoin = System.Drawing.Drawing2D.LineJoin.Bevel;
for (int i = 0; i < PointsX; i++)
{
for (int j = 0; j < PointsY; j++)
{
e.Graphics.DrawRectangle(blackPen,
new Rectangle(i*20, j*20, 2, 2));
}
}
blackPen.Dispose();
}
private System.Windows.Forms.Panel panel1;
}
}