为什么按钮会闪烁?

时间:2019-04-17 04:57:41

标签: c# winforms button controls

当我进入主按钮时,我有2个按钮(从现在开始是自定义按钮)被添加到按钮的控件(从现在开始是主按钮)中,我希望自定义按钮具有ImageBackground出现,其工作方式与众不同。现在,当我用鼠标进入自定义按钮时,我希望再次出现2 ImageBackground,并且当这种情况发生时,我希望主按钮保持与我第一次用鼠标输入它时的颜色相同。我想要它,但是按钮闪烁,有时当我输入另一个主按钮的自定义按钮时,前一个按钮仍处于mouseEnter状态。这是为什么?我需要使用async / await或类似的东西吗?

我认为可能是因为它必须在发生时进行编译,并且需要一些时间,这就是为什么它会闪烁,这就是我认为我需要使用async / await的原因,但这对我来说真的是新的,所以我不知道该怎么用。

public class MyButton : Button
    {
        public MyButton()
        {
            SetStyle(ControlStyles.StandardClick | ControlStyles.StandardDoubleClick | ControlStyles.UserMouse, true);

            Margin = new Padding(0);
            TextAlign = ContentAlignment.TopCenter;
            ImageAlign = ContentAlignment.TopLeft;
            TextImageRelation = TextImageRelation.ImageBeforeText;
            Font = new Font("Century Gothic", 11f, FontStyle.Bold);
            Size = new Size(200, 75);
            FlatStyle = FlatStyle.Flat;
            BackColor = Color.FromArgb(0, 255, 255, 255);
            FlatAppearance.MouseOverBackColor = ColorTranslator.FromHtml("#64A4B3B6");
            FlatAppearance.BorderSize = 2;
            FlatAppearance.BorderColor = Color.FromArgb(0, 255, 255, 255);

            Button[] custom = CustomButtons();
            for (int i = 0; i < 2; i++)
            {
                Controls.Add(custom[i]);
                Controls[i].MouseHover += CustomOnMouseEnter;
            }

            MouseEnter += OnMouseEnter;
            MouseLeave += OnMouseLeave;
        }

        private Button[] CustomButtons()
        {

            Button delete = new Button();
            delete.Name = "delete";
            delete.Location = new Point(this.Size.Width - 22, 2);
            delete.Size = new Size(20, 20);
            delete.FlatStyle = FlatStyle.Flat;
            delete.BackColor = Color.Transparent;
            delete.FlatAppearance.MouseOverBackColor = ColorTranslator.FromHtml("#64A4B3B6");
            delete.FlatAppearance.BorderSize = 0;

            Button customize = new Button();
            customize.Name = "customize";
            customize.Location = new Point(delete.Left - 20, delete.Top);
            customize.Size = new Size(20, 20);
            customize.FlatStyle = FlatStyle.Flat;
            customize.BackColor = Color.Transparent;
            customize.FlatAppearance.MouseOverBackColor = ColorTranslator.FromHtml("#64A4B3B6");
            customize.FlatAppearance.BorderSize = 0;

            Button[] buttons = { delete, customize };
            return buttons;
        }

        private void OnMouseLeave(object sender, EventArgs e)
        {
            if (Controls.Count != 0)
            {
                Controls[0].BackgroundImage = null;
                Controls[1].BackgroundImage = null;
            }

            if (BackColor != ColorTranslator.FromHtml("#64389eed"))
            {
                BackColor = Color.FromArgb(0, 255, 255, 255);
            }
        }

        private void OnMouseEnter(object sender, EventArgs e)
        {
            if (Controls.Count != 0)
            {
                Controls[0].BackgroundImage = Resources.cross;
                Controls[1].BackgroundImage = Resources.settings;
            }
        }

        private void CustomOnMouseEnter(object sender, EventArgs e)
        {
            this.BackColor = ColorTranslator.FromHtml("#64A4B3B6");
            Controls[0].BackgroundImage = Resources.cross;
            Controls[1].BackgroundImage = Resources.settings;
        }
    }

这是此代码output的输出 当您输入自定义按钮时,以及在我离开鼠标时,前一个按钮处于MouseEnter状态的方式,您都可以看到闪烁!

我们非常感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

主要问题是“ OnMouseLeave”不仅在鼠标离开整个控件时被调用,而且在它进入两个小按钮中的任何一个时也会被调用,因为它们重叠了它们的父对象。您还应该使用“ MouseEnter”事件代替“ MouseHover”事件。

在下面,您会找到应该解决问题的简化版本。 “内部”字段保存有关整个控件的“输入”数减去“叶子”数。如果该值大于零,则鼠标位于控件内,包括两个小按钮。

public class MyButton : Button
{
    Image[] images;
    Button[] custom;
    Color hilited = ColorTranslator.FromHtml("#64A4B3B6");
    int inside;

    public MyButton()
    {
        SetStyle(ControlStyles.StandardClick | ControlStyles.StandardDoubleClick | ControlStyles.UserMouse, true);

        Margin = new Padding(0);
        TextAlign = ContentAlignment.TopCenter;
        ImageAlign = ContentAlignment.TopLeft;
        TextImageRelation = TextImageRelation.ImageBeforeText;
        Font = new Font("Century Gothic", 11f, FontStyle.Bold);
        Size = new Size(200, 75);
        FlatStyle = FlatStyle.Flat;
        BackColor = Color.Transparent;
        FlatAppearance.MouseOverBackColor = hilited;
        FlatAppearance.BorderSize = 2;
        FlatAppearance.BorderColor = Color.FromArgb(0, 255, 255, 255);

        images = new Image[] { Resources.cross, Resources.settings };
        custom = CustomButtons();
        for (int i = 0; i < 2; i++)
        {
            Controls.Add(custom[i]);
            Controls[i].MouseEnter += CommonEnter;
            Controls[i].MouseLeave += CommonLeave;
        }

        MouseEnter += CommonEnter;
        MouseLeave += CommonLeave;
    }

    private Button[] CustomButtons()
    {
        Button delete = new Button();
        delete.Name = "delete";
        delete.Location = new Point(this.Size.Width - 22, 2);
        delete.Size = new Size(20, 20);
        delete.FlatStyle = FlatStyle.Flat;
        delete.BackColor = Color.Transparent;
        delete.FlatAppearance.MouseOverBackColor = hilited;
        delete.FlatAppearance.BorderSize = 0;

        Button customize = new Button();
        customize.Name = "customize";
        customize.Location = new Point(delete.Left - 20, delete.Top);
        customize.Size = new Size(20, 20);
        customize.FlatStyle = FlatStyle.Flat;
        customize.BackColor = Color.Transparent;
        customize.FlatAppearance.MouseOverBackColor = hilited;
        customize.FlatAppearance.BorderSize = 0;

        return new Button[] { delete, customize };
    }

    void CommonEnter(object sender, EventArgs e)
    {
        if (inside++ == 0)
        {
            BackColor = hilited;
            custom[0].BackgroundImage = images[0];
            custom[1].BackgroundImage = images[1];
        }
    }

    void CommonLeave(object sender, EventArgs e)
    {
        if (--inside == 0)
        {
            BackColor = Color.Transparent;
            custom[0].BackgroundImage = null;
            custom[1].BackgroundImage = null;
        }
    }
}