防止鼠标单击时的默认操作

时间:2017-12-14 10:22:24

标签: mouseevent autoit mouseclick-event

我想在鼠标点击时MsgBox(),但要防止鼠标点击的自然行为。这意味着如果我点击Stack Overflow上的任何链接,我的活动页面必须保持不变。

#include <MsgBoxConstants.au3>
#Include <Misc.au3>

While 1
    If _IsPressed(01) Then ShowAlert() ; 01 is for left mouse button
    Sleep (100)
WEnd

Func ShowAlert()
    MsgBox($MB_SYSTEMMODAL, "", "Test")
EndFunc

2 个答案:

答案 0 :(得分:2)

这应该可以解决问题:

#include <Constants.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>

Global Const $HC_ACTION = 0
Global $hStub_MouseProc = DllCallbackRegister("_MouseProc", "long", "int;wparam;lparam")
Global $hmod = _WinAPI_GetModuleHandle(0)
Global $hHook = _WinAPI_SetWindowsHookEx($WH_MOUSE_LL, DllCallbackGetPtr($hStub_MouseProc), $hmod)

HotKeySet('^+!e', '_ende') ; CTRL+SHIFT+ALT+E to exit.
OnAutoItExitRegister('ExitFunc')

While 1
    Sleep(100)
WEnd

Func _ende()
    Exit
EndFunc   ;==>_ende

Func _MouseProc($nCode, $wParam, $lParam)
    If $nCode < 0 Then
        Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
    EndIf
    If $nCode = $HC_ACTION Then
        Switch $wParam
            Case 0x0201
                ConsoleWrite('No Click!' & @CRLF)
                Return -1
;~          Case $WM_MBUTTONUP
;~              Return _mouse_event($MOUSEEVENTF_RIGHTUP) ; Returns -1; mouse click will be ignored.
        EndSwitch
    EndIf
    Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
EndFunc   ;==>_MouseProc

Func ExitFunc()
    _WinAPI_UnhookWindowsHookEx($hHook)
    DllCallbackFree($hStub_MouseProc)
EndFunc   ;==>ExitFunc

Func _mouse_event($dwFlags)
    DllCall("user32", 'long', 'mouse_event', 'long', $dwFlags, 'long', 0, 'long', 0, 'long', 0, 'long', 0)
    Return -1
EndFunc   ;==>_mouse_event

答案 1 :(得分:1)

  

......同时防止它的自然行为......

根据MouseOnEvent UDF

  

此UDF允许为鼠标设备设置事件处理程序。

示例(使用ConsoleWrite()作为MsgBox()需要阻止鼠标单击才能关闭):

#include "MouseOnEvent.au3"

Global Const $g_sKeyQuit    = '{esc}'
Global Const $g_iDelay      = 10
Global Const $g_bBlockInput = True

Global       $g_bStateQuit  = False

Main()

Func Main()
    HotKeySet($g_sKeyQuit, 'Quit')
    _MouseSetOnEvent($MOUSE_PRIMARYDOWN_EVENT, '_MousePrimaryDown_Event')
    _MouseSetOnEvent($MOUSE_PRIMARYDBLCLK_EVENT, '_MousePrimaryDblclk_Event')

    While Not $g_bStateQuit
        Sleep($g_iDelay)
    WEnd

    Exit
EndFunc

Func Quit()
    $g_bStateQuit = True
EndFunc

Func _MousePrimaryDown_Event()
    ConsoleWrite('Detected $MOUSE_PRIMARYDOWN_EVENT' & @CRLF)
    Return $g_bBlockInput ? $MOE_BLOCKDEFPROC : $MOE_RUNDEFPROC
EndFunc

Func _MousePrimaryDblclk_Event()
    ConsoleWrite('Detected $MOUSE_PRIMARYDBLCLK_EVENT' & @CRLF)
    Return $g_bBlockInput ? $MOE_BLOCKDEFPROC : $MOE_RUNDEFPROC
EndFunc

UDF也允许将事件注册到特定窗口。它可以在检测后阻止或传递事件,并包括如何限制屏幕部分功能的示例。