Jump to content

a little help with GUIRegisterMsg


Skysnake
 Share

Recommended Posts

I have read the Tutorial. Used the Help File and used an example script by @Melba23.  I am still struggling.

To use GUIRegisterMsg one needs the following

  • a control in the GUI
  • a GUIRegisterMsg instruction in the GUI to link the control to the WM_COMMAND function
  • a Case statement in the Switch loop to run it
  • a WM_COMMAND function
  • and a processor function...

Mine looks like this

;~  ; QuickSearch combo box
    $g_cbQwkSrch = GUICtrlCreateCombo("", 8, 26, 180, 17) ; start blank
    GUICtrlSetTip($g_cbQwkSrch, "Type to search", "QuickSearch")


    GUIRegisterMsg($WM_COMMAND, "__WM_COMMAND_QWKSEARCH_CMB")



;------------------------------------

; use in Switch Loop

;~              ; QuickSearch
            Case $g_cbQwkSrch
                ConsoleWrite("221 Case $g_cbQwkSrch" & @CRLF)



;--------------------------------------

Func _QwkSrch_Edit_Changed()
    ConsoleWrite("41 _QwkSrch_Edit_Changed" & @CRLF)

    ; Autocomplete the edit
    _GUICtrlComboBox_AutoComplete($g_cbQwkSrch)
    ; Change the label to match the autocompleted edit entry

EndFunc   ;==>_QwkSrch_Edit_Changed

Func __WM_COMMAND_QWKSEARCH_CMB($hWnd, $iMsg, $wParam, $lParam) ; GUIRegisterMsg($WM_COMMAND, "__WM_COMMAND_QWKSEARCH_CMB")
    ConsoleWrite("50 _WM_COMMAND_QWKSEARCH_CMB fired " & @CRLF)
    Local $Found = ''

    #forceref $hWnd, $iMsg
    If $lParam = GUICtrlGetHandle($g_cbQwkSrch) And BitShift($wParam, 16) = $CBN_EDITCHANGE Then ; Our combo edit content has changed

; get data from source ----------------------------
Local $datafromsource = fFindFilesForQuickSearch($stringtofind)

; datafromsource convert to Combo friendly format

                    ; Add data to combo
                    _GUICtrlComboBox_BeginUpdate($g_cbQwkSrch)
                    GUICtrlSetData($g_cbQwkSrch, $Found)
                    _GUICtrlComboBox_EndUpdate($g_cbQwkSrch)


        EndIf

        _QwkSrch_Edit_Changed() ; Action this function

    EndIf

EndFunc   ;==>_WM_COMMAND_QWKSEARCH_CMB

Func fFindFilesForQuickSearch($stringtofind)
    ; helper function for QuickSearch

; get data here

    Return $aResult

EndFunc   ;==>fFindFilesForQuickSearch

Now the following:

  1. Can I declare the variable $g_cbQwkSrch in Global, then use in multiple functions - reassigning different combo's to it as I go along?
  2. Should GUIRegisterMsg be expressly de-registered, or does that happen automatically at GuiDelete()?
  3. Where is the best/correct place to call GUIRegisterMsg? With the creation of its control, before the GUISetState, or before the start of the loop?
  4. The Help File contains this entry, what is the intent?  Must this always be called with four args, or a max of four...   !!! To make the user function workable you have to define it with maximum 4 function parameters otherwise the function won't be called !!!
Edited by Skysnake

Skysnake

Why is the snake in the sky?

Link to comment
Share on other sites

  • Moderators

Skysnake,

1. This is how I would deal with multiple combos:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiComboBox.au3>
#include <File.au3>

; A flag to prevent the autocomplete from continually firing
Global $bAutoComplete = False

; Your AutoIt folder
Global $sRootFolder = StringLeft(@AutoItExe, StringInStr(@AutoItExe, "\", Default, -1))
ConsoleWrite($sRootFolder & @CRLF)

; Create GUI
$hGUI = GUICreate("Test", 500, 500)

GUICtrlCreateLabel("Include files", 10, 30, 200, 20)
$cCombo_1 = GUICtrlCreateCombo("", 10, 50, 200, 20)
GUICtrlCreateLabel("SciTE Property files", 10, 80, 200, 20)
$cCombo_2 = GUICtrlCreateCombo("", 10, 100, 200, 20)
; Create a dummy control which fires when a combo is edited
$cComboChange_Dummy = GUICtrlCreateDummy()

GUISetState()

; Register the mesasge handler
GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $cComboChange_Dummy
            ; A Combo was changed - read the dummy to find out which one
            $cComboChanged = GUICtrlRead($cComboChange_Dummy)
            ; And then run the function with the correct combo ControlID
            _Combo_Changed($cComboChanged)
    EndSwitch

WEnd

Func _WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)

    ; Check it is a cambo edit change messgae and that it is not an autocomplete change
    If BitShift($wParam, 16) = $CBN_EDITCHANGE And $bAutoComplete = False Then
        ; Check which combo sent the message
        Switch $lParam
            Case GUICtrlGetHandle($cCombo_1)
                ; Send the combo ControlID to the dummy so we can read it later
                GUICtrlSendToDummy($cComboChange_Dummy, $cCombo_1)
                ; Set the flag so that the autocomplete does not fire the handler
                $bAutoComplete = True
            Case GUICtrlGetHandle($cCombo_2)
                GUICtrlSendToDummy($cComboChange_Dummy, $cCombo_2)
                $bAutoComplete = True
        EndSwitch
    EndIf

EndFunc

Func _Combo_Changed($cCombo)

    Switch $cCombo
        Case $cCombo_1
            ; Set the required path
            $sPath = $sRootFolder & "Include"
            ; Get the files which match
            $aList = _FileListToArray($sPath, "*.au3")
        Case $cCombo_2
            $sPath = $sRootFolder & "SciTE"
            $aList = _FileListToArray($sPath, "*.properties")
    EndSwitch

    ; Convert the list to required format
    $sComboData = ""
    For $i = 1 To $aList[0]
        $sComboData &= "|" & $aList[$i]
    Next

    ; Read the current content of the edit
    $sCurrContent = GUICtrlRead($cCombo)
    ; Load the file list into the combo
    GUICtrlSetData($cCombo,$sComboData)
    ; Reload the edit content as it has been cleared by the combo data load
    ControlSend($hGUI, "", $cCombo, $sCurrContent)
    ; Do the sutocomplete
    _GUICtrlComboBox_AutoComplete($cCombo)
    ; Clea the flag so new user input will allow the handler to fire
    $bAutoComplete = False

EndFunc

2. There is no need to specifically deregister messages on exit - AutoIt does this for you

3. I usually register messages just before entering the idle loop - although you can do it earlier and in some cases it is necessary to do so.

4. The Help file is correct - there is a maximum of 4 parameters. If you are not going to use parameters then you can omit them - as long as there are no subsequent parameters required:

Func _WM_COMMAND($hWnd, $iMsg)                  ; Correct syntax

Func _WM_COMMAND($hWnd, $iMsg, $lParam)         ; Incorrect syntax - $lParam will actually hold $wParam value

All clear?

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
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
 Share

×
×
  • Create New...