AutoClose Brackets Topic is solved

Script editor, GUI designer, debugger and tools for AutoHotkey
Rafio
Posts: 25
Joined: 07 Oct 2018, 05:14

AutoClose Brackets

03 Oct 2019, 09:10

Is it possible in AutoGui to programmatically toggle AutoClose Brackets?
I would like to disable this funcion before sending hotstrings and re-enable it just after.

Thanks.
User avatar
kczx3
Posts: 921
Joined: 06 Oct 2015, 21:39

Re: AutoClose Brackets

03 Oct 2019, 10:34

Maybe just paste the contents instead of using the hotstring to "type" the text.
https://www.autohotkey.com/boards/viewtopic.php?t=39078
Rafio
Posts: 25
Joined: 07 Oct 2018, 05:14

Re: AutoClose Brackets

05 Oct 2019, 08:23

Thanks, but it does not solve my problem.

Is it possible to use gui scripting to trigger the menu item?
User avatar
Alguimist
Posts: 347
Joined: 05 Oct 2015, 16:41
Contact:

Re: AutoClose Brackets

11 Oct 2019, 05:00

Rafio wrote:
03 Oct 2019, 09:10
Is it possible in AutoGui to programmatically toggle AutoClose Brackets?
I would like to disable this funcion before sending hotstrings and re-enable it just after.
Try this:

Code: Select all

::rc::
    ToggleBrackets()
    Send VarSetCapacity(RECT, 16, 0)
    ToggleBrackets()
Return

ToggleBrackets() {
    TopMenu := " &Options "
    MenuItem := "Autoclose &Brackets"
    WinMenuSelectItem A,, %TopMenu%, %MenuItem%
}
Rafio
Posts: 25
Joined: 07 Oct 2018, 05:14

Re: AutoClose Brackets

14 Oct 2019, 07:08

Thanks, Alguimist.
And my appologies for being so late to respond.

The script is fine but:
could we know if the menu item is checked prior to select it?
User avatar
Alguimist
Posts: 347
Joined: 05 Oct 2015, 16:41
Contact:

Re: AutoClose Brackets  Topic is solved

15 Oct 2019, 15:35

Rafio wrote:could we know if the menu item is checked prior to select it?
You can do it with the Windows API function GetMenuState.

Code: Select all

Global g_AppName := "AutoGUI"
     , g_TopMenu := " &Options "
     , g_MenuItem := "Autoclose &Brackets"

::rc::
    Enabled := IsAutocloseBracketEnabled() ; Store current state
    If (Enabled == -1) {
        Return
    }

    EnableAutocloseBrackets(0) ; Disable

    Send VarSetCapacity(RECT, 16, 0)

    If (Enabled) {
        EnableAutocloseBrackets(1) ; Restore
    }
Return

EnableAutocloseBrackets(Enable := True) {
    AutocloseBracketsEnabled := IsAutocloseBracketEnabled()
    If ((!Enable && AutocloseBracketsEnabled) || (Enable && !AutocloseBracketsEnabled)) {
        WinMenuSelectItem A,, %g_TopMenu%, %g_MenuItem%
    }
}

IsAutocloseBracketEnabled() {
    WinGet hWnd, ID, %g_AppName% v ahk_class AutoHotkeyGUI
    If (!hWnd) {
        MsgBox 0x10, Error, The window could not be found.
        Return -1
    }

    hMenu := GetMenu(hWnd)
    OptionsMenuPos := FindMenuPos(hMenu, g_TopMenu)

    hMenuOptions := GetSubMenu(hMenu, OptionsMenuPos)
    AutocloseBracketsPos := FindMenuPos(hMenuOptions, g_MenuItem)

    Return IsMenuItemChecked(hMenuOptions, AutocloseBracketsPos)
}

FindMenuPos(hMenu, MenuString) {
    MenuItemCount := GetMenuItemCount(hMenu)

    Loop %MenuItemCount% {
        If (GetMenuString(hMenu, A_Index - 1) = MenuString) {
            Return A_Index - 1 ; Zero-based
        }
    }

    Return -1
}

GetMenu(hWnd) {
    Return DllCall("GetMenu", "Ptr", hWnd, "Ptr")
}

GetSubMenu(hMenu, nPos) {
    Return DllCall("GetSubMenu", "Ptr", hMenu, "UInt", nPos, "Ptr")
}

GetMenuItemCount(hMenu) {
    Return DllCall("GetMenuItemCount", "Ptr", hMenu)
}

GetMenuItemID(hMenu, nPos) {
    Return DllCall("GetMenuItemID", "Ptr", hMenu, "UInt", nPos)
}

GetMenuString(hMenu, uIDItem) { ; zero-based
    Local lpString, MenuItemID

    VarSetCapacity(lpString, 4096)
    If !(DllCall("GetMenuString", "Ptr", hMenu, "UInt", uIDItem, "Str", lpString, "UInt", 4096, "UInt", 0x400)) {
        MenuItemID := GetMenuItemID(hMenu, uIDItem)
        If (MenuItemID > -1) {
            Return "SEPARATOR"
        } Else {
            Return (GetSubMenu(hMenu, uIDItem)) ? "SUBMENU" : "ERROR"
        }
    }

    Return lpString
}

IsMenuItemChecked(hMenu, Pos) {
    MenuState := DllCall("GetMenuState", "Ptr", hMenu, "UInt", Pos, "UInt", 0x400) ; By position
    Return MenuState & 0x8 ; MF_CHECKED
}
Note: I'm considering to implement expansion of abbreviations as a built-in functionality, but this will take some time.
Rafio
Posts: 25
Joined: 07 Oct 2018, 05:14

Re: AutoClose Brackets

16 Oct 2019, 11:48

Perfect!
I have modified your script to make it a unique function that only desables the menu item.

Problem solved.

;)

Return to “AutoGUI”

Who is online

Users browsing this forum: No registered users and 7 guests