PictureBox with overriden OnPaint method in C#(C#中重写OnPaint方法的PictureBox)
问题描述
我想在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#代码。覆盖像OnPaint()这样的虚方法很好,但只能在派生自PictureBox的类中这样做。编译后,您将自动将新控件添加到工具箱中,以便可以将其放在窗体上。
但是,这不是必需的。只需实现控件的Paint事件即可。您已经这样做了,您将其命名为PictureBox1_Paint()。只需将您的代码移到那里。
其他重要提示:切勿编辑InitializeComponent()。它是由设计者自动生成的。一旦修改了表单的设计,您在那里编写的所有代码都将丢失。这也是使表单不可设计的一种非常好的方法,在设计者加载表单时触发异常。如果您确实覆盖了OnPaint(),那么调用base.OnPaint()很重要。以使正常的PictureBox管道继续工作。包括绘制图像和引发绘制事件。请务必至少阅读有关WinForms编程的教程或书籍,否则会有很多尝试,如果不这样做,大多数情况下都会出错。
这篇关于C#中重写OnPaint方法的PictureBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!