AutoIt Snippets

281 posts in this topic

I was trying to make a downloader similar to Youtube Downloader and wanted to be able to grab urls from the clip board when I move my mouse inside the edit control (If you've used Youtube downloader you know what I'm talking about). I came up with this and it's a pretty nifty little snippit

#include <GUIConstants.au3>
#include <Memory.au3>
#include <WinAPIShellEx.au3>
#include <GuiEdit.au3>

Global Enum $idProcEdit = 9999

Global $hMain = GUICreate("Capture clipboard in edit", 400, 400)
GUICtrlCreateLabel("Move mouse over the edit control", 10, 10, 380, 20)
Global $edtEdit = GUICtrlCreateEdit("Move mouse into edit", 10, 35, 380, 360)
Global $hEdtEdit = GUICtrlGetHandle($edtEdit)
Global $tPData = DllStructCreate("struct;char func[9];int iControl;endstruct")
Global $hNewWindowProc = DllCallbackRegister(NewWindowProc, "lresult", "hwnd;uint;wparam;lparam;uint_ptr;dword_ptr")
Global $pNewWindowProc = DllCallbackGetPtr($hNewWindowProc)
$tPData.func = "Example"
$tPData.iControl = $edtEdit

_WinAPI_SetWindowSubclass($hEdtEdit, $pNewWindowProc, $idProcEdit, DllStructGetPtr($tPData))

GUISetState(@SW_SHOW, $hMain)

While (True)
    Switch (GUIGetMsg())
        Case $GUI_EVENT_CLOSE
            _WinAPI_RemoveWindowSubclass($hEdtEdit, $pNewWindowProc, $idProcEdit)
            Exit 0

Func Example($idControl)
    Return GUICtrlSetData($idControl, GUICtrlRead($idControl) & @CRLF & ClipGet())

Func NewWindowProc($hWnd, $iMsg, $wParam, $lParam, $uIdSubclass, $dwRefData)
    #forceref $hWnd, $iMsg, $wParam, $lParam, $uIdSubclass, $dwRefData
    Local Static $bInserted = False

    Switch ($uIdSubclass)
        Case $idProcEdit
            Switch ($iMsg)
                Case $WM_CHAR
                    ; Ctrl + A, highlight all of the control that has focus
                    If ($wParam = 1) Then Return _GUICtrlEdit_SetSel($hWnd, 0, -1)

                ; Mouse moved in the control or user did Ctrl + V
                Case $WM_MOUSEMOVE, $WM_PASTE
                    If ($hWnd = $hEdtEdit And $bInserted = False) Then
                        $bInserted = True
                        Local $pData = DllStructCreate("struct;char func[64];int iControl;endstruct", $dwRefData)
                        Return Call($pData.func, $pData.iControl)

                Case $WM_NCMOUSEMOVE
                    $bInserted = False

    Return _WinAPI_DefSubclassProc($hWnd, $iMsg, $wParam, $lParam)
EndFunc   ;==>NewWindowProc

In my example I put a function to call associated with the edit control id in a struct and passed it to my window proc. This would let you call a specific function per control if you had multiple edit (or input boxes) that needed to do different things

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