【代码块】-控件-双缓冲绘制

整理代码块

代码块整理后存储,供后期使用

using System;
using System.Drawing;
using System.Windows.Forms;

/*
这段代码是用于自定义控件绘图的示例,你可以根据自己的需求进行修改和扩展
*/
public class CustomControl : Control
{
    private bool IsMouseOn = false;
    private bool IsSelect = false;
    private Pen rectanglePen = new Pen(Color.Black, 2);
    private Brush fillBrush = new SolidBrush(Color.LightGray);
    private Rectangle rectangleRec = new Rectangle(10, 10, 100, 100);
    private Image Image;
    private Rectangle imageRec = new Rectangle(20, 20, 80, 80);
    private string Showtext = "Hello, World!";
    private Font Font = new Font("Arial", 12);
    private Brush textBrush = new SolidBrush(Color.Black);
    private PointF textPointF = new PointF(30, 120);

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        this.NewPaint(e);
    }

    private void NewPaint(PaintEventArgs e)
    {
        using (BufferedGraphics Buf = GraphicsManager.Current.Allocate(e.Graphics, e.ClipRectangle))
        {
            Graphics g = Buf.Graphics;
            g.Clear(this.BackColor);

            if (this.IsMouseOn || this.IsSelect)
            {
                g.DrawRectangle(rectanglePen, rectangleRec);
                g.FillRectangle(fillBrush, rectangleRec);
            }

            if (this.Image != null)
            {
                g.DrawImage(Image, imageRec);
            }

            g.DrawString(this.Showtext, this.Font, textBrush, textPointF);

            Buf.Render(e.Graphics);
        }
    }
}
posted @ 2023-08-08 23:04  叫夏洛啊  阅读(11)  评论(0编辑  收藏  举报