Jump to content
Sign in to follow this  
big_daddy

Using Common Dialog Boxes

Recommended Posts

big_daddy

Ctrl + F ??

I know it not the point.

I'm asking how to call the dialog box from a GUI created with AutoIt.

Share this post


Link to post
Share on other sites
WolfWorld

Oh, Here

#include <GuiEdit.au3>

Opt('MustDeclareVars', 1)

$Debug_Ed = False ; Check ClassName being passed to Edit functions, set to True and use a handle to another control to see it work

Example_Internal()
Example_External1()
Example_External2()

Func Example_Internal()
    Local $s_texttest = "this is a test" & @CRLF & _
            "this is only a test" & @CRLF & _
            "this testing should work for you as well as it does for me"
    Local $GuiCtrlEditSearch, $Button1, $Button2, $msg, $aResult, $aMargins, $hEdit
    
    $GuiCtrlEditSearch = GUICreate('(Internal) Find And Replace Example with AutoIt ' & FileGetVersion(@AutoItExe), 622, 448, 192, 125)
    $hEdit = GUICtrlCreateEdit($s_texttest, 64, 24, 505, 233, _
            BitOR($ES_AUTOVSCROLL, $WS_VSCROLL, $ES_MULTILINE, $WS_HSCROLL, $ES_NOHIDESEL))
    $Button1 = GUICtrlCreateButton("Find", 176, 288, 121, 33, 0)
    $Button2 = GUICtrlCreateButton("Find And Replace", 368, 288, 121, 33, 0)
    GUISetState()

    While 1
        $msg = GUIGetMsg()
        Select
            Case $msg = $GUI_EVENT_CLOSE
                ExitLoop
            Case $msg = $Button1
                _GUICtrlEdit_Find ($hEdit)
            Case $msg = $Button2
                _GUICtrlEdit_Find ($hEdit, True)
            Case Else
                ;;;;;;;
        EndSelect
    WEnd
    GUIDelete()
EndFunc   ;==>Example_Internal

Func Example_External1()
    Local $s_texttest = "this is a test" & @CRLF & _
            "this is only a test" & @CRLF & _
            "this testing should work for you as well as it does for me"
    Local $GuiCtrlEditSearch, $Button1, $Button2, $msg, $aResult, $aMargins, $hEdit
    
    $GuiCtrlEditSearch = GUICreate('(External) Find And Replace Example with AutoIt ' & FileGetVersion(@AutoItExe), 622, 448, 192, 125)
    $hEdit = _GUICtrlEdit_Create ($GuiCtrlEditSearch, $s_texttest, 64, 24, 505, 233, _
            BitOR($ES_AUTOVSCROLL, $WS_VSCROLL, $ES_MULTILINE, $WS_HSCROLL, $ES_NOHIDESEL))
    $Button1 = GUICtrlCreateButton("Find", 176, 288, 121, 33, 0)
    $Button2 = GUICtrlCreateButton("Find And Replace", 368, 288, 121, 33, 0)
    GUISetState()
    
    While 1
        $msg = GUIGetMsg()
        Select
            Case $msg = $GUI_EVENT_CLOSE
                ExitLoop
            Case $msg = $Button1
                _GUICtrlEdit_Find ($hEdit)
            Case $msg = $Button2
                _GUICtrlEdit_Find ($hEdit, True)
            Case Else
                ;;;;;;;
        EndSelect
    WEnd
    GUIDelete()
EndFunc   ;==>Example_External1

Func Example_External2()
    Local $s_texttest = 'Find And Replace Example with AutoIt ' & FileGetVersion(@AutoItExe) & @LF & _
            "this is a test" & @LF & _
            "this is only a test" & @LF & _
            "this testing should work for you as well as it does for me"
    Local $whandle, $handle
    Local $Title = "Untitled - Notepad"
    
    Run("notepad", "", @SW_MAXIMIZE)
    ;Wait for the window "Untitled" to exist
    WinWait($Title)
    
    ; Get the handle of a notepad window
    $whandle = WinGetHandle($Title)
    If @error Then
        MsgBox(4096, "Error", "Could not find the correct window")
    Else
        $handle = ControlGetHandle($whandle, "", "Edit1")
        If @error Then
            MsgBox(4096, "Error", "Could not find the correct control")
        Else
            ; Send some text directly to this window's edit control
            ControlSend($whandle, "", "Edit1", $s_texttest)
            _GUICtrlEdit_Find ($handle, True)
        EndIf
    EndIf
EndFunc   ;==>Example_External2

Share this post


Link to post
Share on other sites
ResNullius

Anyone know how to use the standard Windows Find Dialog Box? I would like to use it for Uninstall Apps, but I don't know where to even start.

post-8730-1200153184_thumb.jpg

http://msdn2.microsoft.com/en-us/library/m...px#finding_text

Don't know if this will help, but it's from a project called "Windows API for VB"

http://www.krugle.org/files/archive/archiv...API/CommDlg.bas

Search the page for "FINDREPLACE". It's way beyond my comprehension at this point, but might get you started?...

Share this post


Link to post
Share on other sites
big_daddy

@ResNullius - This section look promising, thanks. Now maybe an API guru can lend some assistance.

Public Type FINDREPLACEA
    lStructSize As Long ' size of this struct &H20
    hwndOwner As Long ' handle to owner's window
    hInstance As Long ' instance handle of.EXE that
'                    '   contains cust. dlg. template
    Flags As Long ' one or more of the FR_??
    lpstrFindWhat As String ' ptr. to search string
    lpstrReplaceWith As String ' ptr. to replace string
    wFindWhatLen As Integer ' size of find buffer
    wReplaceWithLen As Integer ' size of replace buffer
    lCustData As Long ' data passed to hook fn.
    lpfnHook As Long ' ptr. to hook fn. or NULL
    lpTemplateName As String ' custom template name
End Type
'typedef struct tagFINDREPLACEW {
'   DWORD       lStructSize;        ' size of this struct &H20
'   HWND         hwndOwner;       ' handle to owner's window
'   HINSTANCE   hInstance;        ' instance handle of.EXE that
'                                   '   contains cust. dlg. template
'   DWORD       Flags;            ' one or more of the FR_??
'   LPWSTR     lpstrFindWhat;     ' ptr. to search string
'   LPWSTR     lpstrReplaceWith;   ' ptr. to replace string
'   WORD         wFindWhatLen;     ' size of find buffer
'   WORD         wReplaceWithLen;   ' size of replace buffer
'   LPARAM     lCustData;         ' data passed to hook fn.
'   LPFRHOOKPROC lpfnHook;         ' ptr. to hook fn. or NULL
'   LPCWSTR   lpTemplateName;    ' custom template name
'} FINDREPLACEW, *LPFINDREPLACEW;
'
Public Const FR_DOWN = &H1
Public Const FR_WHOLEWORD = &H2
Public Const FR_MATCHCASE = &H4
Public Const FR_FINDNEXT = &H8
Public Const FR_REPLACE = &H10
Public Const FR_REPLACEALL = &H20
Public Const FR_DIALOGTERM = &H40
Public Const FR_SHOWHELP = &H80
Public Const FR_ENABLEHOOK = &H100
Public Const FR_ENABLETEMPLATE = &H200
Public Const FR_NOUPDOWN = &H400
Public Const FR_NOMATCHCASE = &H800
Public Const FR_NOWHOLEWORD = &H1000
Public Const FR_ENABLETEMPLATEHANDLE = &H2000
Public Const FR_HIDEUPDOWN = &H4000
Public Const FR_HIDEMATCHCASE = &H8000
Public Const FR_HIDEWHOLEWORD = &H10000

Public Declare Function FindTextA Lib "commdlg32" (lp As FINDREPLACEA) As Long
'HWND  APIENTRY FindTextW(LPFINDREPLACEW);
Public Declare Function AfxReplaceTextA Lib "commdlg32" (lp As FINDREPLACEA) As Long
'HWND  APIENTRY AfxReplaceTextW(LPFINDREPLACEW);

Share this post


Link to post
Share on other sites
Siao

Eh.

#include <GuiEdit.au3>

Const $FR_DOWN = 0x1
Const $FR_WHOLEWORD = 0x2
Const $FR_MATCHCASE = 0x4
Const $FR_FINDNEXT = 0x8
Const $FR_REPLACE = 0x10
Const $FR_REPLACEALL = 0x20

$hGui = GUICreate("Find/Replace Test", 300, 300)
$cEdit = GUICtrlCreateEdit("abcdef", 0,0, 300, 280)
$cBtn1 = GUICtrlCreateButton("Find...", 40, 280, 100, 20)
$cBtn2 = GUICtrlCreateButton("Replace...", 160, 280, 100, 20)

GUIRegisterMsg(RegisterWindowMessage("commdlg_FindReplace"), "FRMsg")

$hFR_hook = DllCallbackRegister("FRHookProc", "int", "hwnd;uint;wparam;lparam")

$tFINDREPLACE = DllStructCreate("dword;hwnd hOwner;hwnd hInstance;dword Flags;ptr FindWhat;ptr ReplaceWith;ushort FindLen;ushort ReplaceLen;ptr CustData;ptr pHook;ptr")
$tFindBuffer = DllStructCreate("char[256]")
$tReplaceBuffer = DllStructCreate("char[256]")
DllStructSetData($tFINDREPLACE, 1, DllStructGetSize($tFINDREPLACE))
DllStructSetData($tFINDREPLACE, "hOwner", $hGui)
DllStructSetData($tFINDREPLACE, "Flags", 0x101) ;FR_DOWN+FR_ENABLEHOOK
DllStructSetData($tFINDREPLACE, "FindWhat", DllStructGetPtr($tFindBuffer))
DllStructSetData($tFINDREPLACE, "FindLen", 255)
DllStructSetData($tFINDREPLACE, "ReplaceWith", DllStructGetPtr($tReplaceBuffer))
DllStructSetData($tFINDREPLACE, "ReplaceLen", 255)
DllStructSetData($tFINDREPLACE, "pHook", DllCallbackGetPtr($hFR_hook))

GUISetState()
While 1
    Switch GUIGetMsg()
        Case -3
            Exit
        Case $cBtn1
            $hFind = FindTextCreate()
        Case $cBtn2
            $hReplace = ReplaceTextCreate()
    EndSwitch
WEnd


Func FRMsg($hWnd, $Msg, $wParam, $lParam)
;   $lParam = pointer to $tFINDREPLACE struct, but if we have the struct as global, we don't need it.
    Local $iFlags = DllStructGetData($tFINDREPLACE, "Flags")
    If BitAND($iFlags, $FR_FINDNEXT) Then
        ;If BitAND($iFlags, $FR_DOWN) Then $iDirection = 1
        ;If BitAND($iFlags, $FR_MATCHCASE) Then $bMatchCase = 1
        ;If BitAND($iFlags, $FR_WHOLEWORD) Then $bWholeWord = True
        ;$sFindWhat = DllStructGetData($tFindBuffer, 1)
        TextFind()
    ElseIf BitAND($iFlags, $FR_REPLACE) Then
        TextReplace()
    ElseIf BitAND($iFlags, $FR_REPLACEALL) Then
        TextReplaceAll()
    EndIf
EndFunc
Func FRHookProc($hWnd, $Msg, $wParam, $lParam)
    Switch $Msg
        Case $WM_INITDIALOG
            Return 1
        Case Else
            ;ConsoleWrite('FRHookProc MSG=' & Hex($Msg) & ' wparam=' & Hex($wParam) & ' lparam=' & Hex($lParam) & @CRLF)
    EndSwitch
EndFunc
Func FindTextCreate()
    Local $aRet = DllCall('comdlg32.dll', 'hwnd', 'FindText', 'ptr', DllStructGetPtr($tFINDREPLACE))
    If $aRet[0] <> 0 Then Return $aRet[0]
    $aRet = DllCall('comdlg32.dll', 'int', 'CommDlgExtendedError')
    Return SetError($aRet[0],0,0)
EndFunc
Func ReplaceTextCreate()
    Local $aRet = DllCall('comdlg32.dll', 'hwnd', 'ReplaceText', 'ptr', DllStructGetPtr($tFINDREPLACE))
    If $aRet[0] <> 0 Then Return $aRet[0]
    $aRet = DllCall('comdlg32.dll', 'int', 'CommDlgExtendedError')
    Return SetError($aRet[0],0,0)
EndFunc
Func RegisterWindowMessage($sText)
    Local $aRet = DllCall('user32.dll', 'int', 'RegisterWindowMessage', 'str', $sText)
    ConsoleWrite('RegisterWindowMessage ' & $aRet[0] & @CRLF)
    Return $aRet[0]
EndFunc
Func TextFind()
    ConsoleWrite('TextFind' & @CRLF)
EndFunc
Func TextReplace()
    ConsoleWrite('TextReplace' & @CRLF) 
EndFunc
Func TextReplaceAll()
    ConsoleWrite('TextReplaceAll' & @CRLF)
EndFunc

All the FR_HookProc via DllCallback stuff isn't mandatory, included just to show.

Have fun writing all the actual find/replace code.

Edited by Siao

"be smart, drink your wine"

Share this post


Link to post
Share on other sites
big_daddy

Outstanding! Thank you.

Here is what I've stripped it down to for my needs.

#include <GuiEdit.au3>

Const $FR_DOWN = 0x1
Const $FR_MATCHCASE = 0x4
Const $FR_FINDNEXT = 0x8
Const $FR_HIDEWHOLEWORD = 0x10000

$hGui = GUICreate("Find/Replace Test", 300, 300)
$cEdit = GUICtrlCreateEdit("abcdef", 0, 0, 300, 280)
$cBtn1 = GUICtrlCreateButton("Find...", 40, 280, 100, 20)

GUIRegisterMsg(RegisterWindowMessage("commdlg_FindReplace"), "FR_MSG")

$tFINDREPLACE = DllStructCreate("dword;hwnd hOwner;hwnd hInstance;dword Flags;" & _
        "ptr FindWhat;ptr ReplaceWith;ushort FindLen;ushort ReplaceLen;ptr CustData;ptr pHook;ptr")
$tFindBuffer = DllStructCreate("char[256]")
$tReplaceBuffer = DllStructCreate("char[256]")
DllStructSetData($tFINDREPLACE, 1, DllStructGetSize($tFINDREPLACE))
DllStructSetData($tFINDREPLACE, "hOwner", $hGui)
DllStructSetData($tFINDREPLACE, "Flags", BitOR($FR_DOWN, $FR_HIDEWHOLEWORD))
DllStructSetData($tFINDREPLACE, "FindWhat", DllStructGetPtr($tFindBuffer))
DllStructSetData($tFINDREPLACE, "FindLen", 255)
DllStructSetData($tFINDREPLACE, "ReplaceWith", DllStructGetPtr($tReplaceBuffer))
DllStructSetData($tFINDREPLACE, "ReplaceLen", 255)

GUISetState()
While 1
    Switch GUIGetMsg()
        Case - 3
            Exit
        Case $cBtn1
            $hFind = FindTextCreate()
    EndSwitch
WEnd


Func FR_MSG($hWnd, $Msg, $wParam, $lParam)
    ;   $lParam = pointer to $tFINDREPLACE struct, but if we have the struct as global, we don't need it.
    Local $iDirection = 0, $fMatchCase = False, $iFlags = DllStructGetData($tFINDREPLACE, "Flags")
    $sFindWhat = DllStructGetData($tFindBuffer, 1)
    If BitAND($iFlags, $FR_FINDNEXT) Then
        If BitAND($iFlags, $FR_DOWN) Then $iDirection = 1
        If BitAND($iFlags, $FR_MATCHCASE) Then $fMatchCase = True
        ;If BitAND($iFlags, $FR_WHOLEWORD) Then $bWholeWord = True
        ConsoleWrite("Find: " & $sFindWhat & @CR)
        ConsoleWrite("Direction: " & $iDirection & @CR)
        ConsoleWrite("Match Case: " & $fMatchCase & @CR)
    EndIf
EndFunc   ;==>FR_MSG

Func FindTextCreate()
    Local $aRet = DllCall('comdlg32.dll', 'hwnd', 'FindText', 'ptr', DllStructGetPtr($tFINDREPLACE))
    If $aRet[0] <> 0 Then Return $aRet[0]
    $aRet = DllCall('comdlg32.dll', 'int', 'CommDlgExtendedError')
    Return SetError($aRet[0], 0, 0)
EndFunc   ;==>FindTextCreate

Func RegisterWindowMessage($sText)
    Local $aRet = DllCall('user32.dll', 'int', 'RegisterWindowMessage', 'str', $sText)
    Return $aRet[0]
EndFunc   ;==>RegisterWindowMessage

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.