Fokus setzen nach Auswahl in Combo Box

  • Hi,

    ich komme momentan nicht weiter! Ich möchte den Fokus nach einer Auswahl aus der Combo Box auf das Set Button setzen, da sonst die Farbe, die man in der Combo Box ausgewählt hat, durch den Fokus auf der Combo Box falsch dargestellt wird, z.B. das hell gelbe wird grün dargestellt, wenn der Fokus auf der Combo Box ist.

    Spoiler anzeigen
    [autoit]


    #include <Array.au3>
    #include <ComboConstants.au3>
    #include <Constants.au3>
    #include <EditConstants.au3>
    #include <GuiComboBoxEx.au3>
    #include <GUIConstantsEx.au3>
    #include <GuiImageList.au3>
    #include <Misc.au3>
    #include <SliderConstants.au3>
    #include <StructureConstants.au3>
    #include <WinAPI.au3>
    #include <WindowsConstants.au3>
    Global $hGUI = GUICreate("")
    GUISetState()
    Global $load_auto = ""
    Global $load_auto, $GUI_Bg_Color = 0xB9D1EA, $ping_to = 500
    Global Const $ini_file = @ScriptDir & "\Check Online Status.ini"

    [/autoit] [autoit][/autoit] [autoit]

    Global $hComboColor, $hGUI_Settings, $hProc, $hHook
    Settings()

    [/autoit] [autoit][/autoit] [autoit]

    Exit

    [/autoit] [autoit][/autoit] [autoit]

    Func Settings()
    GUISetState(@SW_DISABLE, $hGUI)
    $hGUI_Settings = GUICreate("Settings", 612, 229, -1, -1, $GUI_SS_DEFAULT_GUI, Default, $hGUI)
    Local Const $iAutoLoadFile = GUICtrlCreateInput($load_auto, 152, 16, 369, 22)
    GUICtrlSetFont(-1, 9, 400, 0, "Arial")
    Local Const $lLoadAuto = GUICtrlCreateLabel("Load List Automatically", 8, 18, 140, 20)
    GUICtrlSetFont(-1, 10, 400, 0, "Arial")
    GUICtrlSetTip(-1, "Select a file which will be loaded to the program automatically")
    Local Const $bBrowse = GUICtrlCreateButton("&Browse", 528, 14, 75, 25)
    GUICtrlSetFont(-1, 10, 400, 0, "Arial")
    Local Const $lBgColor = GUICtrlCreateLabel("Change GUI background color", 6, 58, 177, 20)
    GUICtrlSetFont(-1, 10, 400, 0, "Arial")
    ;~ Local Const $lBgColorAdd = GUICtrlCreateLabel("Add new color", 6, 97, 97, 20)
    ;~ GUICtrlSetFont(-1, 10, 400, 0, "Arial")
    ;~ Local Const $bAddColor = GUICtrlCreateButton("Add &Color", 105, 94, 75, 25)
    ;~ GUICtrlSetFont(-1, 10, 400, 0, "Arial")
    $hComboColor = _GUICtrlComboBoxEx_Create($hGUI_Settings, "", 192, 56, 329, 200, BitOR($CBS_DROPDOWNLIST, $CBS_DISABLENOSCROLL))
    ;~ $hComboColor = GUICtrlCreateCombo("", 192, 56, 329, 200, BitOR($CBS_DROPDOWNLIST, $CBS_DISABLENOSCROLL))
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $hComboColor = ' & $hComboColor & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
    GUICtrlSetData(-1, "item2|item3", "item3")
    GUICtrlSetFont(-1, 10, 400, 0, "Arial")
    Local Const $bSet = GUICtrlCreateButton("&Set", 528, 55, 75, 64)
    GUICtrlSetFont(-1, 10, 400, 0, "Arial")
    Local Const $bApply = GUICtrlCreateButton("&Apply", 528, 180, 75, 40)
    GUICtrlSetFont(-1, 10, 400, 0, "Arial")
    Local Const $bExit_Setting = GUICtrlCreateButton("E&xit", 438, 180, 75, 40)
    GUICtrlSetFont(-1, 10, 400, 0, "Arial")
    Local Const $cSlider = GUICtrlCreateSlider(88, 138, 440, 30, BitOR($TBS_BOTH,$TBS_NOTICKS, $TBS_ENABLESELRANGE, $TBS_FIXEDLENGTH, $WS_TABSTOP))
    GUICtrlSetLimit(-1, 5000, 100)
    GUICtrlSetData(-1, 500)
    GUICtrlSetCursor (-1, 0)
    Local Const $lSlider = GUICtrlCreateLabel("Ping Timeout", 8, 141, 80, 20)
    GUICtrlSetFont(-1, 10, 400, 0, "Arial")
    GUICtrlSetTip(-1, "Default is 500 ms")
    Local Const $iPing = GUICtrlCreateInput($ping_to, 528, 140, 74, 21, BitOR($ES_CENTER, $ES_NUMBER))
    GUICtrlSetFont(-1, 10, 400, 0, "Arial")

    [/autoit] [autoit][/autoit] [autoit]

    GUISetState(@SW_SHOW, $hGUI_Settings)
    ControlFocus($hGUI_Settings, "", $bExit_Setting)
    GUISetIcon(@SystemDir & "\Shell32.dll", 14, $hGUI_Settings)
    Local $nMsg, $i, $file, $cindex, $em, $p, $newcolor

    [/autoit] [autoit][/autoit] [autoit]

    Local $aColors[7] = [0xB9D1EA, 0xC0DCC0, 0xD7E4F2, 0xFFFFE1, 0xA6CAF0, 0xFFFBF0, 0xFECFF4]

    [/autoit] [autoit][/autoit] [autoit]

    Local Const $hImage = _GUIImageList_Create(256, 56, 5, 3)
    For $i = 0 To 6
    _GUIImageList_Add($hImage, _GUICtrlComboBoxEx_CreateSolidBitMap($hComboColor, $aColors[$i], 256, 56))
    Next
    _GUICtrlComboBoxEx_SetImageList($hComboColor, $hImage)

    [/autoit] [autoit][/autoit] [autoit]

    _GUICtrlComboBoxEx_BeginUpdate($hComboColor)
    For $i = 0 To 6
    _GUICtrlComboBoxEx_AddString($hComboColor, "Color " & $i + 1, $i, $i)
    Next
    _GUICtrlComboBoxEx_EndUpdate($hComboColor)

    [/autoit] [autoit][/autoit] [autoit]

    $cindex = _ArraySearch($aColors, $GUI_Bg_Color)
    If Not @error Then
    _GUICtrlComboBoxEx_SetCurSel($hComboColor, $cindex)
    Else
    _GUICtrlComboBoxEx_SetCurSel($hComboColor, 0)
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

    [/autoit] [autoit][/autoit] [autoit]

    While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $GUI_EVENT_CLOSE, $bExit_Setting
    GUISetState(@SW_ENABLE, $hGUI)
    GUIDelete($hGUI_Settings)
    Return 1
    Case $cSlider
    GUICtrlSetData($iPing, GUICtrlRead($cSlider))
    Case $iPing
    $p = GUICtrlRead($iPing)
    If $p > 5000 Then
    $p = 500
    ElseIf $p < 100 Then
    $p = 100
    EndIf
    GUICtrlSetData($cSlider, $p)
    Case $bBrowse
    $file = FileOpenDialog("File Selection", "", "Text Files (*.txt;*.csv)")
    If Not @error Then GUICtrlSetData($iAutoLoadFile, $file)
    Case $bSet
    $GUI_Bg_Color = "0x" & Hex($aColors[_GUICtrlComboBoxEx_GetCurSel($hComboColor)], 6)
    GUISetBkColor($GUI_Bg_Color, $hGUI)
    Case $bApply
    $GUI_Bg_Color = "0x" & Hex($aColors[_GUICtrlComboBoxEx_GetCurSel($hComboColor)], 6)
    GUISetBkColor($GUI_Bg_Color, $hGUI)
    IniWrite($ini_file, "Bg-Color", "Color", "0x" & Hex($aColors[_GUICtrlComboBoxEx_GetCurSel($hComboColor)], 6))
    $em = @error
    IniWrite($ini_file, "Auto-Load", "Load", GUICtrlRead($iAutoLoadFile))
    $em = @error
    IniWrite($ini_file, "Ping-Timeout", "Ping", GUICtrlRead($iPing))
    $em = @error
    If $em Then
    MsgBox(16, "Error", "Unable to save one or more settings to ini file!", 10)
    Else
    MsgBox(64, "Information", "Settings were save to " & $ini_file & " successfully", 10)
    EndIf
    ;~ Case $bAddColor
    ;~ $newcolor = _ChooseColor(2, 0, 2, $hGUI_Settings)
    ;~ If Not @error Then
    ;~ ConsoleWrite($newcolor & @CRLF)
    ;~ EndIf
    EndSwitch
    WEnd
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func WM_NOTIFY($hWnd, $iMsg, $wParam, $iParam)
    ;~ ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $wParam = ' & $iParam & "/" & Hex(BitAND($iParam, $hComboColor)) & "," & $hComboColor & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR

    [/autoit] [autoit][/autoit] [autoit]

    $tNMHDR = DllStructCreate($tagNMHDR, $iParam)
    $hWndFrom = DllStructGetData($tNMHDR, "hWndFrom")
    ;~ ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $hWndFrom = ' & $hWndFrom & "/" & $hComboColor & "/" & $hGUI_Settings & "," & $hGUI & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")

    [/autoit] [autoit][/autoit] [autoit]

    Switch $hWndFrom
    Case $hComboColor
    Switch $iCode
    Case $CBEN_ENDEDITA, $CBEN_ENDEDITW ; Sent when the user has concluded an operation within the edit box or has selected an item from the control's drop-down list.
    ConsoleWrite("huhu")
    Return False ; accept the notification and allow the control to display the selected item
    Case Else
    ;~ ConsoleWrite(Hex($iCode) & "/ " & Hex($CBEN_ENDEDITA) & "," & Hex($CBEN_ENDEDITW) & @CRLF)
    EndSwitch

    [/autoit] [autoit][/autoit] [autoit]

    EndSwitch
    Return "GUI_RUNDEFMSG"
    EndFunc ;==>WM_NOTIFY

    [/autoit]

    BugFix hat mir einige Beispiele geschickt, um das Ganze mit DllCallbackRegisters (Hooking) zu lösen, nur sehe ich nicht den Vorteil gegenüber der "normalen" Variante mit GUIRegisterMsg(), was doch auch eigentlich ein DllCallbackRegister ist, oder? Abgesehen davon, habe ich auch keinen Erfolg mit DCR!

    Danke und Gruß,
    UEZ

    Auch am Arsch geht ein Weg vorbei...

    ¯\_(ツ)_/¯

    Einmal editiert, zuletzt von UEZ (14. Juni 2011 um 20:22)

    • Offizieller Beitrag

    Hi UEZ,
    das Hauptproblem ist eigentlich, dass es keinen definierten Zustand für das Ende der Farbwahl gibt (und somit den Zeitpunkt zum Wechsel des Fokus).
    Du hast mehrere Möglichkeiten die Auswahl zu treffen:
    1. Klick auf Combo-Button zum Ausklappen der Auswahlliste
    - a) mit Mausklick Farbeintrag wählen
    - b) mit Pfeiltasten auf/ab bewegen und mit Enter übernehmen
    2. Fokus auf Combo ohne dass die LIste ausgeklappt ist
    - a) mit Pfeiltasten ändert sich der Eintrag im sichtbaren Bereich - Übernahme mit Enter
    - b) mit Scrollen des Mausrades ändert sich der Eintrag im sichtbaren Bereich - Übernahme mit Enter

    Die einzig klare Form der Wahl ist: Ausklappen, mit Mausklick wählen.
    Aber der Nutzer hat ja durchaus die Möglichkeit anders vorzugehen. Deshalb denke ich, dass das Standard Combo-Ctrl hier nicht geeignet ist. Ich würde hier ein eigenes Ctrl erstellen, dem du nur die gewollten Eigenschaften zuordnen kannst.
    Unabhängig davon, werde ich mich mal an der Variante: "Ausklappen, mit Mausklick wählen, Fokus auf Button" probieren.

    Edit:
    Du hattest "WM_NOTIFY" statt "WM_COMMAND" ausgewertet.
    So geht es per Auswahl mit Mausklick:

    Spoiler anzeigen
    [autoit]

    #include <Array.au3>
    #include <ComboConstants.au3>
    #include <Constants.au3>
    #include <EditConstants.au3>
    #include <GuiComboBoxEx.au3>
    #include <GUIConstantsEx.au3>
    #include <GuiImageList.au3>
    #include <Misc.au3>
    #include <SliderConstants.au3>
    #include <StructureConstants.au3>
    #include <WinAPI.au3>
    #include <WindowsConstants.au3>

    [/autoit] [autoit][/autoit] [autoit]

    Global $hGUI = GUICreate("")
    GUISetState()
    Global $load_auto = ""
    Global $load_auto, $GUI_Bg_Color = 0xB9D1EA, $ping_to = 500
    Global Const $ini_file = @ScriptDir & "\Check Online Status.ini"

    [/autoit] [autoit][/autoit] [autoit]

    Global $bSet, $hComboColor, $hGUI_Settings, $hProc, $hHook
    Settings()

    [/autoit] [autoit][/autoit] [autoit]

    Exit

    [/autoit] [autoit][/autoit] [autoit]

    Func Settings()
    GUISetState(@SW_DISABLE, $hGUI)
    $hGUI_Settings = GUICreate("Settings", 612, 229, -1, -1, $GUI_SS_DEFAULT_GUI, Default, $hGUI)
    Local Const $iAutoLoadFile = GUICtrlCreateInput($load_auto, 152, 16, 369, 22)
    GUICtrlSetFont(-1, 9, 400, 0, "Arial")
    Local Const $lLoadAuto = GUICtrlCreateLabel("Load List Automatically", 8, 18, 140, 20)
    GUICtrlSetFont(-1, 10, 400, 0, "Arial")
    GUICtrlSetTip(-1, "Select a file which will be loaded to the program automatically")
    Local Const $bBrowse = GUICtrlCreateButton("&Browse", 528, 14, 75, 25)
    GUICtrlSetFont(-1, 10, 400, 0, "Arial")
    Local Const $lBgColor = GUICtrlCreateLabel("Change GUI background color", 6, 58, 177, 20)
    GUICtrlSetFont(-1, 10, 400, 0, "Arial")
    ;~ Local Const $lBgColorAdd = GUICtrlCreateLabel("Add new color", 6, 97, 97, 20)
    ;~ GUICtrlSetFont(-1, 10, 400, 0, "Arial")
    ;~ Local Const $bAddColor = GUICtrlCreateButton("Add &Color", 105, 94, 75, 25)
    ;~ GUICtrlSetFont(-1, 10, 400, 0, "Arial")
    $hComboColor = _GUICtrlComboBoxEx_Create($hGUI_Settings, "", 192, 56, 329, 200, BitOR($CBS_DROPDOWNLIST, $CBS_DISABLENOSCROLL))
    ;~ $hComboColor = GUICtrlCreateCombo("", 192, 56, 329, 200, BitOR($CBS_DROPDOWNLIST, $CBS_DISABLENOSCROLL))
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $hComboColor = ' & $hComboColor & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
    GUICtrlSetData(-1, "item2|item3", "item3")
    GUICtrlSetFont(-1, 10, 400, 0, "Arial")
    $bSet = GUICtrlCreateButton("&Set", 528, 55, 75, 64) ;#############################
    GUICtrlSetFont(-1, 10, 400, 0, "Arial")
    Local Const $bApply = GUICtrlCreateButton("&Apply", 528, 180, 75, 40)
    GUICtrlSetFont(-1, 10, 400, 0, "Arial")
    Local Const $bExit_Setting = GUICtrlCreateButton("E&xit", 438, 180, 75, 40)
    GUICtrlSetFont(-1, 10, 400, 0, "Arial")
    Local Const $cSlider = GUICtrlCreateSlider(88, 138, 440, 30, BitOR($TBS_BOTH,$TBS_NOTICKS, $TBS_ENABLESELRANGE, $TBS_FIXEDLENGTH, $WS_TABSTOP))
    GUICtrlSetLimit(-1, 5000, 100)
    GUICtrlSetData(-1, 500)
    GUICtrlSetCursor (-1, 0)
    Local Const $lSlider = GUICtrlCreateLabel("Ping Timeout", 8, 141, 80, 20)
    GUICtrlSetFont(-1, 10, 400, 0, "Arial")
    GUICtrlSetTip(-1, "Default is 500 ms")
    Local Const $iPing = GUICtrlCreateInput($ping_to, 528, 140, 74, 21, BitOR($ES_CENTER, $ES_NUMBER))
    GUICtrlSetFont(-1, 10, 400, 0, "Arial")

    [/autoit] [autoit][/autoit] [autoit]

    GUISetState(@SW_SHOW, $hGUI_Settings)
    ControlFocus($hGUI_Settings, "", $bExit_Setting)
    GUISetIcon(@SystemDir & "\Shell32.dll", 14, $hGUI_Settings)
    Local $nMsg, $i, $file, $cindex, $em, $p, $newcolor

    [/autoit] [autoit][/autoit] [autoit]

    Local $aColors[7] = [0xB9D1EA, 0xC0DCC0, 0xD7E4F2, 0xFFFFE1, 0xA6CAF0, 0xFFFBF0, 0xFECFF4]

    [/autoit] [autoit][/autoit] [autoit]

    Local Const $hImage = _GUIImageList_Create(256, 56, 5, 3)
    For $i = 0 To 6
    _GUIImageList_Add($hImage, _GUICtrlComboBoxEx_CreateSolidBitMap($hComboColor, $aColors[$i], 256, 56))
    Next
    _GUICtrlComboBoxEx_SetImageList($hComboColor, $hImage)

    [/autoit] [autoit][/autoit] [autoit]

    _GUICtrlComboBoxEx_BeginUpdate($hComboColor)
    For $i = 0 To 6
    _GUICtrlComboBoxEx_AddString($hComboColor, "Color " & $i + 1, $i, $i)
    Next
    _GUICtrlComboBoxEx_EndUpdate($hComboColor)

    [/autoit] [autoit][/autoit] [autoit]

    $cindex = _ArraySearch($aColors, $GUI_Bg_Color)
    If Not @error Then
    _GUICtrlComboBoxEx_SetCurSel($hComboColor, $cindex)
    Else
    _GUICtrlComboBoxEx_SetCurSel($hComboColor, 0)
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    ;~ GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
    GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")

    [/autoit] [autoit][/autoit] [autoit]

    While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $GUI_EVENT_CLOSE, $bExit_Setting
    GUISetState(@SW_ENABLE, $hGUI)
    GUIDelete($hGUI_Settings)
    Return 1
    Case $cSlider
    GUICtrlSetData($iPing, GUICtrlRead($cSlider))
    Case $iPing
    $p = GUICtrlRead($iPing)
    If $p > 5000 Then
    $p = 500
    ElseIf $p < 100 Then
    $p = 100
    EndIf
    GUICtrlSetData($cSlider, $p)
    Case $bBrowse
    $file = FileOpenDialog("File Selection", "", "Text Files (*.txt;*.csv)")
    If Not @error Then GUICtrlSetData($iAutoLoadFile, $file)
    Case $bSet
    $GUI_Bg_Color = "0x" & Hex($aColors[_GUICtrlComboBoxEx_GetCurSel($hComboColor)], 6)
    GUISetBkColor($GUI_Bg_Color, $hGUI)
    Case $bApply
    $GUI_Bg_Color = "0x" & Hex($aColors[_GUICtrlComboBoxEx_GetCurSel($hComboColor)], 6)
    GUISetBkColor($GUI_Bg_Color, $hGUI)
    IniWrite($ini_file, "Bg-Color", "Color", "0x" & Hex($aColors[_GUICtrlComboBoxEx_GetCurSel($hComboColor)], 6))
    $em = @error
    IniWrite($ini_file, "Auto-Load", "Load", GUICtrlRead($iAutoLoadFile))
    $em = @error
    IniWrite($ini_file, "Ping-Timeout", "Ping", GUICtrlRead($iPing))
    $em = @error
    If $em Then
    MsgBox(16, "Error", "Unable to save one or more settings to ini file!", 10)
    Else
    MsgBox(64, "Information", "Settings were save to " & $ini_file & " successfully", 10)
    EndIf
    ;~ Case $bAddColor
    ;~ $newcolor = _ChooseColor(2, 0, 2, $hGUI_Settings)
    ;~ If Not @error Then
    ;~ ConsoleWrite($newcolor & @CRLF)
    ;~ EndIf
    EndSwitch
    WEnd
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func WM_COMMAND($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg
    Local $hWndFrom, $iIDFrom, $iCode
    $hWndFrom = $ilParam
    $iIDFrom = BitAND($iwParam, 0xFFFF)
    $iCode = BitShift($iwParam, 16)
    Switch $hWndFrom
    Case $hComboColor
    Switch $iCode
    Case $CBN_SELENDOK
    ControlFocus($hGUI_Settings, '', $bSet)
    EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
    EndFunc ;==>WM_COMMAND

    [/autoit]
  • Vielen Dank BugFix! Das Problem lag an der falschen GUIRegisterMsg Konstante, aber laut der Hilfe unter _GUICtrlComboBoxEx_Create

    Zitat


    Case $CBEN_ENDEDITA, $CBEN_ENDEDITW ; Sent when the user has concluded an operation within the edit box or has selected an item from the control's drop-down list.

    wird $CBEN_ENDEDITA, $CBEN_ENDEDITW benutzt, um das Ende der Selektion einer Combo Box abzufragen und das mit WM_Notify.

    Problem gelöst und vielen Dank für die Beispiele! ;)

    Gruß,
    UEZ

    Auch am Arsch geht ein Weg vorbei...

    ¯\_(ツ)_/¯