簡體   English   中英

將滾動條應用於pictureBox

[英]applying scrollbars to 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,我將看到所有的點,但是沒有滾動條;如果未設置,我將看到滾動條,但是沒有所有的點。 我需要的是一個面板,該面板會根據顯示的點數自動調整大小,並采用固定的窗口大小。 所以所有的點都是可見的,並且我有滾動條。

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;


        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM