如何为一组控件创建悬停效果?

发布于 2024-11-19 21:04:30 字数 306 浏览 6 评论 0原文

这看起来很简单,但我似乎无法弄清楚。

请参见下图:

在此处输入图像描述

这是一个带有 5 个标签的面板。

我想要的行为是,如果鼠标进入框(任何地方),背景颜色就会改变(例如:AliceBlue 而不是白色)。问题出在 Windows 窗体中,透明度在其他问题中很奇怪。如果我在鼠标输入时设置面板的背景,则标签仍然具有白色背景,因此标签周围有白色块。等等

我相信其他人也遇到过这个问题。我确信这很简单。我就是无法理解。

This seems so simple but I just can't seem to figure it out.

See the image below:

enter image description here

It is a panel with 5 labels on it.

The behavior I want is that if the mouse enters the box (anywhere), the background color changes (for ex: AliceBlue instead of White). The problem is in Windows Forms, the transparency is wierd among other problems. If I set the background of the panel on mouse enter, the labels all still have white backgrounds and so I have white blocks around the labels. Etc.

I am sure others have run into this problem. And I'm sure it's simple. I just can't get it.

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

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

发布评论

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

评论(1

相权↑美人 2024-11-26 21:04:31

BackColor 是一个“环境”属性。它无法正常工作,因为您显式设置了标签的背景颜色。右键单击标签的 BackColor 属性,然后单击“重置”,使其不再以粗体显示。更改面板的背景色现在也会自动更改标签的背景色。

然而这仍然不能解决你的问题。当您将鼠标移过标签之一时,将触发面板的 MouseLeave 事件。在 Winforms 中没有干净的解决方案,订阅所有标签和面板的 MouseEnter/Leave 事件并不能消除极端情况。就像用户非常快速地将鼠标从靠近面板边缘的标签上移动一样。您将获得标签的 MouseLeave,但不会获得面板的 MouseEnter + Leave。

唯一好的解决办法是使用计时器或 Application.Idle 事件。像这样:

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
        Application.Idle += Application_Idle;
    }
    protected override void OnFormClosed(FormClosedEventArgs e) {
        Application.Idle -= Application_Idle;
        base.OnFormClosed(e);
    }
    void Application_Idle(object sender, EventArgs e) {
        var pos = panel1.PointToClient(Cursor.Position);
        if (panel1.DisplayRectangle.Contains(pos)) panel1.BackColor = Color.Red;
        else panel1.BackColor = this.BackColor;
    }
}

BackColor is an 'ambient' property. It doesn't work right because you set the labels' BackColor explicitly. Right-click the BackColor property of the labels and click Reset so it is no longer shown in bold. Changing the panel's BackColor will now automatically change the labels' BackColor as well.

This however still doesn't solve your problem. The panel's MouseLeave event will fire when you move the mouse across one of the labels. There is no clean solution for this in Winforms, subscribing all the labels and the panel's MouseEnter/Leave events doesn't eliminate corner cases. Like when the user very quickly moves the mouse from a label that's close to the edge of the panel. You'll get the MouseLeave for the label but not the MouseEnter + Leave for the panel.

The only good fix for this is a timer or the Application.Idle event. Like this:

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
        Application.Idle += Application_Idle;
    }
    protected override void OnFormClosed(FormClosedEventArgs e) {
        Application.Idle -= Application_Idle;
        base.OnFormClosed(e);
    }
    void Application_Idle(object sender, EventArgs e) {
        var pos = panel1.PointToClient(Cursor.Position);
        if (panel1.DisplayRectangle.Contains(pos)) panel1.BackColor = Color.Red;
        else panel1.BackColor = this.BackColor;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文