简体   繁体   English

Windows 窗体面板边界的背景色

[英]Backcolor of THE BORDER of a Windows Forms panel

Is there any way to change the BackColor of the border of a panel or similar control?有什么方法可以更改面板或类似控件边框的BackColor吗?

I am trying to "highlight" the user control when I hover the mouse over the user control.当我将鼠标悬停在用户控件上时,我试图“突出显示”用户控件。

Here's a simple class that highlights controls on the form with a border:这是一个简单的类,它用边框突出显示表单上的控件:

    public class Highlighter : Control
    {
        public void SetTarget(Control c)
        {
            Rectangle r = c.Bounds;
            r.Inflate(3, 3);
            Bounds = r;
        }

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

            e.Graphics.FillRectangle(Brushes.Red, e.ClipRectangle);
        }
    }

Then, in your form, set everything to use it:然后,在您的表单中,设置所有内容以使用它:

    private void Form1_Load(object sender, EventArgs e)
    {
        foreach (Control c in Controls)
        {
            c.MouseEnter += mouseEnter;
            c.MouseLeave += mouseLeave;
        }
    }

    private void mouseEnter(object sender, EventArgs e)
    {
        _highlighter.SetTarget(sender as Control);
        _highlighter.Visible = true;
    }

    private void mouseLeave(object sender, EventArgs e)
    {
        _highlighter.Visible = false;
    }

Then, in the constructor, just create the highlighter:然后,在构造函数中,只需创建荧光笔:

    public Form1()
    {
        InitializeComponent();
        _highlighter = new Highlighter();
        Controls.Add(_highlighter);
    }

You can use the MouseEnter / MouseLeave events to do this.您可以使用 MouseEnter / MouseLeave 事件来执行此操作。

    private void panel1_MouseEnter(object sender, EventArgs e)
    {
        panel1.BackColor = System.Drawing.Color.Red;
    }

    private void panel1_MouseLeave(object sender, EventArgs e)
    {
        panel1.BackColor = System.Drawing.Color.Empty;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM