Winforms 更改焦点上Windows窗体控件的边框颜色

Winforms 更改焦点上Windows窗体控件的边框颜色,winforms,Winforms,当Windows窗体中的一些常用控件(TextBox、ComboBox、MaskedTextBox等)处于焦点时,是否有方法更改它们的边框颜色?我希望在对话框中实现这一点,因此当控件处于焦点时,其边框将变为蓝色?我建议在活动控件周围绘制一个矩形,如下所示: 我需要一个方法来获取表单中的所有控件,甚至是嵌套面板或GroupBox中的控件 方法: // Get all controls that exist in the form. public static List<Control&g

当Windows窗体中的一些常用控件(TextBox、ComboBox、MaskedTextBox等)处于焦点时,是否有方法更改它们的边框颜色?我希望在对话框中实现这一点,因此当控件处于焦点时,其边框将变为蓝色?

我建议在活动控件周围绘制一个矩形,如下所示:

  • 我需要一个方法来获取表单中的所有控件,甚至是嵌套面板或GroupBox中的控件
方法:

// Get all controls that exist in the form.
public static List<Control> GetAllControls(IList controls)
{
    List<Control> controlsCollectorList = new List<Control>();
    foreach (Control control in controls)
    {
        controlsCollectorList.Add(control);
        List<Control> SubControls = GetAllControls(control.Controls);
        controlsCollectorList.AddRange(SubControls);
    }
    return controlsCollectorList;
}
//获取表单中存在的所有控件。
公共静态列表GetAllControls(IList控件)
{
列表控件CollectorList=新列表();
foreach(控件中的控件)
{
controlsCollectorList.Add(控件);
列表子控件=GetAllControls(control.Controls);
controlsCollectorList.AddRange(子控件);
}
返回控件集合列表;
}
  • 然后。。绘图功能
守则:

public Form1()
{
    InitializeComponent();

    // The parents that'll draw the borders for their children
    HashSet<Control> parents = new HashSet<Control>(); 

    // The controls' types that you want to apply the new border on them
    var controlsThatHaveBorder = new Type[] { typeof(TextBox), typeof(ComboBox) };

    foreach (Control item in GetAllControls(Controls))
    {
        // except the control if it's not in controlsThatHaveBorder
        if (!controlsThatHaveBorder.Contains(item.GetType())) continue;

        // Redraw the parent when it get or lose the focus
        item.GotFocus += (s, e) => ((Control)s).Parent.Invalidate();
        item.LostFocus += (s, e) => ((Control)s).Parent.Invalidate();

        parents.Add(item.Parent);
    }

    foreach (var parent in parents)
    {
        parent.Paint += (sender, e) =>
        {
            // Don't draw anything if this is not the parent of the active control
            if (ActiveControl.Parent != sender) return; 

            // Create the border's bounds
            var bounds = ActiveControl.Bounds;
            var activeCountrolBounds = new Rectangle(bounds.X - 1, bounds.Y - 1, bounds.Width + 1, bounds.Height + 1);

            // Draw the border...
            ((Control)sender).CreateGraphics().DrawRectangle(Pens.Blue, activeCountrolBounds);
        };
    }
}
public Form1()
{
初始化组件();
//为孩子划定边界的父母
HashSet parents=新HashSet();
//要在控件上应用新边框的控件类型
var controlsThatHaveBorder=新类型[]{typeof(TextBox),typeof(ComboBox)};
foreach(GetAllControls(控件)中的控件项)
{
//除了控件,如果它不在有边框的控件中
如果(!controlsthathatHaveBorder.Contains(item.GetType()))继续;
//当父对象获得或失去焦点时,重新绘制父对象
item.GotFocus+=(s,e)=>((控件)s).Parent.Invalidate();
item.LostFocus+=(s,e)=>((控件)s).Parent.Invalidate();
parents.Add(item.Parent);
}
foreach(父对象中的var父对象)
{
parent.Paint+=(发送方,e)=>
{
//如果这不是活动控件的父控件,则不要绘制任何内容
如果(ActiveControl.Parent!=发送方)返回;
//创建边界的边界
var-bounds=ActiveControl.bounds;
var activeCountrolBounds=新矩形(bounds.X-1,bounds.Y-1,bounds.Width+1,bounds.Height+1);
//画边界。。。
((控件)sender.CreateGraphics().DrawRectangle(Pens.Blue,activeCountrolBounds);
};
}
}
祝你好运