Jump to content

This code in not perfect, can some one help me optimize this code


Efo74
 Share

Recommended Posts

Not sure if this optimizes your code but you could also do something like:

#include <GUIConstantsEx.au3>
#include <GuiEdit.au3>
#include <Misc.au3>
#include <StaticConstants.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>

Global $g_hGUI, $g_idControls[2][10], $g_dFocused = 0x0000FF, $g_dNoFocus = 0x666666

Example()

Func Example()
    ;~ ======================== Create Gui ======================== ~;
    ;~ $g_hGUI Width = Left Margin + Control Label Width + Control Space + Control Input Width + Right Margin
    ;~ $g_hGUI Height = Top Margin + Control Height + (Spacer * Control Count) + (Control Height * Control Count) + Bottom Margin
    $g_hGUI = GUICreate("", 10 + 70 + 5 + 70 + 10, 10 + (5 * UBound($g_idControls, 2) - 1) + (20 * UBound($g_idControls, 2) - 1) + 10)
        _GuiCreateControls("Control ", 10)
        GUICtrlSetColor($g_idControls[0][0], $g_dFocused)
        GUICtrlSetState($g_idControls[0][0], $GUI_FOCUS)
    GUISetState(@SW_SHOW)
        GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
    While 1
        $nMsg= GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                ExitLoop
        EndSwitch
        $hControlId = ControlGetHandle($g_hGUI, "", ControlGetFocus($g_hGUI, ""))
        $iControlId = _WinAPI_GetDlgCtrlID ($hControlId)
        Switch $iControlId
            Case $g_idControls[1][0] To $g_idControls[1][UBound($g_idControls, 2) - 1]
                ;~ BackSpace Or Arrow Left Or Arrow Up
                If _IsPressed("08") Or _IsPressed("25") Or _IsPressed("26") Then
                    While _IsPressed("08") Or _IsPressed("25") Or _IsPressed("26")
                        Sleep(10)
                    WEnd
                    $aEditSel = _GUICtrlEdit_GetSel($iControlId)
                    If $aEditSel[0] = 0 And $aEditSel[1] = 0 Then
                        If $iControlId = $g_idControls[1][0] Then
                            GUICtrlSetState($g_idControls[1][UBound($g_idControls, 2) - 1], $GUI_FOCUS)
                            _GUICtrlEdit_SetSel($g_idControls[1][UBound($g_idControls, 2) - 1], -1, - 1)
                        Else
                            GUICtrlSetState($iControlId - 2, $GUI_FOCUS)
                            _GUICtrlEdit_SetSel($iControlId - 2, -1, - 1)
                        EndIf
                    EndIf
                ;~ Arrow Right Or Arrow Down
                ElseIf _IsPressed("27") Or _IsPressed("28") Then
                    While _IsPressed("27") Or _IsPressed("28")
                        Sleep(10)
                    WEnd
                    $aEditSel = _GUICtrlEdit_GetSel($iControlId)
                    If $aEditSel[0] = _GUICtrlEdit_GetTextLen($iControlId) And $aEditSel[1] = _GUICtrlEdit_GetTextLen($iControlId) Then
                        If $iControlId = $g_idControls[1][UBound($g_idControls, 2) - 1] Then
                            GUICtrlSetState($g_idControls[1][0], $GUI_FOCUS)
                            _GUICtrlEdit_SetSel($g_idControls[1][0], -1, - 1)
                        Else
                            GUICtrlSetState($iControlId + 2, $GUI_FOCUS)
                            _GUICtrlEdit_SetSel($iControlId + 2, -1, - 1)
                        EndIf
                    EndIf
                EndIf
        EndSwitch
    WEnd
    GUIDelete()
EndFunc

Func _GuiCreateControls($_vCtrlLabel = "", $_iMaxChars = 10)
    Local $i_xCtrlInput = 85
    Local $i_yCtrlCoord = 10
    For $i = 0 To UBound($g_idControls, 2) - 1
        $g_idControls[0][$i] = GUICtrlCreateLabel($_vCtrlLabel & $i + 1, 10, $i_yCtrlCoord, 70, 20, $SS_CENTERIMAGE)
        GUICtrlSetColor($g_idControls[0][$i], $g_dNoFocus)
        $g_idControls[1][$i] = GUICtrlCreateInput("", $i_xCtrlInput, $i_yCtrlCoord, 70, 20)
        GUICtrlSetLimit($g_idControls[1][$i], $_iMaxChars)
        $i_yCtrlCoord += 25
    Next
EndFunc

Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg
    Local $hWndFrom, $iIDFrom, $iCode, $hWndEdit
    If Not IsHWnd($g_idControls[0][0]) Then $hWndEdit = GUICtrlGetHandle($g_idControls[0][0])
    $hWndFrom = $lParam
    $iIDFrom = _WinAPI_LoWord($wParam)
    $iCode = _WinAPI_HiWord($wParam)
    Switch $iIDFrom
        Case $g_idControls[1][0] To $g_idControls[1][UBound($g_idControls, 2) - 1]
            Switch $iCode
                Case $EN_KILLFOCUS ; Sent when an edit control loses the keyboard focus
                    GUICtrlSetColor($iIDFrom - 1, $g_dNoFocus)
                Case $EN_MAXTEXT ; Sent when the current text insertion has exceeded the specified number of characters for the edit control
                    GUICtrlSetState($iIDFrom + 2, $GUI_FOCUS)
                    _GUICtrlEdit_SetSel($iIDFrom + 2, 0, 0)
                Case $EN_SETFOCUS ; Sent when an edit control receives the keyboard focus
                    GUICtrlSetColor($iIDFrom - 1, $g_dFocused)
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_COMMAND

 

Link to comment
Share on other sites

Efo74,

Per MS Doc for EM_MAXTEXT

Quote

This message indicates that the current text insertion has exceeded the specified number of characters for the edit control. The text insertion has been truncated.

In short, the edit control consumes the character and we have no accessibility to it that I see.

Here is another take on you task...

#include <EditConstants.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <array.au3>
#include <Misc.au3>

#AutoIt3Wrapper_Add_Constants=n

Local $hDLL = DllOpen("user32.dll")

; keep track of which input control has focus
Local $who_has_focus

Local $gui010 = GUICreate('Input Control Tabbing Demo')

Local $ctlInputIDS[6]
For $1 = 0 To 5
    GUICtrlCreateLabel('Input # ' & $1 + 1, 10, $1 * 25 + 10, 100, 20)
    $ctlInputIDS[$1] = GUICtrlCreateInput('', $1 + 70, $1 * 25 + 10, Random(50, 80, 1), 20, $es_left)
Next

GUISetState()

GUIRegisterMsg($WM_COMMAND, 'WM_COMMAND')

While 1
    Switch GUIGetMsg()
        Case $gui_event_close
            Exit
    EndSwitch

    If _IsPressed("08", $hDLL) Then

        While _IsPressed("08", $hDLL)
            Sleep(100)
        WEnd

        ; if the input control is blank then tab backward
        If GUICtrlRead($who_has_focus) = '' Then ControlSend('', '', $who_has_focus, '+{tab}')

    EndIf

WEnd

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

    Switch BitAND($wParam, 0xFFFF)
        Case $ctlInputIDS[0] To $ctlInputIDS[UBound($ctlInputIDS) - 1]
            Switch BitShift($wParam, 16)
                Case $EN_setfocus
                    $who_has_focus = BitAND($wParam, 0xFFFF)
                Case $EN_maxtext
                    ControlSend('', '', BitAND($wParam, 0xffff), '{tab}')
            EndSwitch

    EndSwitch

    Return $GUI_RUNDEFMSG

EndFunc   ;==>WM_COMMAND

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

@kylomas The issue is the OP wants to type for example 0123456789, if he presses 0 it should go to the next line and type 0, I've figured out how to do it by just adding in and _IsPressed capture, the only issue _IsPressed only looks at keys rather than actual characters pressed so if you type "a" it will add "A".

Here is the code:

#include <GUIConstantsEx.au3>
#include <GuiEdit.au3>
#include <Misc.au3>
#include <StaticConstants.au3>
#include <String.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>

Global $g_hGUI, $g_idControls[2][10], $g_dFocused = 0x0000FF, $g_dNoFocus = 0x666666

Example()

Func Example()
    ;~ ======================== Create Gui ======================== ~;
    ;~ $g_hGUI Width = Left Margin + Control Label Width + Control Space + Control Input Width + Right Margin
    ;~ $g_hGUI Height = Top Margin + Control Height + (Spacer * Control Count) + (Control Height * Control Count) + Bottom Margin
    $g_hGUI = GUICreate("", 10 + 70 + 5 + 70 + 10, 10 + (5 * UBound($g_idControls, 2) - 1) + (20 * UBound($g_idControls, 2) - 1) + 10)
        _GuiCreateControls("Control ", 10)
        GUICtrlSetColor($g_idControls[0][0], $g_dFocused)
        GUICtrlSetState($g_idControls[1][0], $GUI_FOCUS)
    GUISetState(@SW_SHOW)
        GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
    While 1
        $nMsg= GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                ExitLoop
        EndSwitch
        $hControlId = ControlGetHandle($g_hGUI, "", ControlGetFocus($g_hGUI, ""))
        $iControlId = _WinAPI_GetDlgCtrlID ($hControlId)
        Switch $iControlId
            Case $g_idControls[1][0] To $g_idControls[1][UBound($g_idControls, 2) - 1]
                ;~ BackSpace Or Arrow Left Or Arrow Up
                If _IsPressed("08") Or _IsPressed("25") Or _IsPressed("26") Then
                    While _IsPressed("08") Or _IsPressed("25") Or _IsPressed("26")
                        Sleep(10)
                    WEnd
                    $aEditSel = _GUICtrlEdit_GetSel($iControlId)
                    If $aEditSel[0] = 0 And $aEditSel[1] = 0 Then
                        If $iControlId = $g_idControls[1][0] Then
                            GUICtrlSetState($g_idControls[1][UBound($g_idControls, 2) - 1], $GUI_FOCUS)
                            _GUICtrlEdit_SetSel($g_idControls[1][UBound($g_idControls, 2) - 1], -1, - 1)
                        Else
                            GUICtrlSetState($iControlId - 2, $GUI_FOCUS)
                            _GUICtrlEdit_SetSel($iControlId - 2, -1, - 1)
                        EndIf
                    EndIf
                ;~ Arrow Right Or Arrow Down
                ElseIf _IsPressed("27") Or _IsPressed("28") Then
                    While _IsPressed("27") Or _IsPressed("28")
                        Sleep(10)
                    WEnd
                    $aEditSel = _GUICtrlEdit_GetSel($iControlId)
                    If $aEditSel[0] = _GUICtrlEdit_GetTextLen($iControlId) And $aEditSel[1] = _GUICtrlEdit_GetTextLen($iControlId) Then
                        If $iControlId = $g_idControls[1][UBound($g_idControls, 2) - 1] Then
                            GUICtrlSetState($g_idControls[1][0], $GUI_FOCUS)
                            _GUICtrlEdit_SetSel($g_idControls[1][0], -1, - 1)
                        Else
                            GUICtrlSetState($iControlId + 2, $GUI_FOCUS)
                            _GUICtrlEdit_SetSel($iControlId + 2, -1, - 1)
                        EndIf
                    EndIf
                EndIf
        EndSwitch
    WEnd
    GUIDelete()
EndFunc

Func _GuiCreateControls($_vCtrlLabel = "", $_iMaxChars = 10)
    Local $i_xCtrlInput = 85
    Local $i_yCtrlCoord = 10
    For $i = 0 To UBound($g_idControls, 2) - 1
        $g_idControls[0][$i] = GUICtrlCreateLabel($_vCtrlLabel & $i + 1, 10, $i_yCtrlCoord, 70, 20, $SS_CENTERIMAGE)
        GUICtrlSetColor($g_idControls[0][$i], $g_dNoFocus)
        $g_idControls[1][$i] = GUICtrlCreateInput("", $i_xCtrlInput, $i_yCtrlCoord, 70, 20)
        GUICtrlSetLimit($g_idControls[1][$i], $_iMaxChars)
        $i_yCtrlCoord += 25
    Next
EndFunc

Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg
    Local $hWndFrom, $iIDFrom, $iCode, $hWndEdit, $iEnd, $iKeyPressed = ""
    If Not IsHWnd($g_idControls[0][0]) Then $hWndEdit = GUICtrlGetHandle($g_idControls[0][0])
    $hWndFrom = $lParam
    $iIDFrom = _WinAPI_LoWord($wParam)
    $iCode = _WinAPI_HiWord($wParam)
    Switch $iIDFrom
        Case $g_idControls[1][0] To $g_idControls[1][UBound($g_idControls, 2) - 1]
            Switch $iCode
                Case $EN_KILLFOCUS ; Sent when an edit control loses the keyboard focus
                    GUICtrlSetColor($iIDFrom - 1, $g_dNoFocus)
                Case $EN_MAXTEXT ; Sent when the current text insertion has exceeded the specified number of characters for the edit control
                    For $i = 32 To 126
                        If _IsPressed(Dec2Hex($i)) Then
                            $iKeyPressed = Dec2Hex($i)
                            ExitLoop
                        EndIf
                    Next
                    If $iIDFrom = $g_idControls[1][UBound($g_idControls, 2) - 1] Then
                        GUICtrlSetState($g_idControls[1][0], $GUI_FOCUS)
                        If $iKeyPressed <> "" Then GUICtrlSetData($g_idControls[1][0], _HexToString("0x" & $iKeyPressed))
                    Else
                        GUICtrlSetState($iIDFrom + 2, $GUI_FOCUS)
                        If $iKeyPressed <> "" Then GUICtrlSetData($iIDFrom + 2, _HexToString("0x" & $iKeyPressed))
                    EndIf
                Case $EN_SETFOCUS ; Sent when an edit control receives the keyboard focus
                    GUICtrlSetColor($iIDFrom - 1, $g_dFocused)
                    $iEnd = StringLen(GUICtrlRead($iIDFrom))
                    GuiCtrlSendMsg($iIDFrom, $EM_SETSEL, $iEnd, $iEnd)
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_COMMAND

;~ Created by Malkey
Func Dec2Hex($iN)
    Local $ihex = ""
    Do
        $ihex = Hex(Mod($iN, 16), 1) & $ihex
        $iN = Floor($iN / 16)
        ;ConsoleWrite($iN & " " & $ihex & @CRLF)
    Until $iN = 0
    Return $ihex
EndFunc ;==>Dec2Hex

 

Edited by Subz
Link to comment
Share on other sites

Efo74,

This will do what you've asked...

#include <EditConstants.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <array.au3>
#include <Misc.au3>
#include <WinAPI.au3>
#include <WinAPIvkeysConstants.au3>
#include <APISysConstants.au3>
#include <WinAPISys.au3>
#include <GuiEdit.au3>

#AutoIt3Wrapper_Add_Constants=n

Local $hDLL = DllOpen("user32.dll")

; keep track of which input control has focus
Local $who_has_focus

; max input characters
Local $maxchars = 10

Local $gui010 = GUICreate('Input Control Tabbing Demo')

Local $ctlInputIDS[6]
For $1 = 0 To 5
    GUICtrlCreateLabel('Input # ' & $1 + 1, 10, $1 * 25 + 10, 100, 20)
    $ctlInputIDS[$1] = GUICtrlCreateInput('', $1 + 70, $1 * 25 + 10, 100, 20, $es_left)
Next

GUISetState()

$who_has_focus = $ctlInputIDS[0]

GUIRegisterMsg($WM_COMMAND, 'WM_COMMAND')

While 1
    Switch GUIGetMsg()
        Case $gui_event_close
            Exit
    EndSwitch

    If _IsPressed("08", $hDLL) Then

        While _IsPressed("08", $hDLL)
            Sleep(100)
        WEnd

        ; if the input control is blank then tab backward
        If GUICtrlRead($who_has_focus) = '' Then
            ControlSend('', '', $who_has_focus, '+{tab}')

            ; the following deselects highlighting for the control tabbed to
            _GUICtrlEdit_SetSel(ControlGetHandle('', '', $who_has_focus), -1, 0)

        EndIf
    EndIf

WEnd

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

    Switch BitAND($wParam, 0xFFFF)
        Case $ctlInputIDS[0] To $ctlInputIDS[UBound($ctlInputIDS) - 1]
            Switch BitShift($wParam, 16)
                Case $EN_setfocus
                    $who_has_focus = BitAND($wParam, 0xFFFF)
                Case $EN_change
                    If StringLen(GUICtrlRead(BitAND($wParam, 0xffff))) >= $maxchars Then
                        ControlSend('', '', BitAND($wParam, 0xffff), '{tab}')
                    EndIf
            EndSwitch

    EndSwitch

    Return $GUI_RUNDEFMSG

EndFunc   ;==>WM_COMMAND

kylomas

*** delay is answering can be blamed on a river full of large mouth bass!!!

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...