.NET中的非透明点击表单

时间:2011-02-11 00:04:09

标签: .net api window transparency

在.NET中,是否可以创建一个可以单击的非透明表单?我假设应该有某种API将鼠标点击转移到窗体后面的窗口。哪一个?

1 个答案:

答案 0 :(得分:2)

要进行表单点击,您需要从Windows API P / Invoke一些函数并设置表单extended window styles。我随意选择在VB.NET中表示示例代码。如果这不是您的偏好,则很容易转换为C#。

GetWindowLong function开始,您将使用它来检索扩展窗口样式。

Public Const GWL_EXSTYLE As Integer = -20

<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Public Shared Function GetWindowLong(ByVal hWnd As IntPtr, _
                                     ByVal nIndex As Integer) As Integer
End Function

您还需要其姐妹函数SetWindowLong来指定其他扩展窗口样式。

<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Public Shared Function SetWindowLong(ByVal hWnd As IntPtr, _
                                     ByVal nIndex As Integer, _
                                     ByVal dsNewLong As Integer) As Integer
End Function

需要设置的扩展窗口样式的常量:

Public Const WS_EX_TRANSPARENT As Integer = &H20


现在要使用所有这些,您可以覆盖表单的OnLoad method并添加以下行:

Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
   ''# Call the base class implementation
   MyBase.OnLoad(e)

   ''# Grab the current extended style information for this form
   Dim initialStyles As Integer = GetWindowLong(Me.Handle, GWL_EXSTYLE)

   ''# Add the transparent extended window style
   Dim newStyles As Integer = initialStyles Or WS_EX_TRANSPARENT

   ''# Update the form's extended window styles
   SetWindowLong(Me.Handle, GWL_EXSTYLE, newStyles)
End Sub

当然,请注意,用户现在无法与表单上的元素进行交互,并且非常难以关闭它。仔细考虑这是否真的是你想要做的。

相关问题