[SOLVED] SysTray Position

Windows specific forum
SkyManager
Enthusiast
Enthusiast
Posts: 339
Joined: Tue Jan 30, 2007 5:47 am
Location: Hong Kong

[SOLVED] SysTray Position

Post by SkyManager »

Does anybody know how to get the position of the SysTray Icon?
I want to display a window right above the SysTray Icon.
Last edited by SkyManager on Tue Feb 08, 2011 7:08 am, edited 1 time in total.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: SysTray Position

Post by IdeasVacuum »

...should be simple? Verify the screen size, subtract the typical height of the Task Bar:

Code: Select all

ExamineDesktops()

#MainScreen = 0
#MyWindow = 1

   iScreenWidth.l = DesktopWidth(#MainScreen)
  iScreenHeight.l = DesktopHeight(#MainScreen)
 iTaskBarHeight.l = 60
 iMyWindowWidth.l = 250
iMyWindowHeight.l = 150

iWinPosX.l = iScreenWidth - iMyWindowWidth
iWinPosY.l = iScreenHeight - (iMyWindowHeight + iTaskBarHeight)
 
If OpenWindow(#MyWindow,iWinPosX,iWinPosY,iMyWindowWidth,iMyWindowHeight,"Window above Sys Tray",#PB_Window_SystemMenu)

EndIf

Repeat

      iEvent.i = WaitWindowEvent()
 
Until iEvent = #PB_Event_CloseWindow

End
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
C64
Enthusiast
Enthusiast
Posts: 151
Joined: Sat Dec 18, 2010 4:40 am

Re: SysTray Position

Post by C64 »

IdeasVacuum wrote:subtract the typical height of the Task Bar
No way! The height of the Taskbar is different on every PC. Even from XP to 7. It depends what theme Windows is running (Classic or XP/Aero), and if the user has set it larger than usual (to show the date with the clock on XP), whether it's locked or not, the system font size, and so on. And not everyone has it at the bottom. You simply cannot hard-code a value of 60 and assume it's going to be alright. There's code in these forums that show how to get the exact value, even taking into account if it's not at the bottom.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: SysTray Position

Post by Trond »

Even then it wouldn't solve the problem of knowing where in the systray the icon is.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: SysTray Position

Post by IdeasVacuum »

:cry: I knew there had to be a catch. Is the Sys Tray effectively just another window? If so, what does that make the icons, are they 'buttons in disguise' or maybe a listicon gadget?
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
idle
Always Here
Always Here
Posts: 5844
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: SysTray Position

Post by idle »

Code: Select all

 SystemParametersInfo_(#SPI_GETWORKAREA,0,@rc,0)
 GetCursorPos_(@pt)
 offset = GetSystemMetrics_(#SM_CYMENU) ;if it's a pop up menu 
 DisplayPopupMenu(#menu,WindowID(gWind),pt\x-30,rc\bottom-offset)
Windows 11, Manjaro, Raspberry Pi OS
Image
SkyManager
Enthusiast
Enthusiast
Posts: 339
Joined: Tue Jan 30, 2007 5:47 am
Location: Hong Kong

Re: SysTray Position

Post by SkyManager »

Thanks for the tips
But it seems to me that there is an error in defining the rc structure.
Any help for me?

Code: Select all

Structure pts
  x.l
  y.l
EndStructure
pt.pts
Structure rcs
  bottom_offset.l
EndStructure
rc.rcs
gWind = 0
menu = 1
If Not CreatePopupMenu(menu) : End : EndIf
MenuItem(1,"TEST-1")
MenuItem(2,"TEST-2")
MenuItem(3,"TEST-3")
SystemParametersInfo_(#SPI_GETWORKAREA, 0, @rc, 0)
GetCursorPos_(@pt)
offset = GetSystemMetrics_(#SM_CYMENU) ;if it's a pop up menu
If Not OpenWindow(gWind, 100, 150, 300, 100, "Test", #PB_Window_Normal) : End : EndIf
runOnceOnly.l = #True
Repeat
  If (runOnceOnly)
    runOnceOnly = #False
    DisplayPopupMenu(menu, WindowID(gWind), pt\x-30, rc\bottom_offset)
  EndIf
  Event = WaitWindowEvent()
Until (Event = #PB_Event_CloseWindow)
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: SysTray Position

Post by Trond »

MSDN wrote:SPI_GETWORKAREA
Retrieves the size of the working area. The working area is the portion of the screen not obscured by the tray. The pvParam parameter must point to the RECT structure that receives the coordinates of the working area.
Your structure rcs does not bear much resemblance to a rectangle.
SkyManager
Enthusiast
Enthusiast
Posts: 339
Joined: Tue Jan 30, 2007 5:47 am
Location: Hong Kong

Re: SysTray Position

Post by SkyManager »

Thanks for the tips.
My test is working now :D

Code: Select all

Structure pts
  x.l
  y.l
EndStructure
pt.pts
rc.RECT
gWind       = 0
menu        = 1
SystemTray  = 2
FileIcon    = 3
If Not OpenWindow(gWind, 100, 150, 300, 100, "Test", #PB_Window_Normal) : End : EndIf
DataSection
  FileIconLabel:
  IncludeBinary "traymenu.bmp"
EndDataSection
IconID = CatchImage(FileIcon, ?FileIconLabel)
AddSysTrayIcon(SystemTray, WindowID(gWind), IconID)
SysTrayIconToolTip(SystemTray, "Title")
If Not CreatePopupMenu(menu) : End : EndIf
MenuItem(1,"TEST-1")
MenuItem(2,"TEST-2")
MenuItem(3,"TEST-3")
Repeat
  Event = WaitWindowEvent()
  If Event = #PB_Event_SysTray
    SystemParametersInfo_(#SPI_GETWORKAREA, SystemTray, @rc, 0)
    GetCursorPos_(@pt)
    offset = GetSystemMetrics_(SystemTray) ;if it's a pop up menu
    DisplayPopupMenu(menu, WindowID(gWind), pt\x, rc\bottom-offset + 30)
  EndIf
Until (Event = #PB_Event_CloseWindow)
FreeImage(FileIcon)
FreeMenu(menu)
FreeMenu(SystemTray)
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: [SOLVED] SysTray Position

Post by Shardik »

SkyManager wrote:Does anybody know how to get the position of the SysTray Icon?
I want to display a window right above the SysTray Icon.
Why do you need to calculate the position of the SysTray icon?
Because PureBasic does that for you automatically if you omit the
x and y coordinates. So in Windows XP SP3 and Windows 7 with

Code: Select all

  If Event = #PB_Event_SysTray
    SystemParametersInfo_(#SPI_GETWORKAREA, SystemTray, @rc, 0)
    GetCursorPos_(@pt)
    offset = GetSystemMetrics_(SystemTray) ;if it's a pop up menu
    DisplayPopupMenu(menu, WindowID(gWind), pt\x, rc\bottom-offset + 30)
  EndIf
I achieve the same result as with

Code: Select all

  If Event = #PB_Event_SysTray
    DisplayPopupMenu(menu, WindowID(gWind))
  EndIf
And without the calculation of the icon position using WinAPI calls
and structures it should even work crossplatform. This example works
in Windows and Linux (Suse Linux Enterprise Server 10 SP3 x64):

Code: Select all

If Not OpenWindow(gWind, 100, 150, 300, 100, "Test") : End : EndIf
IconID = LoadImage(FileIcon, #PB_Compiler_Home + "examples/sources/Data/Drive.bmp")
If IconID
  AddSysTrayIcon(SystemTray, WindowID(gWind), IconID)
  SysTrayIconToolTip(SystemTray, "Title")
  If Not CreatePopupMenu(menu) : End : EndIf
  MenuItem(1,"TEST-1")
  MenuItem(2,"TEST-2")
  MenuItem(3,"TEST-3")
  Repeat
    Event = WaitWindowEvent()
    If Event = #PB_Event_SysTray
      DisplayPopupMenu(menu, WindowID(gWind))
    EndIf
  Until (Event = #PB_Event_CloseWindow)
EndIf
SkyManager
Enthusiast
Enthusiast
Posts: 339
Joined: Tue Jan 30, 2007 5:47 am
Location: Hong Kong

Re: [SOLVED] SysTray Position

Post by SkyManager »

The answer is simple because I want to popup my own designed window with other gadgets, not just a plain popup menu.
noorderling
User
User
Posts: 16
Joined: Fri Jul 16, 2010 3:54 pm

Re: [SOLVED] SysTray Position

Post by noorderling »

But what if the taskbar is on the upper side , or left or right side?
SkyManager
Enthusiast
Enthusiast
Posts: 339
Joined: Tue Jan 30, 2007 5:47 am
Location: Hong Kong

Re: [SOLVED] SysTray Position

Post by SkyManager »

Oops! You're right :?
Post Reply