chopperfield chopperfield - 6 months ago 105 Question

how to set transparent opacity to panel

how do i set panel transparent like opacity to 0. i set the panel by program and it was on top of video player. the code is like this

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click ', AxVLCPlugin21.Click
Dim panelx As New Panel
panelx.Visible = True
panelx.Size = New Size(AxVLCPlugin21.Width, CInt(AxVLCPlugin21.Height / 2))
panelx.BackColor = System.Drawing.Color.Transparent
'AddHandler panelx.DoubleClick, AddressOf panelx_click
End Sub

the result is like this
enter image description here

then i try to play the video it only show the half
enter image description here

the reason i use panel is to pause the video (set panel on top of video by transparent), when i click the panel since the video doesn't support click event


i put the code in usercontrol1
enter image description here

still got me an error, although i have insert the code in designer. too clarify i put the code designer after below the main designer code. i have tried to put only
inherit panel
code in main designer code but it only take one inherit only.
enter image description here

Answer Source

The best way to do this is to create a custom control that inherits the panel class and overrides CreateParams and OnPaintBackground with this bit of code:

(Props to Zohar Peled for his post here)

Replace the code behind with:

Public Class TransparentPanel
    Inherits System.Windows.Forms.Panel

    Protected Overrides ReadOnly Property CreateParams() As CreateParams
            ' Make background transparent
            Dim cp As CreateParams = MyBase.CreateParams
            cp.ExStyle = cp.ExStyle Or &H20
            Return cp
        End Get
    End Property

    Protected Overrides Sub OnPaintBackground(e As PaintEventArgs)
        ' call MyBase.OnPaintBackground(e) only if the backColor is not Color.Transparent
        If Me.BackColor <> Color.Transparent Then
        End If
    End Sub
End Class

And replace the designer code with:

Partial Class TransparentPanel
    Inherits System.Windows.Forms.Panel
End Class

NOTE: This code will make the panel transparent if the backcolor is set to Transparent or Control (which depending on the control is normally actually the same as transparent.)

I tried to find an updated resource for creating and implementing a custom control, but I wasn't able to find a maintained resource. So here are some step by step instructions on how to create a custom control.

To create custom control usable in the designer:

(I'm using Visual Studio 2015 for the examples below, it may appear different in other versions.)

1. Create new Windows Forms Control Library

enter image description here

2. Then right click and rename your control to "TransparentPanel" (or whatever name you like)

3. Paste the code above into the code behind and the designer code respectively (changing the class name if you didn't use "TransparentPanel")

4. Build the project (this will create the .dll you will need to reference in your main project)

5. This one is optional, but it is good to store your DLLs somewhere consistent, other than the project bin folder, so, optionally, navigate to the control library bin folder and copy the created DLL to another location you want to store your custom DLLs.

6. Go to the project you want to use the control in, and right click in the toolbox and click "Choose Items..."

enter image description here

7. Make sure you are on the the ".NET Framework Component" tap and select "Browse".

enter image description here

8. Navigate to the bin folder of the control library (or where ever you stored the DLL), select the control and click "Open".

enter image description here

9. You will see the TransparentControl selected now in the "Choose Toolbox Items" form. Click "OK"

10. Then you should be able to find the control under "General" section.

enter image description here

11. Drag and drop the control onto your form.

NOTE: The control may not look transparent in the designer, but on runtime it should do what you are looking for.

I hope this works for you!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download