帮我写个简单的OnPaint的C# Windows FORMS代码

来源:百度知道 编辑:UC知道 时间:2024/06/04 08:14:31
在窗体上托个 按钮 这是我写的代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace 高级画图
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{

Graphics formGraphics=this.CreateGraphics();
SolidBrush redbrush = new SolidBrush(Color.Red);
formGraphics.FillRectangle(redbrush,20,20,100,100);
}
protected override void OnPaint(PaintEventArgs e)
{

Graphics formGraphics = e.Graphics;
button1_Click(null,null);
}
}
}

private bool _bbuttonPush = false;

private void button1_Click(object sender, EventArgs e)
{
this._bbuttonPush = true;
}

protected override void OnPaint(PaintEventArgs e)
{
if (this._bbuttonPush)// 修正过
{
SolidBrush redbrush = new SolidBrush(Color.Red);
e.Graphics.FillRectangle(redbrush, 20, 20, 100, 100);
}
base.OnPaint(e);
}