如何为面板设置透明不透明度 [英] how to set transparent opacity to panel

查看:48
本文介绍了如何为面板设置透明不透明度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何将面板透明(如不透明度)设置为 0.我通过程序设置了面板,它位于视频播放器的顶部.代码是这样的

Private Sub Button1_Click(sender As Object, e As EventArgs) 处理 Button1.Click ', AxVLCPlugin21.Click将面板调暗为新面板panelx.Visible = Truepanelx.Size = 新尺寸(AxVLCPlugin21.Width, CInt(AxVLCPlugin21.Height/2))panelx.BackColor = System.Drawing.Color.TransparentAxVLCPlugin21.Controls.Add(panelx)panelx.BringToFront()'AddHandler panelx.DoubleClick, AddressOf panelx_click结束子

结果是这样的

然后我尝试播放它只显示一半的视频

我使用面板的原因是暂停视频(通过透明将面板设置在视频顶部),当我点击面板时,因为视频不支持点击事件

更新

我把代码放在 usercontrol1 中

虽然我已经在设计器中插入了代码,但仍然给我一个错误.太澄清了,我将代码设计器放在主设计器代码之后.我试图在主设计器代码中只放置 inherit panel 代码,但它只需要一个继承.

解决方案

最好的方法是创建一个自定义控件,该控件继承面板类并覆盖 CreateParamsOnPaintBackground 用这段代码:

(支持 Zohar Peled 的帖子

2. 然后右键单击并将您的控件重命名为TransparentPanel"(或随便你取什么名字)

3.将上面的代码分别粘贴到后面的代码和设计器的代码中(如果没有使用TransparentPanel"就改类名)

4. 构建项目(这将创建您需要在主项目中引用的 .dll)

5. 这是可选的,但最好将 DLL 存储在一致的位置,而不是项目 bin 文件夹,因此,可以选择导航到控件库 bin 文件夹并复制创建的DLL 到您想要存储自定义 DLL 的另一个位置.

6. 转到要在其中使用控件的项目,然后在工具箱中右键单击并单击选择项目..."

7. 确保您在.NET Framework 组件"上点击并选择浏览".

8. 导航到控件库的 bin 文件夹(或您存储 DLL 的位置),选择控件并单击打开".

9. 您将在选择工具箱项目"表单中看到现在选中的透明控件.点击确定"

10.然后您应该可以在常规"部分找到控件.

11. 将控件拖放到表单上.

注意:该控件在设计器中可能看起来不透明,但在运行时它应该可以满足您的要求.

希望这对你有用!

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
    AxVLCPlugin21.Controls.Add(panelx)        
    panelx.BringToFront()
    'AddHandler panelx.DoubleClick, AddressOf panelx_click
End Sub

the result is like this

then i try to play the video it only show the half

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

update

i put the code in usercontrol1

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.

解决方案

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
        Get
            ' 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
            MyBase.OnPaintBackground(e)
        End If
    End Sub
End Class

And replace the designer code with:

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()>
Partial Class TransparentPanel
    Inherits System.Windows.Forms.Panel

    'Control overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()>
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Control Designer
    Private components As System.ComponentModel.IContainer

    ' NOTE: The following procedure is required by the Component Designer
    ' It can be modified using the Component Designer.  Do not modify it
    ' using the code editor.
    <System.Diagnostics.DebuggerStepThrough()>
    Private Sub InitializeComponent()
        components = New System.ComponentModel.Container()
    End Sub

End Class

The code you're replacing may look different initially, but using this code will ensure everything works.

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

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..."

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

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

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.

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!

这篇关于如何为面板设置透明不透明度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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