将滚动条应用于pictureBox

时间:2013-06-17 09:36:11

标签: c# scrollbar picturebox

我已经看过这个和其他网站上的例子来创建这个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,我会看到所有点但没有滚动条,如果没有设置我看到滚动条而不是所有点。我需要的是一个面板,它根据应显示的点数自动调整大小,固定的窗口大小。所以所有点都可见,我有滚动条。

1 个答案:

答案 0 :(得分: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;


        }
    }