Code: Select all
;-TOP
; Comment : Memory State
; Author : mk-soft
; Create : 18.09.2016
; Version : v1.01
; ***************************************************************************************
; PB-Internal
Macro SystrayID(Systray)
PeekI(IsSysTrayIcon(Systray))
EndMacro
Procedure SetSystrayWidth(Systray, Width)
Protected StatusItem, ItemLength.cgfloat
ItemLength = Width
StatusItem = CocoaMessage(0, SystrayID(Systray), "retain")
CocoaMessage(0, StatusItem, "setLength:@", @ItemLength)
EndProcedure
; ***************************************************************************************
#NSApplicationActivationPolicyRegular = 0
#NSApplicationActivationPolicyAccessory = 1
#NSApplicationActivationPolicyProhibited = 2
Procedure HideApplication(Window, State)
Protected NSApp
NSApp = CocoaMessage(0, 0, "NSApplication sharedApplication")
If State
CocoaMessage(0, NSApp, "setActivationPolicy:", #NSApplicationActivationPolicyAccessory)
Else
CocoaMessage(0, NSApp, "setActivationPolicy:", #NSApplicationActivationPolicyRegular)
EndIf
HideWindow(Window, State)
EndProcedure
; ***************************************************************************************
Enumeration
#MenuShowWindow
#MenuHideWindow
#MenuAbout
#MenuExit
#MenuInfo
EndEnumeration
; ***************************************************************************************
Procedure UpdateSystray(Systray, Image, Font)
Protected x, y, w, total, free, prozent, text.s, color
Static old_free
free = MemoryStatus(#PB_System_FreePhysical) / 1024 / 1024
If free = old_free
ProcedureReturn 0
EndIf
old_free = free
total = MemoryStatus(#PB_System_TotalPhysical) / 1024 / 1204
prozent = free * 100 / total
text = Str(prozent) + "%"
If prozent >= 30
color = $FFFFFF
ElseIf prozent >= 10
color = $80FFFF
Else
color = $C0C0FF
EndIf
StartDrawing(ImageOutput(Image))
Box(0, 0, 40, 20, color)
w = TextWidth(text)
x = 20 - w / 2
y = -2
DrawingMode(#PB_2DDrawing_Transparent)
DrawingFont(Font)
DrawText(x, y, text, $000000)
StopDrawing()
text = "Free: " + Str(free) + "/" + Str(total) + " MB"
SysTrayIconToolTip(Systray, text)
ChangeSysTrayIcon(Systray, ImageID(Image))
SetMenuItemText(0, #MenuInfo, text)
EndProcedure
; ***************************************************************************************
Procedure Main()
Protected event, exit, image, font
If OpenWindow(0, 100, 150, 300, 100, "PureBasic - MemState", #PB_Window_SystemMenu)
HideApplication(0, #True)
CreatePopupMenu(0)
MenuItem(#MenuShowWindow, "Show Window")
MenuItem(#MenuHideWindow, "Hide Window")
MenuBar()
MenuItem(#MenuAbout, "About")
MenuBar()
MenuItem(#MenuExit, "Exit")
MenuBar()
MenuItem(#MenuInfo, "")
font = LoadFont(0, "Arial", 14)
image = CreateImage(0, 40, 20)
AddSysTrayIcon(0, WindowID(0), image)
UpdateSystray(0, 0, font)
AddWindowTimer(0, 0, 5000)
Repeat
Event = WaitWindowEvent()
Select Event
Case #PB_Event_Menu
Select EventMenu()
Case #PB_Menu_About
Case #PB_Menu_Preferences
Case #PB_Menu_Quit
Break
Case #MenuShowWindow
HideApplication(0, #False)
Case #MenuHideWindow
HideApplication(0, #True)
Case #MenuAbout
MessageRequester("About", "Physical Memory State by mk-soft", #PB_MessageRequester_Info)
Case #MenuExit
Break
EndSelect
Case #PB_Event_SysTray
If EventType() = #PB_EventType_LeftClick
DisplayPopupMenu(0, WindowID(0), DesktopMouseX() - 20, 30)
EndIf
Case #PB_Event_Timer
UpdateSystray(0, 0, font)
Case #PB_Event_CloseWindow
Break
EndSelect
ForEver
EndIf
EndProcedure : Main()