C# Windows Form Application Transparent button

不打扰是莪最后的温柔 提交于 2020-01-10 03:54:27

问题


I'm new to C#. I'd like to create an invisible button, but they are click-able in C# windows form application. Is there a way? I tried BackColor to Transparent, but that does not change the fact that it is transparent


回答1:


Its simple try this.

Click the button that you want to make transparent. Select FlatStyle from Properties and set it to popup Now change the BackColor property to Transparent.

This will make the button transparent.

However if you want to make it transparent over a PictureBox this method wont work..

It works only on normal backgrounds and background images. Hope it works....




回答2:


btnLink.FlatStyle = FlatStyle.Flat; 

btnLink.BackColor = Color.Transparent;

btnLink.FlatAppearance.MouseDownBackColor = Color.Transparent;

btnLink.FlatAppearance.MouseOverBackColor = Color.Transparent;



回答3:


Did you try button.Visible = false? If all you want is to hide it, this will do the job.




回答4:


Reference:

Original article and code can be found at:

Displaying a ToolTip when the Mouse Hovers Over a Disabled Control

@ CodeProject by tetsushmz

Code:

public class TransparentSheet : ContainerControl
{
    public TransparentSheet()
    {
        // Disable painting the background.
        this.SetStyle(ControlStyles.Opaque, true);
        this.UpdateStyles();

        // Make sure to set the AutoScaleMode property to None
        // so that the location and size property don't automatically change
        // when placed in a form that has different font than this.
        this.AutoScaleMode = AutoScaleMode.None;

        // Tab stop on a transparent sheet makes no sense.
        this.TabStop = false;
    }

    private const short WS_EX_TRANSPARENT = 0x20;

    protected override CreateParams CreateParams
    {
        [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
        get
        {
            CreateParams l_cp;
            l_cp = base.CreateParams;
            l_cp.ExStyle = (l_cp.ExStyle | WS_EX_TRANSPARENT);
            return l_cp;
        }
    }
}

Explanation:

What you need to do is use the given control as an overlay on your disabled TextBox (that you mentioned in one of your comments). Sibscribe to the overlay control's Click event and you have yourself a click on a disabled control.

I strongly recommend against this approach and feel it is kind of a hack. You really should look for an alternative approach instead of having to use a disabled control with an overlay control on top of it.

Maybe a different UI or atleast wrap it up in a UserControl to isolate this messy logic.




回答5:


Setting the background property of the button to transparent will still leave a border. If you want a completely transparent button, do one of 2 things:

Create a transparent panel and assign a method to the Click event

or preferably

Create a custom UserControl that is filled with only BackColor (set to transparent) and assign method to Click event.

    public class Invisible_Button : UserControl
{
    protected override void OnMouseMove(MouseEventArgs e)
    {
        base.OnMouseMove(e);
        this.Cursor = Cursors.Hand;
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        e.Graphics.FillRectangle(new SolidBrush(this.BackColor), 0, 0, this.Width, this.Height);
    }
}


来源:https://stackoverflow.com/questions/10763640/c-sharp-windows-form-application-transparent-button

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!