gpt4 book ai didi

winforms - Windows 窗体面板边界的背景色

转载 作者:行者123 更新时间:2023-12-04 07:08:16 26 4
gpt4 key购买 nike

有什么办法可以改变BackColor面板或类似控件的边框?

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

最佳答案

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

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

关于winforms - Windows 窗体面板边界的背景色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/767944/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com