Windows窗体面板的边框的背景色

时间:2009-04-20 11:55:18

标签: winforms panel border

有没有办法更改面板或类似控件边框的BackColor

当我将鼠标悬停在用户控件上时,我试图“突出显示”用户控件。

2 个答案:

答案 0 :(得分:3)

这是一个简单的类,它使用边框突出显示窗体上的控件:

    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);
        }
    }

然后,在您的表单中,设置所有内容以使用它:

    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;
    }

然后,在构造函数中,只需创建荧光笔:

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

答案 1 :(得分:0)

您可以使用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;
    }