首页 > 解决方案 > vb.net Click through windows form, just right or left mouseclick

问题描述

As described here I managed to have a Windows-Form one can click through.

My (in my opinion for this question most) relevant code parts read as follows:

First imports:

...
Imports System.Runtime.InteropServices
...

then definition and import of DLLs

...
Private InitialStyle As Integer
Dim PercentVisible As Decimal
...
<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
...

and finally using it

...
InitialStyle = GetWindowLong(Me.Handle, -20)
PercentVisible = 0.8
SetWindowLong(Me.Handle, -20, InitialStyle Or &H80000 Or &H20)
SetLayeredWindowAttributes(Me.Handle, 0, 255 * PercentVisible, &H2)
...

As mentioned, this code makes it possible to click throug a windows form, so the clicks reach the programm placed behind the windows form. In my opinion the feature is best described as color filter for parts of or the whole monitor.

Is there a possibility to modify the code in such a way, that only left clicks are transferred through the form? Right mouse clicks are needed to open a contextmenu or the like...

标签: vb.net

解决方案


如果要接收鼠标事件,则必须&H20像这样从窗口样式中删除

SetWindowLong(Me.Handle, -20, InitialStyle Or &H80000)

但这可以防止所有鼠标事件通过您的表单传播。现在您可以根据需要处理事件。并且您必须自己将鼠标左键单击相关事件传播到其他应用程序。


推荐阅读