[英]Drawing in Windows.Forms
I've got a simple Windows.Forms
Form. 我有一个简单的
Windows.Forms
表单。
I want to fill the pictureBox1
with color Color.Aqua and draw a rectangle. 我想用Color.Aqua填充
pictureBox1
并绘制一个矩形。
However nothing is drown untill I move the Form. 但是,在我移动表格之前,什么都不会淹没。
Why is this? 为什么是这样?
How can I force everything to be drawn without moving the Form? 如何在不移动表格的情况下强制绘制所有内容?
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private readonly Graphics _graphics;
private List<PointF> _points;
private Bitmap _bitmap;
public Form1()
{
InitializeComponent();
_bitmap = new Bitmap(1000, 600);
_graphics = Graphics.FromImage(_bitmap);
pictureBox1.Image = _bitmap;
var timer = new Timer
{
Interval = 1
};
timer.Tick += OnTick;
timer.Start();
Invalidate();
}
private void OnTick(object sender, EventArgs e)
{
_graphics.Clear(Color.Aqua);
_graphics.DrawRectangle(Pens.Black, 10, 10, 10, 10);
Invalidate();
}
}
}
You must subscribe to Paint event of your picturebox and put your drawing code there,something like this: 您必须订阅图片框的Paint事件并将绘图代码放在此处,如下所示:
public Form1()
{
InitializeComponent();
pictureBox1.Paint += PictureBox1_Paint;
}
private void PictureBox1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.Clear(Color.Aqua);
e.Graphics.DrawRectangle(Pens.Black, 10, 10, 10, 10);
}
This event is raised everytime the control needs to be redrawn, so you don't need that Timer
trick or calling Invalidate
每次需要重绘控件时都会引发此事件,因此您不需要该
Timer
技巧或调用Invalidate
I see four problems: 我看到四个问题:
Invalidate()
method is called for the form. Invalidate()
方法。 This should invalidate the PictureBox, but you can do better by just invalidating the PictureBox directly. Image
property of the PictureBox. Image
属性。 Put it all together, and you get this: 放在一起,你会得到:
public partial class Form1 : Form
{
private Timer _timer;
private List<PointF> _points;
public Form1()
{
InitializeComponent();
_timer = new Timer(100);
timer.Tick += OnTick;
timer.Start();
}
private void OnTick(object sender, EventArgs e)
{
pictureBox1.Invalidate();
}
private void PictureBox1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.Clear(Color.Aqua);
e.Graphics.DrawRectangle(Pens.Black, 10, 10, 10, 10);
}
}
You don't need the Invalidate()
call at all, since you're drawing to a buffer ( Bitmap
). 您根本不需要
Invalidate()
调用,因为您正在绘制到缓冲区( Bitmap
)。 Just set the bitmap to pictureBox1.Image
property: 只需将位图设置为
pictureBox1.Image
属性即可:
private void OnTick(object sender, EventArgs e)
{
_graphics.Clear(Color.Aqua);
_graphics.DrawRectangle(Pens.Black, 10, 10, 10, 10);
pictureBox1.Image = _bitmap;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.