整理代码块
代码块整理后存储,供后期使用
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);
}
}
}
标签:控件,using,缓冲,Image,private,Graphics,new,绘制,Rectangle
From: https://www.cnblogs.com/Katakana/p/17615653.html