Jump to content

[Solved] Connect to the right _WinAPI_ShellNotifyIconGetRect trayicon


nend
 Share

Recommended Posts

Hoi guys,

If you use this Windows API function to get the dimesions of the systray icon it gets mixup if you use multiple AutoIt programs with systray menu.
It use WinGetHandle(AutoItWinGetTitle()) to get the handle.
If you use a other AutoIt program with a systram menu and you open and close the menu (from the other AutoIt program) the handle came from that program.
And there for it return the coordinates and dimension from the other Autoit script.
Is there a way to connect this to the script it's running so it will not get mixup by other Autoit scripts with a systray menu?

#include <MsgBoxConstants.au3>
#include <WinAPIGdi.au3>
#include <WinAPIShellEx.au3>
#include <WinAPISys.au3>

Local $tRECT = _WinAPI_ShellNotifyIconGetRect(WinGetHandle(AutoItWinGetTitle()), 1)

Local $aPos
If Not @error Then
    $aPos = _WinAPI_GetPosFromRect($tRECT)
    MouseMove($aPos[0] + 12, $aPos[1] + 12)
    MouseClick('left')
    While 1
        Sleep(1000)
    WEnd
EndIf

 

Edited by nend
Link to comment
Share on other sites

Hi @nend.

I imagine your problem is hard to explain, but I'm not sure i 100% get/understand the issue.

  1. If you are afraid to get the coordinates from another AutoIt script from _WinAPI_ShellNotifyIconGetRect that's not an issue. An issue would be if one script running used multiple trays, but then again the script would know or can get the ID of the tray icons created.
  2. On the other hand, if your problem is tray position changing from when you used _WinAPI_ShellNotifyIconGetRect, it's just a matter of calling it on the line above required.

If none of the above is correctly understood, I'm sorry but explaining it another way may be necessary :>

Edited by genius257
Link to comment
Share on other sites

@genius257,

If you are afraid to get the coordinates from another AutoIt script from _WinAPI_ShellNotifyIconGetRect that's not an issue
That's exactly what happens!

On the other hand, if your problem is tray position changing from when you used _WinAPI_ShellNotifyIconGetRect, it's just a matter of calling it on the line above required.
I'm allready done that, I inserted into the main program and it works fine except for the problem above.
I think it has to do with WinGetHandle(AutoItWinGetTitle()) it returns for all AutoIt scripts the same handle.
The handle switch when you open (and close) a tray menu from a other Autoit script/exe, so the "_WinAPI_ShellNotifyIconGetRect" give the rect from that last script/exe.

See for yourself, open this script and see the console for the x position open a other Autoit program which has a Tray menu in it and open and close the tray menu you see that the coordinates chance to the last script where the tray menu is opened/closed.

#include <TrayConstants.au3>
#include <WinAPIGdi.au3>
#include <WinAPIShellEx.au3>


Opt("TrayMenuMode", 3)

Local $idExit = TrayCreateItem("Exit")

TraySetState($TRAY_ICONSTATE_SHOW)

TraySetToolTip(Chr("0"))

Sleep(500)

While 1
    $msg = TrayGetMsg()
    Switch $msg
        Case $idExit ; Exit the loop.
            ExitLoop
    EndSwitch

    _Tray_notification($msg, "_Gui_notification", "_Gui_notification_del", 500)
WEnd

Func _Gui_notification()
    ConsoleWrite("--> Notification" & @CRLF)
EndFunc

Func _Gui_notification_del()
    ConsoleWrite("<-- Out Notification" & @CRLF)
EndFunc

Func _Tray_notification($msg, $hover, $hover_out, $hover_time)
    Local Static $tray_count, $tray_tmr, $tray_pos, $tmr_notification, $notification
    Local $mousetgetpos

    If $msg = $TRAY_EVENT_MOUSEOVER Then
        If $tray_count <> "" Then
            If $tray_tmr = False Then
                $tmr_notification = TimerInit()
            EndIf

            Local $tRECT = _WinAPI_ShellNotifyIconGetRect(WinGetHandle(AutoItWinGetTitle()), 1)
            $tray_pos = _WinAPI_GetPosFromRect($tRECT)
            ConsoleWrite($tray_pos[0] & @CRLF)
            $tray_tmr = True
        Else
            $tray_count = $tray_count + 1
        EndIf
    EndIf

    If $tray_tmr = True Then
        If TimerDiff($tmr_notification) > $hover_time And $notification = False Then
            Call($hover)
            $notification = True
        Else
            $mousetgetpos = MouseGetPos()
            If Not @error Then
                If $mousetgetpos[0] < $tray_pos[0] Or $mousetgetpos[0] > $tray_pos[0] + $tray_pos[2] Or $mousetgetpos[1] < $tray_pos[1] Or $mousetgetpos[1] > $tray_pos[1] + $tray_pos[3] Then
                    $tray_tmr = False
                EndIf
            EndIf
        EndIf
    EndIf

    If $notification = True Then
        $mousetgetpos = MouseGetPos()
        If Not @error Then
            If $mousetgetpos[0] < $tray_pos[0] Or $mousetgetpos[0] > $tray_pos[0] + $tray_pos[2] Or $mousetgetpos[1] < $tray_pos[1] Or $mousetgetpos[1] > $tray_pos[1] + $tray_pos[3] Then
                $tray_tmr = False
                Call($hover_out)
                $notification = False
            EndIf
        EndIf
    EndIf
EndFunc

 

Edited by nend
Link to comment
Share on other sites

23 minutes ago, nend said:

I think I solved it just by changing the Autoit Window title "AutoItWinSetTitle"
Now this script has a different name than the other AutoIt programs.

Ah i think i get it now ^^'

In case window names gives you too much trouble, i suggest enumerating the windows and getting windows belonging to the PID.

I suggest doing this:

#include <WinAPIProc.au3>
#include <Array.au3>
$a = _WinAPI_EnumProcessWindows(@AutoItPID, False)
_ArrayDisplay($a); see all windows from PID
MsgBox(0, "AutoIt v3 window array position", _ArraySearch($a, "AutoIt v3"))
Exit

Hope this helps you :)

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