问题描述
你好,
我有一个全局鼠标钩.当用户在窗口(可以是任何应用程序)内单击鼠标左键时,我要获取此窗口的句柄并进行移动.我具有以下用于移动窗口并在鼠标坐标处获得窗口的功能:
Hello,
I have a global mouse hook. When the user left-clicks inside a window (can be any application), I want to get this window''s handle and move it. I have the following functions that I use to move the window and get the window at the mouse coordinates:
<DllImport("user32.dll", SetLastError:=True)> _
Public Shared Function SetWindowPos(ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal uFlags As UInt32) As Boolean
End Function
<DllImport("user32.dll", EntryPoint:="WindowFromPoint", CharSet:=CharSet.Auto, ExactSpelling:=True)> _
Public Shared Function WindowFromPoint(ByVal pt As Point) As IntPtr
End Function
程序在这里检测到左键单击:
Here is where the program detects the left-clicks:
Public Sub MouseMoved(ByVal sender As Object, ByVal e As MouseEventArgs)
If (e.Clicks > 0 AndAlso e.Button = Windows.Forms.MouseButtons.Left) Then
SetWindowPos(WindowFromPoint(New Point(Cursor.Position.X, Cursor.Position.Y)), New IntPtr(-1), 200, 200, SomeWidth, SomeHeight, 0)
End If
End Sub
这应该将窗口移动到坐标200、200,并将其大小调整为SomeWidth/SomeHeight(无论大小如何).
当前,如果我单击标题栏或窗口的边缘(出现调整大小的光标),它可以按期望的方式工作,但是如果用户在窗口中单击,即无法工作.用户在页面加载控件中的IE窗口内单击.单击标题栏或窗口边缘以外的任何位置,将返回与窗口句柄不同的句柄,因此不会移动窗口.无论用户在此窗口内单击什么位置,如何获取窗口的句柄?
谢谢.
This should move the window to coordinates 200, 200 and resize it to SomeWidth/SomeHeight (whatever those are).
Currently, it works as desired if I click in the title bar or the window''s edge (resize cursor appears), but it does not work if the user clicks inside the window - ie. the user clicks inside an IE window in the control where the pages load. Clicking anywhere but on the title bar or window edge returns a different handle than the window handle, and so the window is not moved. How can I get the window''s handle no matter where the user clicks inside this window?
Thanks.
推荐答案
这篇关于当用户单击窗口句柄时获取窗口句柄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!