C# 当控件聚焦时绘制边框
我想在任何有焦点的控件上画一个边框,当控件不再有焦点时,边框必须消失。我试图在下面的代码中画一个边框,但我不知道如何在边框消失之前让它被画出来C# 当控件聚焦时绘制边框,c#,winforms,C#,Winforms,我想在任何有焦点的控件上画一个边框,当控件不再有焦点时,边框必须消失。我试图在下面的代码中画一个边框,但我不知道如何在边框消失之前让它被画出来 void mButton_Paint(object sender, PaintEventArgs e) { ControlPaint.DrawBorder(e.Graphics, ((Control)sender).ClientRectangle, Color.DarkBlue, ButtonBorderStyle.Solid); } 试试这个
void mButton_Paint(object sender, PaintEventArgs e) {
ControlPaint.DrawBorder(e.Graphics, ((Control)sender).ClientRectangle, Color.DarkBlue, ButtonBorderStyle.Solid);
}
试试这个。(结果比我想象的要混乱得多。)
公共部分类表单SO29381768:表单
{
//建造师
公共表格SO29381768()
{
初始化组件();
InstallEventHandlers(此);
}
///
///递归方法为窗体上的所有容器控件安装绘制事件处理程序,
///包括表单本身,并为所有控件安装“enter”事件处理程序
///在表格上。
///
私有void InstallEventHandler(控制容器控制)
{
containerControl.Paint-=Control\u Paint;//防御性编程
容器控制油漆+=控制油漆;
foreach(containerControl.Controls中的控件嵌套控件)
{
nestedControl.Enter-=Control\u ReceivedFocus;//防御性编程
nestedControl.Enter+=Control\u ReceivedFocus;
如果(nestedControl是ScrollableControl)
InstallEventHandlers(nestedControl);
}
}
///
///控件接收焦点时调用的事件处理程序方法。这只是指示
///整个表单需要重新绘制。(这有点低效,但可能会
///只有当窗体上有很多控件时,才应注意。)
///
私有void控件\u ReceivedFocus(对象发送方、事件参数e)
{
这个。刷新();
}
///
///事件处理程序方法,如果控件有焦点,则在其周围绘制一个深蓝色矩形,以及
///如果在调用此方法的容器控件中。
///
私有void控件_Paint(对象发送器,PaintEventArgs e)
{
Control activeControl=this.activeControl;
if(activeControl!=null&&activeControl.Parent==sender)
{
e、 图形.DrawRectangle(Pens.DarkBlue,
新矩形(activeControl.Location.X-2,activeControl.Location.Y-2,
activeControl.Size.Width+4,activeControl.Size.Height+4);
}
}
}
您希望边框位于控件内部还是外部?集装箱内有控制装置吗?有些控件甚至无法获得焦点。你的问题太模糊了。更具体地说,如果((控制)发送器==ActiveControl)绘制是由系统触发的,则在绘制代码前面加上前缀,至少对于在焦点改变时改变外观的按钮和其他控件。对于其他人,您需要观看“离开/进入”事件。我已经编辑了我的答案“微小优化”。谢谢!!我要试试看!
public partial class FormSO29381768 : Form
{
// Constructor
public FormSO29381768()
{
InitializeComponent();
InstallEventHandlers(this);
}
/// <summary>
/// Recursive method to install the paint event handler for all container controls on a form,
/// including the form itself, and also to install the "enter" event handler for all controls
/// on a form.
/// </summary>
private void InstallEventHandlers(Control containerControl)
{
containerControl.Paint -= Control_Paint; // Defensive programming
containerControl.Paint += Control_Paint;
foreach (Control nestedControl in containerControl.Controls)
{
nestedControl.Enter -= Control_ReceivedFocus; // Defensive programming
nestedControl.Enter += Control_ReceivedFocus;
if (nestedControl is ScrollableControl)
InstallEventHandlers(nestedControl);
}
}
/// <summary>
/// Event handler method that gets called when a control receives focus. This just indicates
/// that the whole form needs to be redrawn. (This is a bit inefficient, but will presumably
/// only be noticeable if there are many, many controls on the form.)
/// </summary>
private void Control_ReceivedFocus(object sender, EventArgs e)
{
this.Refresh();
}
/// <summary>
/// Event handler method to draw a dark blue rectangle around a control if it has focus, and
/// if it is in the container control that is invoking this method.
/// </summary>
private void Control_Paint(object sender, PaintEventArgs e)
{
Control activeControl = this.ActiveControl;
if (activeControl != null && activeControl.Parent == sender)
{
e.Graphics.DrawRectangle(Pens.DarkBlue,
new Rectangle(activeControl.Location.X - 2, activeControl.Location.Y - 2,
activeControl.Size.Width + 4, activeControl.Size.Height + 4));
}
}
}