怎样才能悬停效果是控件分组产生的? [英] How can a hover effect be created for a grouping of controls?

查看:172
本文介绍了怎样才能悬停效果是控件分组产生的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这看起来很简单,但我似乎无法找到答案。

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

请参阅下图:

这是一个面板,上面有5标签。

It is a panel with 5 labels on it.

我想要的行为是,如果鼠标进入箱(任意位置),背景颜色变化(例如:艾莉斯蓝的,而不是白色)。问题是在Windows窗体中,透明度是怪异之间的其他问题。如果我设置面板对小鼠的背景进入,标签都还拥有白色的背景,所以我周围有标签的白色块。等等。

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.

推荐答案

背景色是一种环境属性。因为你明确地设置标签的背景色它不工作的权利。用鼠标右键单击该标签的背景色属性,然后单击重置,因此不再以粗体显示。改变面板的背景色,现在会自动改变标签的背景色为好。

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.

然而,这仍然没有解决您的问题。当您在一个标签上移动鼠标面板的MouseLeave事件将触发。有这个在的WinForms没有干净的解决方案,订阅所有的标签和面板的MouseEnter /离开事件,不排除极端情况。像当用户很快地移动鼠标从一个标签,该标签是靠近面板的边缘。你会得到的鼠标离开的标签,但不是的MouseEnter +保留为面板。

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.

这样做的唯一的好修复程序是一个计时器或Application.Idle事件。像这样的:

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

这篇关于怎样才能悬停效果是控件分组产生的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆