【发布时间】:2018-09-06 23:13:58
【问题描述】:
我有一个无法解决的特殊问题。我正在尝试制作一个完全透明的叠加层,但是,我必须能够单击它进入底层表单。这些覆盖形式不属于任何东西。每个覆盖表单包含一个面板。但是,如果不完全使整个表单不可见,我似乎无法获得所需的透明度。我能做什么?
这是我的表单的代码。
Imports System.Runtime.InteropServices
Public Class frmOverlay
Public ChartProperty As strChartProperty
Private InitialStyle As Integer
Dim PercentVisible As Decimal
Public Sub New(ByRef chartProperties As strChartProperty)
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
ChartProperty = chartProperties
SetStyle(ControlStyles.SupportsTransparentBackColor, True)
BackColor = Color.Transparent
ForeColor = Color.Transparent
Opacity = 0
End Sub
Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
InitialStyle = GetWindowLong(Me.Handle, -20)
PercentVisible = 0.5
SetWindowLong(Me.Handle, -20, InitialStyle Or &H80000 Or &H20)
SetLayeredWindowAttributes(Me.Handle, 0, 255 * PercentVisible, &H2)
Me.TopMost = True
Dim panel As New OverlayPanel
Controls.Add(panel)
End Sub
<DllImport("user32.dll", EntryPoint:="GetWindowLong")> Public Shared Function GetWindowLong(ByVal hWnd As IntPtr, ByVal nIndex As Integer) As Integer
End Function
<DllImport("user32.dll", EntryPoint:="SetWindowLong")> Public Shared Function SetWindowLong(ByVal hWnd As IntPtr, ByVal nIndex As Integer, ByVal dwNewLong As Integer) As Integer
End Function
<DllImport("user32.dll", EntryPoint:="SetLayeredWindowAttributes")> Public Shared Function SetLayeredWindowAttributes(ByVal hWnd As IntPtr, ByVal crKey As Integer, ByVal alpha As Byte, ByVal dwFlags As Integer) As Boolean
End Function
Private Sub frmOverlay_ResizeEnd(sender As Object, e As EventArgs) Handles MyBase.ResizeEnd
ResumeLayout()
End Sub
Private Sub frmOverlay_ResizeBegin(sender As Object, e As EventArgs) Handles MyBase.ResizeBegin
SuspendLayout()
End Sub
Public Class OverlayPanel
Inherits Panel
Public Event Event_RedrawRequest(ByRef e As PaintEventArgs)
Public Sub New()
SetStyle(ControlStyles.DoubleBuffer Or ControlStyles.UserPaint Or ControlStyles.AllPaintingInWmPaint, True)
UpdateStyles()
Dock = DockStyle.Fill
End Sub
Protected Overrides Sub OnPaint(e As PaintEventArgs)
Dim ChartProperty As strChartProperty = DirectCast(Me.Parent, frmOverlay).ChartProperty
With e.Graphics
.Clear(Me.Parent.BackColor)
.SmoothingMode = IIf(ChartProperty.MaxDrawSpeed, Drawing2D.SmoothingMode.HighSpeed, Drawing2D.SmoothingMode.AntiAlias)
.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias '.ClearTypeGridFit
.CompositingQuality = IIf(ChartProperty.MaxDrawSpeed, Drawing2D.CompositingQuality.HighSpeed, Drawing2D.CompositingQuality.HighQuality)
End With
MyBase.OnPaint(e)
RaiseEvent Event_RedrawRequest(e)
Debug.Print("Overlay had to paint")
End Sub
End Class
End Class
【问题讨论】:
-
Windows 窗体透明度在此“作弊”。它会在桌面上拍摄您身后的照片,并将其用作背景。因此,如果您的应用程序后面有东西移动,您可能会得到奇怪的结果。如果您需要真正的透明度,则需要 WPF。
-
天哪 :( 好吧,我希望不会是这样。
-
平心而论,我已经使用 Windows 窗体的透明度相当多,人们几乎没有注意到它的弱点。
-
问题是我的桌面背景恰好是黑色的,因为它是覆盖在白色表格上的,所以黑色非常明显。
-
我真的不明白你想做什么。您是否想要一个完全透明的点击表单?小组的目的是什么?表格有边框吗?表单的大小和屏幕一样吗?
标签: vb.net winforms transparency