2

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

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

4

2 回答 2

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);
    }
于 2009-04-20T14:05:55.613 回答
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;
    }
于 2009-04-20T12:03:17.653 回答