我想做一个自己的按钮,里面有另外两个按钮,我想使它们透明,但是透明性不起作用

时间:2019-01-25 14:18:42

标签: c# winforms button transparent flowlayoutpanel

我有一个按钮,我在其中放了另外两个按钮。我希望其他两个按钮仅在我用鼠标输入主按钮时出现。当我输入它时,我希望其他两个按钮是半透明的,并且仅当我输入这两个按钮之一时才是完全透明的。

这些按钮位于FlowLayoutPanel内,上面带有背景图像。 它们是这样的:

enter image description here

按钮内部有图片和文字。

这是我的代码:

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

        Text = component.ProductsName;
        TextAlign = ContentAlignment.TopCenter;
        ImageAlign = ContentAlignment.TopLeft;
        Size = new Size(178, 75);

        foreach (Button item in CustomButtons())
        {
            Controls.Add(item);
        }
    }

    static Button[] CustomButtons()
    {
        Button delete = new Button();
        delete.Location = new Point(157, 1);
        delete.Size = new Size(20, 20);
        delete.MouseEnter += OnMouseEnter;
        delete.MouseLeave += DeleteOnMouseLeave;

        Button customize = new Button();
        customize.Location = new Point(delete.Left - 20, 1);
        customize.Size = new Size(20, 20);

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

    private static void DeleteOnMouseLeave(object sender, EventArgs e)
    {
        Button btn = (Button) sender;
        btn.UseVisualStyleBackColor = true;
        btn.BackColor = Color.Transparent;
    }

    private static void OnMouseEnter(object sender, EventArgs e)
    {
        Button btn = (Button) sender;
        btn.UseVisualStyleBackColor = false;
        btn.FlatAppearance.MouseOverBackColor = Color.FromArgb(100, 
                                                       Color.Black);
    }
 }

我想我尝试了所有想到的事情,尝试了事件,所有事情和按钮都没有按我希望的那样工作。 任何帮助,将不胜感激!谢谢! :D

1 个答案:

答案 0 :(得分:1)

看来我解决了!我只需要设置Flatstyle = FlatStyle.Flat和backColor = Color.Transparent! :D

结果是:exsample of output