[R] Evènement "onChange" avec _GUICtrlComboBoxEx

Aide sur les Interfaces Graphique Utilisateurs (GUI).
Règles du forum
.
Répondre
CleM71
Niveau 2
Niveau 2
Messages : 24
Enregistré le : mer. 16 avr. 2008 08:58
Status : Hors ligne

[R] Evènement "onChange" avec _GUICtrlComboBoxEx

#1

Message par CleM71 »

Bonjour,

J'utilise un objet de type Combo box créé avec

Code : Tout sélectionner

_GUICtrlComboBoxEx_Create()
Son contenu est généré dynmiquement avec

Code : Tout sélectionner

_GUICtrlComboBoxEx_AddString()
Je souhaiterais lancer une fonction lorsque l'utilisateur sélectionne un élément. Lorsque j'utilisais le contrôle par défaut (GUICtrlCreateCombo()), je fais passais par la boucle

Code : Tout sélectionner

While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $MonCombo
MaFonction()
...
...
...
 
Avec le passage en UDF, je suis obligé de passer par la fonction WM_NOTIFY :

Code : Tout sélectionner

Func WM_NOTIFY($hWnd, $Msg, $wParam, $lParam)

    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR

    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    
    Switch $hWndFrom
        Case $hTreeStartMenus
            Switch $iCode
                Case $NM_CLICK
                    ;$tagTVHITTESTINFO = "int X;int Y;int Flags;int Item"
                    Local $tMPos = _WinAPI_GetMousePos(True, $hWndFrom), $tHit = _GUICtrlTreeView_HitTestEx($hWndFrom, DllStructGetData($tMPos, 1), DllStructGetData($tMPos, 2))
                    Local $hItem = DllStructGetData($tHit, "Item"), $iFlags = DllStructGetData($tHit, "Flags")
                    If $hItem <> 0 And BitAND($iFlags, $TVHT_ONITEMSTATEICON) Then
                        _TV_Checkbox_MultiSet($hWndFrom, $hItem, 1)
                    EndIf
                Case $TVN_KEYDOWN
                    Local $tNMTVKEY = DllStructCreate("hwnd;int;int;short key;uint", $lParam)
                    Local $hSelected = _GUICtrlTreeView_GetSelection($hWndFrom)
                    If DllStructGetData($tNMTVKEY, "key") = 0x20 And $hSelected Then ;;space
                        _TV_Checkbox_MultiSet($hWndFrom, $hSelected, 1)
                    EndIf
                Case Else
            EndSwitch
        Case $MonCombo
            Switch $iCode
                Case ??????????????????????
                                       MaFonction()
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc
Le problème c'est que je ne trouve pas quelle variable iCode correspond à la sélection d'un élément, j'ai essayé BEGINUPDATE, ENDUPDATE, GETDISPINFO, aucune ne correspond à ce que je recherche. Les deux premières répondent quand j'ouvre le Combo pour voir son contenu, donc avant même qu'un élément ait été sélectionné.
(Voir http://msdn.microsoft.com/fr-fr/library ... S.71).aspx)

Peut-être y a-t-il une alternative à ce WM_NOTIFY ?

Merci pour vos tuyaux

A+
Modifié en dernier par CleM71 le mar. 03 juin 2008 11:22, modifié 1 fois.
Avatar du membre
timmalos
Niveau 11
Niveau 11
Messages : 1970
Enregistré le : dim. 18 mai 2008 15:16
Status : Hors ligne

Re: [..] Evènement "onChange" avec _GUICtrlComboBoxEx

#2

Message par timmalos »

Code : Tout sélectionner

$index_list_box =_GUICtrlListBox_GetCurSel($liste)
permet de savoir quelle ligne l'utilisateur a sélectionnée
Si aucune ligne n'est cochée:
If $index_list_box = -1 Then MsgBox(16, "No line selected", "You haven t selected any line ...")
Si une ligne est coché tu change le -1 en le nom de la ligne puis tu lance ta fonction:

Code : Tout sélectionner

If $index_list_box = contenu1 Then fonction()
ca devrait correspondre a ce que tu voulais.
CleM71
Niveau 2
Niveau 2
Messages : 24
Enregistré le : mer. 16 avr. 2008 08:58
Status : Hors ligne

Re: [..] Evènement "onChange" avec _GUICtrlComboBoxEx

#3

Message par CleM71 »

Merci, c'était une possibilité que de faire une boucle permanente vérifiant le changement d'un élément, mais peut être pas très gentil pour le processeur :)

J'ai résolu le problème en utilisant une variable booléenne de changement de nom dans le Combo

Code : Tout sélectionner


#include <GuiConstantsEx.au3>
#include <GuiComboBox.au3>

Opt("GUIOnEventMode", 1)

Global $ComboBox_Changed = False
Global Const $DebugIt = 1

Global Const $WM_COMMAND = 0x0111

$hGUI = GUICreate("My GUI combo")  ; Will create a dialog box that when displayed is centered
GUISetOnEvent($GUI_EVENT_CLOSE, "_Terminate")

$hCombo = _GUICtrlComboBox_Create($hGUI, "item1|item2|item3", 10, 10) ; Create Combo items

; Set a default item
_GUICtrlComboBox_SelectString ($hCombo, "item2")

$btn = GUICtrlCreateButton("Ok", 40, 75, 60, 20)
GUICtrlSetOnEvent(-1, "OKPressed")

GUISetState()
GUIRegisterMsg($WM_COMMAND, "MY_WM_COMMAND")

; Just idle around
While 1
    Sleep(10)
   
    If $ComboBox_Changed Then
        $ComboBox_Changed = False
       
        _Combo_Changed()
    EndIf
WEnd

Func OKPressed()
    MsgBox(0, "OK Pressed", "ID=" & @GUI_CtrlId & " WinHandle=" & @GUI_WinHandle & " CtrlHandle=" & @GUI_CtrlHandle)
EndFunc   ;==>OKPressed

Func _Terminate()
    Exit
EndFunc   ;==>_Terminate

Func _Combo_GotFocus()
    ;----------------------------------------------------------------------------------------------
    If $DebugIt Then _DebugPrint("_Combo_GotFocus")
    ;----------------------------------------------------------------------------------------------
EndFunc   ;==>_Combo_GotFocus

Func _Combo_LostFocus()
    ;----------------------------------------------------------------------------------------------
    If $DebugIt Then _DebugPrint("_Combo_LostFocus")
    ;----------------------------------------------------------------------------------------------
EndFunc   ;==>_Combo_LostFocus

Func _Combo_Changed()
    ;----------------------------------------------------------------------------------------------
    If $DebugIt Then _DebugPrint("Combo Changed: " & _GUICtrlComboBox_GetEditText ($hCombo))
    ;----------------------------------------------------------------------------------------------
EndFunc   ;==>_Combo_Changed

Func MY_WM_COMMAND($hWnd, $msg, $wParam, $lParam)
    Local $nNotifyCode = _HiWord($wParam)
    Local $hID = _LoWord($wParam)
    Local $hCtrl = $lParam

    Switch $hCtrl
        Case $hCombo
            Switch $nNotifyCode
                Case $CBN_EDITUPDATE, $CBN_EDITCHANGE ; when user types in new data
                    ; _Combo_Changed()
                    $ComboBox_Changed = True
                Case $CBN_SELCHANGE ; item from drop down selected
                    ;_Combo_Changed()
                    $ComboBox_Changed = True
                Case $CBN_KILLFOCUS
                    _Combo_LostFocus()
                Case $CBN_SETFOCUS
                    _Combo_GotFocus()
            EndSwitch
    EndSwitch
    ; Proceed the default Autoit3 internal message commands.
    ; You also can complete let the line out.
    ; !!! But only 'Return' (without any value) will not proceed
    ; the default Autoit3-message in the future !!!
    Return $GUI_RUNDEFMSG
EndFunc   ;==>MY_WM_COMMAND

Func _DebugPrint($s_Text, $line = @ScriptLineNumber)
    ConsoleWrite( _
            "!===========================================================" & @LF & _
            "+======================================================" & @LF & _
            "-->Line(" & StringFormat("%04d", $line) & "):" & @TAB & $s_Text & @LF & _
            "+======================================================" & @LF)
EndFunc   ;==>_DebugPrint

Func _HiWord($x)
    Return BitShift($x, 16)
EndFunc   ;==>_HiWord

Func _LoWord($x)
    Return BitAND($x, 0xFFFF)
EndFunc   ;==>_LoWord
Répondre