.NET 中的非透明点击表单

发布于 2024-10-17 01:41:07 字数 68 浏览 1 评论 0原文

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

In .NET, is it possible to create a non-transparent form which can be clicked through? I assume there should be some sort of API to transfer mouse clicks to windows right behind the form. Which one?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我乃一代侩神 2024-10-24 01:41:07

要进行表单点击,您需要从 Windows API 中 P/Invoke 一些函数并设置表单的 扩展窗口样式。我任意选择用 VB.NET 来表示示例代码。如果这不是您的偏好,可以轻松将其转换为 C#。

GetWindowLong 函数 开始,您可以使用该函数将用于检索扩展窗口样式。

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 方法 并添加以下行:

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

当然,请注意,用户现在不可能与表单上的元素进行交互,并且 > 他们很难关闭它。仔细考虑这是否真的是您想要做的。

To make a form click-through, you'll need to P/Invoke a few functions from the Windows API and set the form's extended window styles. I've arbitrarily chosen to represent the sample code in VB.NET. If that's not your preference, it's easily converted to C#.

Start with the GetWindowLong function, which you will use to retrieve the extended window styles.

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

You'll also need its sister function, SetWindowLong, to specify the additional extended window styles.

<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

And the constant for the extended window style that will need to be set:

Public Const WS_EX_TRANSPARENT As Integer = &H20

And now to use all of this, you can override your form's OnLoad method and add the following lines:

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

Of course, note that it's now going to be impossible for the user to interact with elements on your form and extremely difficult for them to close it. Consider carefully whether this is really what you want to do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文