在WinForm中,可以使用Invalidate()方法来触发重绘窗体。在需要重绘的事件中调用Invalidate()方法,系统会自动调用窗体的Paint事件来进行重绘。
以下是一个简单的示例:
using System;using System.Drawing;using System.Windows.Forms;public class MyForm : Form{ public MyForm() { // 注册Paint事件 this.Paint += MyForm_Paint; } private void MyForm_Paint(object sender, PaintEventArgs e) { // 绘制重绘的逻辑 Graphics g = e.Graphics; Pen pen = new Pen(Color.Red, 2); g.DrawLine(pen, 0, 0, this.Width, this.Height); } private void button1_Click(object sender, EventArgs e) { // 在按钮点击事件中调用Invalidate方法触发重绘 this.Invalidate(); }}在上述示例中,注册Paint事件,并在事件处理方法中编写重绘的逻辑。然后,当按钮点击时调用Invalidate()方法触发重绘。
注意:Invalidate()方法会尽快地调用Paint事件来进行重绘,但具体的重绘时间可能会有一定的延迟,因此不能保证立即重绘。

