如何为面板设置透明不透明度
how to set transparent opacity to panel
如何将不透明度之类的面板设置为 0。我通过程序设置面板,它位于视频播放器的顶部。代码是这样的
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
结果是这样的
然后我尝试播放只显示一半的视频
我使用面板的原因是暂停视频(通过透明设置面板在视频之上),当我点击面板时因为视频不支持点击事件
更新
我把代码放在usercontrol1
尽管我已经在设计器中插入了代码,但仍然出现错误。太清楚了,我把代码设计器放在主设计器代码下面。我试图在主设计器代码中只放置 inherit panel
代码,但它只需要一个继承。
最好的方法是创建一个继承面板 class 并使用以下代码覆盖 CreateParams
和 OnPaintBackground
的自定义控件:
(支持 Zohar Peled 的 post )
将后面的代码替换为:
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
并将设计器代码替换为:
<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
您要替换的代码最初可能看起来不同,但使用此代码将确保一切正常。
注意:如果将背景色设置为 Transparent
或 Control
(这取决于控件,通常实际上与透明相同),此代码将使面板透明。)
我试图找到用于创建和实现自定义控件的更新资源,但找不到维护的资源。所以这里有一些关于如何创建自定义控件的分步说明。
要创建可在设计器中使用的自定义控件:
(我在下面的示例中使用 Visual Studio 2015,在其他版本中可能会有所不同。)
1. 创建新的 Windows 表单控件库
2. 然后右键单击并将控件重命名为 "TransparentPanel"(或
随便起什么名字)
3.将上面的代码分别粘贴到后台代码和设计器代码中(没有使用"TransparentPanel"的就改成class名字)
4. 构建项目(这将创建您需要在主项目中引用的 .dll)
5. 这是可选的,但最好将 DLL 存储在一致的位置,而不是项目 bin 文件夹,因此,可以选择导航到控件库 bin文件夹并将创建的 DLL 复制到您要存储自定义 DLL 的另一个位置。
6. 转到要在其中使用控件的项目,然后在工具箱中右键单击并单击 "Choose Items..."
7. 确保您位于“.NET Framework 组件”点击和 select "Browse".
8. 导航到控件库的 bin 文件夹(或您存储 DLL 的位置),select 控件并单击 "Open" .
9. 您现在将在 "Choose Toolbox Items" 表单中看到 selected 的 TransparentControl。单击 "OK"
10. 那么应该可以在"General" section下找到控件.
11. 将控件拖放到窗体上。
注意:
该控件在设计器中可能看起来不透明,但在运行时它应该可以满足您的要求。
希望这对你有用!
如何将不透明度之类的面板设置为 0。我通过程序设置面板,它位于视频播放器的顶部。代码是这样的
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
结果是这样的
然后我尝试播放只显示一半的视频
我使用面板的原因是暂停视频(通过透明设置面板在视频之上),当我点击面板时因为视频不支持点击事件
更新
我把代码放在usercontrol1
尽管我已经在设计器中插入了代码,但仍然出现错误。太清楚了,我把代码设计器放在主设计器代码下面。我试图在主设计器代码中只放置 inherit panel
代码,但它只需要一个继承。
最好的方法是创建一个继承面板 class 并使用以下代码覆盖 CreateParams
和 OnPaintBackground
的自定义控件:
(支持 Zohar Peled 的 post
将后面的代码替换为:
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
并将设计器代码替换为:
<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
您要替换的代码最初可能看起来不同,但使用此代码将确保一切正常。
注意:如果将背景色设置为 Transparent
或 Control
(这取决于控件,通常实际上与透明相同),此代码将使面板透明。)
我试图找到用于创建和实现自定义控件的更新资源,但找不到维护的资源。所以这里有一些关于如何创建自定义控件的分步说明。
要创建可在设计器中使用的自定义控件:
(我在下面的示例中使用 Visual Studio 2015,在其他版本中可能会有所不同。)
1. 创建新的 Windows 表单控件库
2. 然后右键单击并将控件重命名为 "TransparentPanel"(或 随便起什么名字)
3.将上面的代码分别粘贴到后台代码和设计器代码中(没有使用"TransparentPanel"的就改成class名字)
4. 构建项目(这将创建您需要在主项目中引用的 .dll)
5. 这是可选的,但最好将 DLL 存储在一致的位置,而不是项目 bin 文件夹,因此,可以选择导航到控件库 bin文件夹并将创建的 DLL 复制到您要存储自定义 DLL 的另一个位置。
6. 转到要在其中使用控件的项目,然后在工具箱中右键单击并单击 "Choose Items..."
7. 确保您位于“.NET Framework 组件”点击和 select "Browse".
8. 导航到控件库的 bin 文件夹(或您存储 DLL 的位置),select 控件并单击 "Open" .
9. 您现在将在 "Choose Toolbox Items" 表单中看到 selected 的 TransparentControl。单击 "OK"
10. 那么应该可以在"General" section下找到控件.
11. 将控件拖放到窗体上。
注意: 该控件在设计器中可能看起来不透明,但在运行时它应该可以满足您的要求。
希望这对你有用!