C#中具有overriden OnPaint方法的PictureBox

本文关键字:方法 PictureBox OnPaint overriden | 更新日期: 2023-09-27 18:29:11

我想在PictureBox中绘制一些小图片(一行4 x 32px的图像),所以我应该重写OnPaint方法还是需要制作扩展PictureBox的新组件?我尝试过这个,它在Java中有效,但在这里无效:

        this.pictureBox1 = new System.Windows.Forms.PictureBox()
        {
            protected override void OnPaint(PaintEventArgs e)
            {
               // If there is an image and it has a location, 
               // paint it when the Form is repainted.
                Graphics g = e.Graphics;
                // Draw a string on the PictureBox.
                g.DrawString("Test, is that working?",
                new Font("Arial", 10), System.Drawing.Brushes.Blue, new Point(30, 30));
             }
        }

InitializeComponent方法的完整代码:

   private void InitializeComponent()
    {
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Tools));
        this.pictureBox1 = new System.Windows.Forms.PictureBox()
        {
            protected override void OnPaint(PaintEventArgs e)
            {
               // If there is an image and it has a location, 
               // paint it when the Form is repainted.
                Graphics g = e.Graphics;
                // Draw a string on the PictureBox.
                g.DrawString("Test, is that working?",
                new Font("Arial", 10), System.Drawing.Brushes.Blue, new Point(30, 30));
             }
        }
        this.vscrollb = new System.Windows.Forms.VScrollBar();
        this.vScrollBar1 = new System.Windows.Forms.VScrollBar();
        ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
        this.SuspendLayout();
        // 
        // pictureBox1
        // 
        this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Left;
        this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
        this.pictureBox1.InitialImage = null;
        this.pictureBox1.Location = new System.Drawing.Point(0, 0);
        this.pictureBox1.Name = "pictureBox1";
        this.pictureBox1.Size = new System.Drawing.Size(264, 262);
        this.pictureBox1.TabIndex = 0;
        this.pictureBox1.TabStop = false;
        this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
        // 
        // vscrollb
        // 
        this.vscrollb.Location = new System.Drawing.Point(0, 0);
        this.vscrollb.Name = "vscrollb";
        this.vscrollb.Size = new System.Drawing.Size(20, 80);
        this.vscrollb.TabIndex = 0;
        // 
        // vScrollBar1
        // 
        this.vScrollBar1.Dock = System.Windows.Forms.DockStyle.Right;
        this.vScrollBar1.Location = new System.Drawing.Point(267, 0);
        this.vScrollBar1.Name = "vScrollBar1";
        this.vScrollBar1.Size = new System.Drawing.Size(17, 262);
        this.vScrollBar1.TabIndex = 1;
        this.vScrollBar1.Scroll += new System.Windows.Forms.ScrollEventHandler(this.HandleScroll);
        // 
        // Tools
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.BackColor = System.Drawing.Color.Black;
        this.ClientSize = new System.Drawing.Size(284, 262);
        this.Controls.Add(this.vScrollBar1);
        this.Controls.Add(this.pictureBox1);
        this.Name = "Tools";
        this.Text = "Tools";
        ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
        this.ResumeLayout(false);
    }

C#中具有overriden OnPaint方法的PictureBox

这不是有效的C#代码。重写像OnPaint()这样的虚拟方法是可以的,但只能在从PictureBox派生的类中这样做。这很好,编译后,你会自动将新控件添加到工具箱中,这样你就可以将其放在表单上

然而,这并不是必须的。您可以简单地为控件实现"绘制"事件。你已经这样做了,你把它命名为pictureBox1_Paint()。把你的代码移到那里。

其他重要的指针:永远不要编辑InitializeComponent()。它是由设计者自动生成的。一旦您修改了表单的设计,您将丢失在那里编写的任何代码。这也是使表单不可设计的一个非常好的方法,当设计器加载表单时会触发异常。如果你覆盖了OnPaint(),那么调用base是很重要的。OnPaint()。以便正常的PictureBox管道继续工作。包括绘制图像和引发"绘制"事件。一定要至少按照教程或读一本关于Winforms编程的书,如果你不这样做,会有很多尝试,而且大多是错误。

我建议从PictureBox继承并在那里添加您的逻辑。因此,您不必将逻辑添加到它不属于的地方(父控件)。

public class SpecialPictureBox : PictureBox
{
    protected override void OnPaint(PaintEventArgs e)
    {
        // if you want to execute the original PaintBox logic before you execute your own code, use the next line of code.
        base.OnPaint(e);
        // now do whatever you want
    }
}

然后,您可以在任何需要的地方使用SpecialPictureBox

编辑:base.OnPaint添加到代码示例