0

我查看了这个网站和其他网站上的示例来创建这个图片框。但是我仍然无法弄清楚,为什么没有显示滚动条。我想我错过了一个小但重要的细节: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() 和滚动条)。现在图片框固定在面板上,但面板无法识别,如果图纸在边框之外。所以仍然没有滚动条。

Edit2:刚刚意识到我什至不需要图片框,所以我创建了一个更容易理解的新示例。如果设置了 this.panel1.Dock,我会看到所有点但没有滚动条,如果未设置,我会看到滚动条但不是所有点。我需要的是一个面板,它会根据应显示的点数自动调整大小,并以固定的窗口大小。所以所有点都是可见的,我有滚动条。

4

1 回答 1

1
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;


        }
    }
于 2013-06-18T13:11:43.263 回答