Jump to content
Sign in to follow this  
eltorro

MsgBox anywhere on the screen

Recommended Posts

eltorro

Edit: this script is obsolete as there are ways to do this without the dll.

see here.

Necessity is the Mother of invention.

Example of using Larry's hook.dll to hook a MsgBox and move it anywhere on the screen. Call the _MsgBoxAu3() function the same as a regular MsgBox but with extra params for x,y,w,h

;------------------------------------------------------------------------------
;
; AutoIt Version: 3.1.1++
; Language:       English
; Description:    Functions to move a message box
; Author:            eltorro <steve@ocotillo.sytes.net>
; ------------------------------------------------------------------------------

#Compiler_AU3Check_Parameters= -q -d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w 7 ;Au3Check parameters
Opt("MustDeclareVars", 1)
Opt("WinTitleMatchMode", 2)
;===============================================================================
Global $DLLinst, $hhCBT
Global $dll
Global $MbRect[5] ;index 0 is for hwnd, 1-4 are left, top, width, height
;===============================================================================
Const $WH_CBT = 5;window message to catch
Const $HCBT_SETFOCUS = 0x1400 + 0x1A30
Const $HCBT_ACTIVATE = 0x1400 + 0x1A31;hook this one
Const $HCBT_CREATEWND = 0x1400 + 0x1A32
Const $HCBT_DESTROYWND = 0x1400 + 0x1A33
Const $HCBT_MINMAX = 0x1400 + 0x1A34
;===============================================================================
$dll = @ScriptDir & "\hook.dll" ; Where did that dll go????
If Not FileExists($dll) Then
    MsgBox(266288, "Error", "This example requires hook.dll." & @LF & _
            "Please correct the path in the script if you have the dll.")
    Exit
EndIf

;===============================================================================
; Put message boxes at different places on the screen.
For $i = 0 To 50 Step 10
    _MsgBoxAu3(4096, "MsgBoxMove", "Msgbox #" & $i, 3, ($i * 10) , ($i * 10), 200+ ($i * 10), 100+ ($i * 10))
Next
Exit
;===============================================================================
Func OnAutoItExit()
    DllCall("user32.dll", "int", "UnhookWindowsHookEx", "hwnd", $hhCBT[0])
    DllCall("kernel32.dll", "int", "FreeLibrary", "hwnd", $DLLinst[0])
EndFunc   ;==>OnAutoItExit


;===============================================================================
;
; Function Name:   _MsgBoxAu3()
; Description:        Displays a MsgBox at the specified point
; Parameter(s):     $sFlag  Standard message box flags
;                    $sTitle The title of the message box.
;                    $sText  The text of the message box
;                    $iTime  The optional timeout
;                    $x=0    The top pos
;                    $y=0    The left pos
;                    $w=0     The width
;                    $h=0     The heighth
; Requirement(s):  hook.dll and beta see post [url="http://www.autoitscript.com/forum/index.php?showtopic=23173"]http://www.autoitscript.com/forum/index.php?showtopic=23173[/url]
; Return Value(s): The return of the msgbox or 0 on failure
; Author(s):       eltorro <steve@ocotillo.sytes.net>
;
;===============================================================================
Func _MsgBoxAu3($iFlag, $sTitle, $sText, $iTime = 0, $x = 0, $y = 0, $w = 0, $h = 0)
    Local $cbtHOOKproc
    Local $ret = 0
    $DLLinst = DllCall("kernel32.dll", "hwnd", "LoadLibrary", "str", $dll)
    $cbtHOOKproc = DllCall("kernel32.dll", "hwnd", "GetProcAddress", "hwnd", $DLLinst[0], "str", "CBTProc")
    $hhCBT = DllCall("user32.dll", "hwnd", "SetWindowsHookEx", "int", $WH_CBT, _
            "hwnd", $cbtHOOKproc[0], "hwnd", $DLLinst[0], "int", 0)
    ;**** Create a dummy window for the hook.  Couldn't get this to work with Desktop hwnd.
    Local    $gui = GUICreate("") ;little hack
    DllCall($dll, "int", "SetValuesCBT", "hwnd", $gui, "hwnd", $hhCBT[0])
    If @error Then
        MsgBox(266288, "Error", "There was an erro with hook.dll." & @LF & "Exiting application now.")
        Exit
    EndIf
    
    GUIRegisterMsg($HCBT_ACTIVATE, "_MsgBoxMove")
    $MbRect[0] = 0
    $MbRect[1] = $x
    $MbRect[2] = $y
    $MbRect[3] = $w
    $MbRect[4] = $h
    If $iTime Then
        $ret = MsgBox($iFlag, $sTitle, $sText, $iTime)
    Else
        $ret = MsgBox($iFlag, $sTitle, $sText)
    EndIf
    ;----- Release Hook and Library.
    DllCall("user32.dll", "int", "UnhookWindowsHookEx", "hwnd", $hhCBT[0])
    DllCall("kernel32.dll", "int", "FreeLibrary", "hwnd", $DLLinst[0])
    GUIDelete($gui) ; delete dummy window.
    Return $ret
EndFunc   ;==> _MsgBoxAu3
;===============================================================================
;
; Function Name:   _MsgBoxMove
; Description:      GuiRegister function
; Parameter(s):     $hWndGUI     The Window handle of the GUI in which the message appears.
;                     $MsgID        The Windows message ID.
;                     $WParam        The first message parameter as hex value
;                    $LParam        The second message parameter as hex value.
; Requirement(s):
; Return Value(s):
; Author(s):
;
;===============================================================================
Func _MsgBoxMove($hWndGUI, $MsgID, $WParam, $LParam)
    Local $rc, $n, $buffer
    $n += 1
    If $n > 25 Then
        $n = 25
        $buffer = StringTrimLeft($buffer, StringInStr($buffer, @LF))
    EndIf
    $buffer &= "CBT: " & $hWndGUI & "," & $MsgID & "," & $WParam & "," & $LParam & @LF
    ToolTip($buffer)
    if    $MsgId = $HCBT_CREATEWND Then Sleep(10000)
    If $MsgID = $HCBT_ACTIVATE Then
        If $MbRect[0] = 0 Then
            $rc = DllStructCreate("int;int;int;int")
            DllCall("user32.dll", "int", "GetWindowRect", "hwnd", $WParam, "ptr", DllStructGetPtr($rc))
            If $MbRect[3] = 0 Then $MbRect[3] = DllStructGetData($rc, 3) - DllStructGetData($rc, 1)
            If $MbRect[4] = 0 Then $MbRect[4] = DllStructGetData($rc, 4) - DllStructGetData($rc, 2)
            DllCall("user32.dll", "int", "MoveWindow", "hwnd", $WParam, "int", $MbRect[1], "int", $MbRect[2], "int", $MbRect[3], "int", $MbRect[4], "int", True)
            $MbRect[0] = $WParam
        EndIf
    EndIf
    $rc = 0
    ;Return $GUI_RUNDEFMSG
EndFunc   ;==>_MsgBoxMove

Regards,

eltorro

Edit: added hyperlink to Larry's hook topic.

Edited by eltorro

Share this post


Link to post
Share on other sites
SmOke_N

Really nice job eltorro!


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites
WTS

pretty cool eltorrro

I think this should be the path?

$dll = @ScriptDir & "\hook.dll"; Where did that dll go????

Share this post


Link to post
Share on other sites
eltorro

pretty cool eltorrro

I think this should be the path?

$dll = @ScriptDir & "\hook.dll"; Where did that dll go????
If that's where you have it. :D

Share this post


Link to post
Share on other sites
Valuater

Nice... added to Autoit Wrappers also

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites
eltorro

Nice... added to Autoit Wrappers also

8)

Great!

@ everyone,

Thanks needs to go to Larry for his dll. As without it... well, the script doesn't work.

Thanks Larry :D

Edited by eltorro

Share this post


Link to post
Share on other sites
busysignal

Great work with the dll hooking. The example worked great.. Thanks Larry & eltorro..

Cheers.. :D

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×