Jump to content

How to remove focus state from a tray menu item?


VAN0
 Share

Recommended Posts

Hello.

When $TRAY_FOCUS used in TrayItemSetState(), how do we remove that state without removing and adding a new menu item?

The state parameter is a bit counterintuitive, because it's bitwise state, however why would we need $TRAY_UNCHECKED or $TRAY_ENABLE states if absents of $TRAY_CHECKED or $TRAY_DISABLE bits would in theory be the same thing...Anyhow, since there is no unfocus bit, how do we remove the focus bit?

#NoTrayIcon
#include <TrayConstants.au3> ; Required for the $TRAY_CHECKED and $TRAY_ICONSTATE_SHOW constants.
Opt("TrayMenuMode", 3) ; The default tray menu items will not be shown and items are not checked when selected. These are options 1 and 2 for TrayMenuMode.

Example()

Func Example()
        TrayCreateItem("Item 1")
        TrayItemSetState(-1, $TRAY_FOCUS)
        TrayItemSetState(-1, $TRAY_CHECKED)
        ConsoleWrite("Item 1 state: " & bits(TrayItemGetState(-1)) & " (" & TrayItemGetState(-1) & ")" & @CRLF)

        TrayCreateItem("Item 2")
        TrayItemSetState(-1, $TRAY_CHECKED)
        ConsoleWrite("Item 2 state: " & bits(TrayItemGetState(-1)) & " (" & TrayItemGetState(-1) & ")" & @CRLF)

        TrayCreateItem("Item 3")
        TrayItemSetState(-1, $TRAY_FOCUS)
        ConsoleWrite("Item 3 state: " & bits(TrayItemGetState(-1)) & " (" & TrayItemGetState(-1) & ")" & @CRLF)

        TrayCreateItem("Item 4")
        TrayItemSetState(-1, $TRAY_DISABLE)
        ConsoleWrite("Item 4 state: " & bits(TrayItemGetState(-1)) & " (" & TrayItemGetState(-1) & ")" & @CRLF)

        TrayCreateItem("Item 5")
        for $i = 0 to 4096
            TrayItemSetState(-1, $i)
        Next
        ConsoleWrite("Item 5 state: " & bits(TrayItemGetState(-1)) & " (" & TrayItemGetState(-1) & ")" & @CRLF)

        TrayCreateItem("Item 6")
        for $i = 0 to 4096
            TrayItemSetState(-1, $i)
        Next
        TrayItemSetState(-1, $TRAY_ENABLE)
        ConsoleWrite("Item 6 state: " & bits(TrayItemGetState(-1)) & " (" & TrayItemGetState(-1) & ")" & @CRLF)

        TrayCreateItem("Item 7")
        for $i = 0 to 4096
            TrayItemSetState(-1, $i)
        Next
        TrayItemSetState(-1, 0)
        ConsoleWrite("Item 7 state: " & bits(TrayItemGetState(-1)) & " (" & TrayItemGetState(-1) & ")" & @CRLF)

        TrayCreateItem("") ; Create a separator line.

        Local $idExit = TrayCreateItem("Exit")
        TraySetState($TRAY_ICONSTATE_SHOW) ; Show the tray menu.
        While 1
                Switch TrayGetMsg()
                        Case $idExit ; Exit the loop.
                                ExitLoop
                EndSwitch
        WEnd
EndFunc   ;==>Example

Func bits($iNum)
    Local $sNum = ""
    While $iNum
        $sNum = BitAND($iNum, 1) & $sNum
        $iNum = BitShift($iNum, 1)
    WEnd
    $sNum = StringRight("0000000000000000" & $sNum, 15)
    If $sNum < 0 Then Return '1' & $sNum
    Return '0' & $sNum
EndFunc

 

Edited by VAN0
Link to comment
Share on other sites

  • VAN0 changed the title to How to remove focus state from a tray menu item?

Maybe this is a bug.

Currently it can be only removed by hovering the mouse over it.  (I guess, if you have a Tutorial option in a Program, this can be used to show where user needs to go or to click ... ) 

If it is an issue, maybe you should open a ticket in the bug-tracker ? 

I have modified your code, a bit. Now you can check and uncheck the 1st menu item but the focus does not go away.

#NoTrayIcon
#include <TrayConstants.au3> ; Required For the $TRAY_CHECKED And $TRAY_ICONSTATE_SHOW constants.
Opt("TrayMenuMode", 3) ; The default tray menu items will not be shown and items are not checked when selected. These are options 1 and 2 for TrayMenuMode.

Example()

Func Example()
    $ti = TrayCreateItem("Item 1")
    TrayItemSetState($ti, $TRAY_FOCUS)
    TrayItemSetState($ti, $TRAY_CHECKED)
    ConsoleWrite("Item 1 state: " & bits(TrayItemGetState($ti)) & " (" & TrayItemGetState($ti) & ")" & @CRLF)

    $pp = TrayCreateItem("Display menu 1 state")
    $pi = TrayCreateItem("Focus menu 1")
    $pc = TrayCreateItem("Check menu 1")
    $puc = TrayCreateItem("Uncheck menu 1")
    TrayCreateItem("")     ; Create a separator line.

    Local $idExit = TrayCreateItem("Exit")
    TraySetState($TRAY_ICONSTATE_SHOW)     ; Show the tray menu.
    While 1
        Switch TrayGetMsg()
            Case $idExit             ; Exit the loop.
                ExitLoop
            Case $pp
                ConsoleWrite("Item 1 state: " & bits(TrayItemGetState($ti)) & " (" & TrayItemGetState($ti) & ")" & @CRLF)
            Case $pi
                TrayItemSetState($ti, $TRAY_FOCUS)
                ConsoleWrite("Item 1 state: " & bits(TrayItemGetState($ti)) & " (" & TrayItemGetState($ti) & ")" & @CRLF)
            Case $pc
                TrayItemSetState($ti, $TRAY_CHECKED)
                ConsoleWrite("Item 1 state: " & bits(TrayItemGetState($ti)) & " (" & TrayItemGetState($ti) & ")" & @CRLF)
            Case $puc
                TrayItemSetState($ti, $TRAY_UNCHECKED)
                ConsoleWrite("Item 1 state: " & bits(TrayItemGetState($ti)) & " (" & TrayItemGetState($ti) & ")" & @CRLF)
        EndSwitch
    WEnd
EndFunc   ;==>Example

Func bits($iNum)
    Local $sNum = ""
    While $iNum
        $sNum = BitAND($iNum, 1) & $sNum
        $iNum = BitShift($iNum, 1)
    WEnd
    $sNum = StringRight("0000000000000000" & $sNum, 15)
    If $sNum < 0 Then Return '1' & $sNum
    Return '0' & $sNum
EndFunc   ;==>bits

 

 

Edited by Dan_555

Some of my script sourcecode

Link to comment
Share on other sites

The problem is more deep than you think.  By getting the hMenu of the tray menu, you can use Win32 API to modify it.  But (oh surprise) even with pure Win32 API you cannot remove the focus state.  So the problem is not AutoIt at all, it is a Windows issue (feature ?).

Edited by Nine
Link to comment
Share on other sites

Simple :

Local $hMenu = TrayItemGetHandle(0)
Local $tInfo = DllStructCreate($tagMENUITEMINFO)
DllStructSetData($tInfo, "Size", DllStructGetSize($tInfo))
DllStructSetData($tInfo, "Mask", $MIIM_STATE)
DllStructSetData($tInfo, "State", 0) ; should remove check & hilite, but only check is done
Local $aCall = DllCall("user32.dll", "bool", "SetMenuItemInfoW", "handle", $hMenu, "uint", 0, "bool", True, "struct*", $tInfo)

 

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...