#include <WinAPI.au3>
#include <WindowsConstants.au3>
Global $hForeground, $_exit = False, $_move = False, $hw_top
$hGUI = GUICreate('Test', 400, 300, -1, -1, 0, $WS_EX_TOPMOST)
GUICtrlCreateButton('Send "a"', 40, 40, 150, 20)
GUICtrlSetCursor(-1, 0)
$hButton = GUICtrlGetHandle(-1)
GUICtrlCreateButton('Exit', 200, 40, 150, 20)
GUICtrlSetCursor(-1, 0)
$hButton1 = GUICtrlGetHandle(-1)
GUICtrlCreateLabel(' 按住此处拖动窗口', 100, 80, 150, 15)
GUICtrlSetBkColor(-1, 0x00ff00)
GUICtrlSetCursor(-1, 9)
$hLabel = GUICtrlGetHandle(-1)
$iThreadId = _WinAPI_GetCurrentThreadId()
$hInstance = _WinAPI_GetModuleHandle(0)
$hCBTProc = DllCallbackRegister('_CBTCallBack', 'int', 'uint;wparam;lparam')
$pCBTProc = DllCallbackGetPtr($hCBTProc)
$hCBTHook = _WinAPI_SetWindowsHookEx(5, $pCBTProc, $hInstance, $iThreadId)
$hMouseProc = DllCallbackRegister('_MouseProc', 'int', 'uint;wparam;lparam')
$pMouseProc = DllCallbackGetPtr($hMouseProc)
$hMouseHook = _WinAPI_SetWindowsHookEx(14, $pMouseProc, $hInstance, 0)
$hEventProc = DllCallbackRegister('_EventProc', 'none', 'ptr;dword;hWnd;long;long;dword;dword')
$pEventProc = DllCallbackGetPtr($hEventProc)
$hEventHook = _SetWinEventHook($pEventProc, 1)
GUISetState(@SW_SHOWNOACTIVATE)
While GUIGetMsg() + 3
If $_exit Then ExitLoop
If $_move = True Then
$hw_top = _WinAPI_GetForegroundWindow()
GuiMove($hGUI)
$_move = False
EndIf
WEnd
_exit()
Func _exit()
GUIDelete($hGUI)
_WinAPI_UnhookWindowsHookEx($hCBTHook)
_WinAPI_UnhookWindowsHookEx($hMouseHook)
_UnhookWinEvent($hEventHook)
DllCallbackFree($hCBTProc)
DllCallbackFree($hMouseProc)
DllCallbackFree($hEventProc)
GUIDelete($hGUI)
EndFunc ;==>_exit
Func _CBTCallBack($iCode, $iwParam, $ilParam)
Switch $iCode
Case 5
If HWnd($iwParam) = $hGUI Then Return 1
EndSwitch
Return _WinAPI_CallNextHookEx($hCBTHook, $iCode, $iwParam, $ilParam)
EndFunc ;==>_CBTCallBack
Func _EventProc($hEventHook, $iEvent, $hWnd, $iObject, $iChild, $iEventThread, $iEventTime)
If ($iEvent = 0x8005) Then $hForeground = $hWnd
EndFunc ;==>_EventProc
Func _MouseProc($iCode, $iwParam, $ilParam)
If $_move = False Then
If ($iCode < 0) Then Return _WinAPI_CallNextHookEx($hMouseHook, $iCode, $iwParam, $ilParam)
Switch $iwParam
Case $WM_LBUTTONDOWN, $WM_LBUTTONUP, $WM_RBUTTONDOWN, $WM_RBUTTONUP, $WM_MBUTTONDOWN, $WM_MBUTTONUP
Local $tBuffer = DllStructCreate('int X;int Y', $ilParam)
Local $hWindow = _WinAPI_WindowFromPoint($tBuffer)
$tBuffer = 0
Switch $hWindow
Case $hGUI
Return 1
Case $hButton
If ($iwParam = $WM_LBUTTONUP) Then Send('a')
Return 1
Case $hButton1
$_exit = True
Return 1
Case $hLabel
$_move = True
Return 1
EndSwitch
EndSwitch
EndIf
Return _WinAPI_CallNextHookEx($hMouseHook, $iCode, $iwParam, $ilParam)
EndFunc ;==>_MouseProc
Func _SetWinEventHook($pCallBack, $iFlags, $iEventMin = 1, $iEventMax = 0x7FFFFFFF, $iProcessId = 0, $iThreadId = 0)
Local $iResult
$iResult = DllCall('User32.dll', 'ptr', 'SetWinEventHook', 'uint', $iEventMin, _
'uint', $iEventMax, 'ptr', 0, 'ptr', $pCallBack, 'dword', $iProcessId, _
'dword', $iThreadId, 'uint', $iFlags)
Return $iResult[0]
EndFunc ;==>_SetWinEventHook
Func _UnhookWinEvent($hEventHook)
Local $iResult
$iResult = DllCall('User32.dll', 'int', 'UnhookWinEvent', 'ptr', $hEventHook)
Return $iResult[0]
EndFunc ;==>_UnhookWinEvent
Func GuiMove($hGUI)
MouseDown('left')
Local $PY[2], $a_R, $PM = MouseGetPos(), $PW = WinGetPos($hGUI, '')
$PY[0] = $PW[0] - $PM[0]
$PY[1] = $PW[1] - $PM[1]
While 1
$a_R = DllCall('user32.dll', "short", "GetAsyncKeyState", "int", '0x01')
If @error Then Return 0
If Not BitAND($a_R[0], 0x8000) Then ExitLoop
$PM = MouseGetPos()
WinMove($hGUI, '', $PM[0] + $PY[0], $PM[1] + $PY[1])
Sleep(5)
WEnd
WinActivate($hw_top)
EndFunc ;==>GuiMove