有你在真好 的个人博客
C#重绘一个点击带框框的Label控件
阅读:2255 添加日期:2021/3/27 23:19:51 原文链接:https://www.toutiao.com/item/6758544051579912707/

首先从Label类继承一个类取名FocusLabel

public class FocusLabel :Label

{

private bool m_ShowBorder = false;

protected override void OnPaint(PaintEventArgs e)

{

if (m_ShowBorder)

{

Rectangle rect = this.ClientRectangle;

rect = new Rectangle(rect.X, rect.Y, rect.Width - 1, rect.Height - 1);

e.Graphics.DrawRectangle(new Pen(Color.Orange), rect);

}

base.OnPaint(e);

}

//重写Click事件

protected override void OnClick(EventArgs e)

{

base.OnClick(e);

if (this.Parent != null)

{

foreach (Control ctrl in this.Parent.Controls)

{

if (ctrl is FocusLabel && ctrl != this)

{

((FocusLabel) ctrl).m_ShowBorder = false;

ctrl.Refresh();

}

}

}

this.m_ShowBorder = true;

this.Refresh();

}

}

ICP备案号:苏ICP备14035786号-1 苏公网安备 32050502001014号