Jump to content

Recommended Posts

Posted

Hey there.

Quick question. I need to poll a GUI combo box constantly for keypresses, and then GUICtrlRead and call a function...but call a function only once.

Thanks in advance.

[font="Impact"]Cats rule, humans drool.[/font]
Posted

Hey there.

Quick question. I need to poll a GUI combo box constantly for keypresses, and then GUICtrlRead and call a function...but call a function only once.

Thanks in advance.

What action are you trying to monitor by polling for keypresses? A change in the combobox value?

If so that already generates a message that can be acted on.

Do you have some sample code you can post of what you're trying to do?

Posted (edited)

Sorry for the delay in response. I actually switched to using an edit control now, so that I can use SHAutoComplete. Basically, I have a run box that I want to add a history feature to. If possible, I want to create a ComboBoxEx so that SHAutoComplete can be called (after using CBEM_GETEDITCONTROL) and then integrate history into the same box.

Hope that makes sense. Here's my full code:

#Include <GUIConstants.au3>
#Include <File.au3>
#Include <Array.au3>
#Include <Misc.au3>
#NoTrayIcon

Opt("RunErrorsFatal", 0)
Opt("WinTitleMatchMode", 3)

$g_szVersion = "mRun"
If WinExists($g_szVersion) Then Exit
AutoItWinSetTitle($g_szVersion)

; --- Define Functions ---

Func _StartRun($sCmd, $sFolder = @ScriptDir, $rState = @SW_SHOWNORMAL)
    Local $prevRunFatal = Opt("RunErrorsFatal", 0)

    Local $pid = Run($sCmd, $sFolder, $rState)

    If $pid = 0 Then
        Local $aRet = DllCall("shell32.dll", "long", "ShellExecute", _
                "hwnd", 0, _
                "string", "", _
                "string", $sCmd, _
                "string", "", _
                "string", $sFolder, _
                "int", $rState)
        Local $errorText = _GetLastErrorMessage()

        If $aRet[0] > 32 Then
            Opt("RunErrorsFatal", $prevRunFatal)
            Return 1
        Else
            Opt("RunErrorsFatal", $prevRunFatal)
            Return $errorText
        EndIf
    Else
        Opt("RunErrorsFatal", $prevRunFatal)
        Return 1
    EndIf
EndFunc   ;==>_StartRun

Func _GetLastErrorMessage()
    Local $ret, $s
    Local $p = DllStructCreate("char[4096]")
    Local Const $FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000

    If @error Then Return ""

    $ret = DllCall("Kernel32.dll", "int", "GetLastError")

    $ret = DllCall("kernel32.dll", "int", "FormatMessage", _
            "int", $FORMAT_MESSAGE_FROM_SYSTEM, _
            "ptr", 0, _
            "int", $ret[0], _
            "int", 0, _
            "ptr", DllStructGetPtr($p), _
            "int", 4096, _
            "ptr", 0)
    $s = DllStructGetData($p, 1)
    Return $s
EndFunc   ;==>_GetLastErrorMessage

Func _StartRun98($sCmd, $sFolder = @WorkingDir, $rState = @SW_SHOWNORMAL)
    Local $prevRunFatal = Opt("RunErrorsFatal", 0)
    Local $pid = Run($sCmd, $sFolder, $rState)
    Opt("RunErrorsFatal", $prevRunFatal)

    If $pid = 0 Then
        $RetVal = ShellExecute($sCmd, "", $sFolder, "", $rState)
        Return $RetVal
    Else
        Return 1
    EndIf
EndFunc   ;==>_StartRun98

Func _CheckConfig()
    Global $hist = "m-run.hist"
    Global $config = "m-config.ini"
    If Not FileExists($config) Then
        $mrun =  "Title=mRun" & @LF _
                 & "Width=150" & @LF _
                 & "Height=20" & @LF _
                 & "FontN=Corbel" & @LF _
                 & "FontS=8" & @LF _
                 & "ShowBrowse=1"
        $transparency = "FadeIn=1" & @LF _
                 & "inStep=5" & @LF _
                 & "inFromTo=0-255" & @LF _
                 & "FadeOut=1" & @LF _
                 & "outStep=-5" & @LF _
                 & "outFromTo=255-0"
        $aliases = "ce=C:\Program Files\CubicExplorer\CubicExplorer.exe"
        $envvars = "BB=C:\Blackbox"
        IniWriteSection($config, "mRun", $mrun)
        IniWriteSection($config, "Transparency", $transparency)
        IniWriteSection($config, "Aliases", $aliases)
    EndIf
    Global $title = IniRead($config, "mRun", "Title", "mRun")
    Global $width = IniRead($config, "mRun", "Width", "150")
    Global $height = IniRead($config, "mRun", "Height", "20")
    Global $fontn = IniRead($config, "mRun", "FontN", "Corbel")
    Global $fonts = IniRead($config, "mRun", "FontS", "8")
    Global $showb = IniRead($config, "mRun", "ShowBrowse", "1")
    Global $fadein = IniRead($config, "Transparency", "FadeIn", "1")
    Global $instep = IniRead($config, "Transparency", "inStep", "5")
    Global $inft = IniRead($config, "Transparency", "inFromTo", "0-255")
    Global $fadeout = IniRead($config, "Transparency", "FadeOut", "1")
    Global $outstep = IniRead($config, "Transparency", "outStep", "-5")
    Global $outft = IniRead($config, "Transparency", "outFromTo", "255-0")
    Global $alist = IniReadSection($config, "Aliases")
    If @error = 1 Then
        Global $checkalias = 0
    Else
        Global $checkalias = 1
    EndIf
EndFunc   ;==>_CheckConfig

Func _ReadHist()
    $sFile = "mrun.hist"
    If FileExists($sFile) Then
        Dim $sNewLines
        $hOpen = FileOpen($sFile, 0)
        For $i = 1 To _FileCountLines($sFile)
            $sLine = FileReadLine($sFile, $i)
            $sLine = StringStripWS($sLine, 3)
            If StringLen($sLine) > 0 Then
                $sNewLines = $sNewLines & $sLine & "|"
            EndIf
        Next
        ; MsgBox(0, "", $sNewLines)
        Global $sLArray = StringSplit($sNewLines, "|")
    Else
        $hOpen = FileOpen($sFile, 2)
        FileClose($hOpen)
        Global $sLArray[0] = 0
    EndIf
EndFunc   ;==>_ReadHist

Func _WriteHist($sFile, $sItem)
    $hOpen = FileOpen($sFile, 1)
    FileWrite($sFile, $sItem)
    FileClose($hOpen)
EndFunc   ;==>_WriteHist

Func _SHAutoComplete()
    $hInput = DllCall("user32.dll", "hwnd", "GetDlgItem", "hwnd", $GUI, "int", $input)
    If $ole32 <> -1 And $shlwapi <> -1 Then DllCall($shlwapi, "int", "SHAutoComplete", "hwnd", $hInput[0], "int", 0)
EndFunc   ;==>_SHAutoComplete

Func _CreateGUI()
    Global $GUI = GUICreate($title, $width, $height + 2, 1, 1, BitOR($WS_POPUP, $WS_SYSMENU), BitOR($WS_EX_TOOLWINDOW, $WS_EX_TOPMOST, $WS_EX_ACCEPTFILES))
    If $showb = 1 Then
        $iwidth = $width - 24
        $iheight = $height + 2
        $xpos = 24
        $browse = GUICtrlCreateButton("", 0, 0, 24, 22, $BS_ICON)
        GUICtrlSetImage($browse, "shell32.dll", 4, 0)
    Else
        $iwidth = $width
        $iheight = $height + 2
        $xpos = 0
    EndIf
    Global $input = GUICtrlCreateInput("", $xpos, 0, $iwidth, $iheight)
    _SHAutoComplete()
    _ReadHist()
    
    Global $gobtn = GUICtrlCreateButton("Go", $width, $height - 2, 10, 10, $BS_DEFPUSHBUTTON)
    GUICtrlSetState($input, $GUI_FOCUS)
    GUICtrlSetFont($input, $fonts, -1, -1, $fontn)
    WinSetTrans($GUI, "", 0)
    GUISetState()
    
    _FadeIn()
    While 1
        $msg = GUIGetMsg()
        Select
            Case $msg = $GUI_EVENT_CLOSE
                _FadeOut()
                _PreExit()
                Exit
            Case $msg = $gobtn
                _RunThis()
                _FadeOut()
                _PreExit()
                Exit
            Case $showb = 1
                If $msg = $browse Then
                    _Browse()
                EndIf
        EndSelect
    WEnd
EndFunc   ;==>_CreateGUI

Func _Browse()
    GUISetState(@SW_HIDE)
    $path = FileOpenDialog("Browse", @HomeDrive, "Executables (*.exe;*.com)|Images (*.jpg;*.png;*.bmp;*.gif)")
    GUISetState(@SW_SHOW)
    If $path <> "" Then GUICtrlSetData($input, $path)
    GUICtrlSetState($input, $GUI_FOCUS)
EndFunc   ;==>_Browse

Func _PreExit()
    If $shlwapi <> -1 Then DllClose($shlwapi)
    If $ole32 <> -1 Then
        DllCall($ole32, "none", "CoUninitialize")
        DllClose($ole32)
    EndIf
EndFunc   ;==>_PreExit

Func _FadeIn()
    If $fadein = "1" Then
        $fromTo = StringSplit($inft, "-")
        If $fromTo[0] = 2 Then
            $from = $fromTo[1]
            $to = $fromTo[2]
            For $i = $from To $to Step $instep
                WinSetTrans($GUI, "", $i)
                Sleep(1)
            Next
        EndIf
    Else
        WinSetTrans($GUI, "", 255)
    EndIf
EndFunc   ;==>_FadeIn

Func _FadeOut()
    If $fadeout = "1" Then
        $fromTo = StringSplit($outft, "-")
        If $fromTo[0] = 2 Then
            $from = $fromTo[1]
            $to = $fromTo[2]
            For $i = $from To $to Step $outstep
                WinSetTrans($GUI, "", $i)
                Sleep(1)
            Next
        EndIf
    Else
        WinSetTrans($GUI, "", 0)
    EndIf
EndFunc   ;==>_FadeOut

Func _CheckEnv()
    If FileExists("envvars.txt") Then
        FileOpen("envvars.txt", 0)
        $envlist = FileRead("envvars.txt")
        $envlist = StringSplit($envlist, @CRLF)
        For $i = 1 To $envlist[0]
            $path = EnvGet("PATH")
            EnvSet("PATH", $path & ";" & $envlist[$i])
        Next
    EndIf
EndFunc   ;==>_CheckEnv

Func _CheckAlias()
    If $checkalias = 1 Then
        For $i = 1 To $alist[0][0]
            Global $alias = ""
            If $alist[$i][0] = $run Then
                Global $alias = StringFormat("%s", $alist[$i][1])
                ExitLoop
            EndIf
        Next
    EndIf
EndFunc   ;==>_CheckAlias

Func _CheckExt()
    If Not StringInStr($run, ".", 0, -1) And Not StringInStr($run, "\") And Not StringInStr($run, "/") Then
        $first = FileFindFirstFile($run & ".*")
        $next = FileFindNextFile($first)
        If @error <> 1 Then
            Global $run = $next
        EndIf
    EndIf
EndFunc   ;==>_CheckExt

Func _RunThis()
    Global $run = GUICtrlRead($input)
    _CheckEnv()
    _CheckAlias()
    _CheckExt()
    $strleft = StringLeft($run, 1)
    $strleft4 = StringLeft($run, 4)
    $strmid = StringMid($run, 2, 1)
    If $strleft = "`" Then
        $run = StringTrimLeft($run, 1)
        _DOSCmd($run)
    ElseIf $strleft = "@" Then
        _ExecBroam($run)
    ElseIf $strleft = "*" Then
        $run = StringTrimLeft($run, 1)
        $kw = StringLeft($run, 1)
        If $kw = "a" Then
            $run = StringTrimLeft($run, 2)
            $part = StringSplit($run, ",")
            If @error <> 1 And $part[0] > 1 Then
                $w = IniWrite($config, "Aliases", StringStripWS($part[1], 3), StringStripWS($part[2], 3))
                If $w = 1 Then
                    GUIDelete()
                    MsgBox(64, "m-Run", "Alias successfully added as """ & StringStripWS($part[1], 3) & """")
                    _CreateGUI()
                EndIf
            EndIf
        ElseIf $kw = "d" Then
            $run = StringTrimLeft($run, 2)
            $d = IniDelete($config, "Aliases", StringStripWS($run, 3))
            If $d = 1 Then
                GUIDelete()
                MsgBox(64, "m-Run", "Alias """ & StringStripWS($run, 3) & """ successfully deleted.")
                _CreateGUI()
            EndIf
        ElseIf $kw = "x" Then
            $proc = StringTrimLeft($run, 2)
            If ProcessExists($proc) Then
                ProcessClose($proc)
            Else
                GUIDelete()
                MsgBox(16, "m-Run", "ERROR: Specified process, """ & $proc & """ does not exist!")
            EndIf
        ElseIf $kw = "." Then
            _RunAll(StringRight($run, 3), @ScriptDir & "\")
        EndIf
    ElseIf $strleft = "?" Then
        If StringMid($run, "2", "1") <> "?" Then
            $term = StringTrimLeft($run, 1)
            $term = StringReplace($term, " ", "+")
            $term = StringReplace($term, ",", "%2C")
            $term = "http://www.google.com/search?hl=en&q=" & $term
            _DependRun($term)
        ElseIf StringLeft($run, 2) = "??" Then
            $term = StringTrimLeft($run, 2)
            $term = StringReplace($term, " ", "%20")
            $term = StringReplace($term, ",", "%2C")
            $term = "http://en.wikipedia.org/wiki/Special:Search?search=" & $term
            _DependRun($term)
        EndIf
    ElseIf $alias <> "" Then
        If StringLeft($alias, 1) = "@" Then
            _ExecBroam($alias)
        Else
            _DependRun($alias)
        EndIf
    ElseIf StringInStr($run, "*.", 0, -1) <> 0 Then
        $ext = StringTrimLeft(StringRight($run, StringLen($run) - StringInStr($run, "*.", 0, -1)), 1)
        $dir = StringLeft($run, StringLen($run) - StringLen($ext) - 2)
        _RunAll($ext, $dir)
    Else
        $run = StringFormat("%s", $run)
        _DependRun($run)
    EndIf
EndFunc   ;==>_RunThis

Func _RunAll($ext, $dir)
    Local $flist
    $f = FileFindFirstFile($dir & "*." & $ext)
    ;MsgBox(0, "", $dir & "*." & $ext)
    If $f = -1 Then
        GUIDelete()
        MsgBox(16, "m-Run", "No files found.")
        _CreateGUI()
    EndIf
    
    While 1
        $nf = FileFindNextFile($f)
        If @error Then 
            ExitLoop
        Else
            $flist &= $nf & @CRLF
        EndIf
    WEnd
    FileClose($f)
    GUIDelete()
    $prompt = MsgBox(36, "m-Run", "Really execute the following?" & @CRLF & $flist)
    If $prompt = 6 Then
        $file = StringSplit($flist, @CRLF)
        For $i = 1 To UBound($file) - 2 Step 2
            _DependRun($dir & $file[$i])
        Next
    EndIf
EndFunc   ;==>_RunAll

Func _DependRun($x)
    If @OSTYPE = "WIN32_WINDOWS" Then
        $go = _StartRun98($x)
        ; If $go = 1 Then _ManageHist($run)
    Else
        $go = _StartRun($x)
        If $go <> 1 Then
            _FadeOut()
            MsgBox(0, "Error", $go)
            Exit
        Else
            _WriteHist("mrun.hist", $run)
        EndIf
    EndIf
EndFunc   ;==>_DependRun

Func _DOSCmd($x)
    Run(@ComSpec & " /c " & $x, "", @SW_HIDE)
EndFunc   ;==>_DOSCmd

Func _ExecBroam($y)
    $bleh = DllCall("broams.dll", "int:cdecl", "SendBroam", "str", $y)
    If $bleh[0] = 0 Then 
        GUIDelete()
        $prompt = MsgBox(20, "Error", "bbKontroller-proxy is required to run bro@ms! Go to download page now?")
        If $prompt = 6 Then
            _DependRun("http://nicht.s8.xrea.com/2004/04/04/BBKontroller")
        EndIf
        _CreateGUI()
    Else
        _ManageHist($y)
    EndIf
EndFunc   ;==>_ExecBroam

; --- Call Functions ---
$ole32 = DllOpen("ole32.dll")
$shlwapi = DllOpen("shlwapi.dll")

If $ole32 <> -1 Then DllCall($ole32, "int", "CoInitialize", "ptr", 0)
_CheckConfig()
_CreateGUI()

Thanks in advance.

Edited by Jettison
[font="Impact"]Cats rule, humans drool.[/font]

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
×
×
  • Create New...