C# 将滚动条应用于pictureBox

C# 将滚动条应用于pictureBox,c#,scrollbar,picturebox,C#,Scrollbar,Picturebox,我已经看了这个和其他网站上的例子来创建这个pictureBox。但我仍然不明白为什么不显示滚动条。我想我遗漏了一个小但重要的细节:P。下面的代码应该是功能齐全的 using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApplication1 { partial class Form2 { private System.ComponentModel.IContainer com

我已经看了这个和其他网站上的例子来创建这个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;
    }
}
使用系统图;
使用System.Windows.Forms;
命名空间Windows窗体应用程序1
{
部分类别表格2
{
private System.ComponentModel.IContainer components=null;
受保护的覆盖无效处置(布尔处置)
{
if(处理和(组件!=null))
{
组件。Dispose();
}
基地。处置(处置);
}
私有void InitializeComponent()
{
this.panel1=new System.Windows.Forms.Panel();
这个.SuspendLayout();
// 
//小组1
// 
//this.panel1.Dock=System.Windows.Forms.DockStyle.Fill;
this.panel1.Location=新系统图纸点(0,0);
this.panel1.Name=“panel1”;
this.panel1.Size=新系统图纸尺寸(300300);
this.panel1.TabIndex=0;
this.panel1.Paint+=new System.Windows.Forms.PaintEventHandler(this.mpicureboxpaint_-Paint);
// 
//表格2
// 
this.AutoScroll=true;
此.AutoScaleDimensions=新系统.Drawing.SizeF(6F,13F);
this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize=新系统.Drawing.Size(391297);
this.Controls.Add(this.panel1);
this.Name=“Form2”;
this.Text=“Form2”;
此选项为.resume布局(false);
}
私有void mpicturexpaint_Paint(对象发送方,PaintEventArgs e)
{
钢笔黑色钢笔=新钢笔(画笔黑色);
黑笔。宽度=1.0F;
blackPen.LineJoin=System.Drawing.Drawing2D.LineJoin.斜面;
对于(int i=0;i<100;i++)
{
对于(int j=0;j<100;j++)
{
e、 图形。DrawRectangle(黑色笔,
新矩形(i*20,j*20,2,2));
}
}
blackPen.Dispose();
}
private System.Windows.Forms.Panel panel1;
}
}
编辑:更新了我的代码(删除了this.Controls.AddRange()和滚动条)。现在,pictureBox已固定到面板,但如果图形位于边界之外,面板将无法识别。所以仍然没有滚动条

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;


        }
    }
编辑2:我刚刚意识到我甚至不需要一个图片盒,所以我创建了一个新的例子,它更容易理解。如果设置了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;


        }
    }
使用系统图; 使用System.Windows.Forms; 命名空间Windows窗体应用程序1 { 公共部分类表单2:表单 { 公共表格2() { 初始化组件(); } //只需在Y轴的X轴上写下所需的点数 //这也将定义面板的大小 int PointsX=10; int PointsY=5; 私有void InitializeComponent() { this.panel1=new System.Windows.Forms.Panel(); 这个.SuspendLayout(); // //小组1 // //this.panel1.Dock=DockStyle.Fill; this.panel1.Location=新系统图纸点(1,1); this.panel1.Name=“panel1”; this.panel1.Size=new System.Drawing.Size((PointsX*20),(PointsY*20));//是300300 this.panel1.TabIndex=0; this.panel1.Paint+=new System.Windows.Forms.PaintEventHandler(this.mpicureboxpaint_-Paint); // //表格2 // 此.AutoScaleDimensions=新系统.Drawing.SizeF(6F,13F); this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font; this.AutoScroll=true; this.ClientSize=新系统.Drawing.Size(400300); this.Controls.Add(this.panel1); this.Name=“Form2”; this.Text=“Form2”; 此选项为.resume布局(false); } 私有void mpicturexpaint_Paint(对象发送方,PaintEventArgs e) { 钢笔黑色钢笔=新钢笔(画笔黑色); 黑笔。宽度=1.0F; blackPen.LineJoin=System.Drawing.Drawing2D.LineJoin.斜面; 对于(int i=0;i
this.Controls.AddRange()调用会将pb从面板移动到窗体。现在它与面板重叠,除了pb之外,你什么也看不到。不要那样做。不要添加滚动条,面板已经知道如何显示它们。“只需缩小屏幕就可以看到它们。@HansPassant根据您的评论,我已经更新了代码,但ScrollBars仍然存在一些问题,无法停靠pb。使其大于面板以获得滚动条。@HansPassant我之所以获得滚动条是因为“使其更大”,但这并不能解决我的问题。所以我举了一个新的例子来阐明我的观点,这正是我所要寻找的。我原以为c#中有预定义的东西,但这也行。好听,祝你好运,编码愉快:-)