Windows 窗体面板边框的背景色

发布于 2024-07-16 20:20:39 字数 92 浏览 6 评论 0原文

有什么方法可以更改面板或类似控件边框的 BackColor 吗?

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

Is there any way to change the BackColor of the border of a panel or similar control?

I am trying to "highlight" the user control when I hover the mouse over the user control.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

給妳壹絲溫柔 2024-07-23 20:20:39

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

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

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);
    }
∞梦里开花 2024-07-23 20:20:39

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

You can use the MouseEnter / MouseLeave events to do this.

    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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文