Code:
-- Global Shell32 = Library.Load("Shell32.dll"); User32 = Library.Load("user32.dll"); HWND = INT; HICON = INT; TCHAR = STRING; GUID = INT; NOTIFYICONDATA = MemoryEx.DefineStruct{ DWORD "cbSize"; HWND "hWnd"; UINT "uID"; UINT "uFlags"; UINT "uCallbackMessage"; HICON "hIcon"; TCHAR ("szTip",128,1,MEMEX_ASCII); DWORD "dwState"; DWORD "dwStateMask"; TCHAR ("szInfo",256,1,MEMEX_ASCII); UNION { UINT "uTimeout"; UINT "uVersion"; }; TCHAR ("szInfoTitle",256,1,MEMEX_ASCII); DWORD "dwInfoFlags"; GUID "guidItem"; HICON "hBalloonIcon"; }; Tray = {} -- Internal Functions Tray.CreateHost = function() Tray.OSName = System.GetOSName(); if OS == "" then return false, "Can't determine OS" end local I = User32.CreateWindowExA(0,"Edit","",1342308359,1,1,0,0,Application.GetWndHandle(),0,0,0) if I and (type(I) ~= "number") then return false, "Can't create callback" else local s = Subclass.Create(I, function(hWnd,uMsg,wParam,lParam) if(uMsg == 0x0002)then Subclass.Remove(I); return 0 end if(uMsg == 0x4DE)then local M = System.GetMousePosition(false,nil) if lParam == 0x0201 then Tray.OnLeftButton(wParam,false,M.X,M.Y) elseif lParam == 0x0202 then Tray.OnLeftButton(wParam,true,M.X,M.Y) elseif lParam == 0x0204 then Tray.OnRightButton(wParam,false,M.X,M.Y) elseif lParam == 0x0205 then Tray.OnRightButton(wParam,true,M.X,M.Y) elseif lParam == 1024 then Tray.OnBalloonShow(wParam,M.X,M.Y) elseif lParam == 1027 then Tray.OnBalloonHide(wParam,M.X,M.Y) elseif lParam == 1028 then Tray.OnBalloonTimeOut(wParam,M.X,M.Y) elseif lParam == 1029 then Tray.OnBalloonClick(wParam,M.X,M.Y) end return 0 end return Subclass.OldWinProc(hWnd,uMsg,wParam,lParam); end) Tray.HostHandle = I return true, I end end; -- Icon Functions Tray.FreeIcon = function(sIcon) local hIcon = User32.DestroyIcon(sIcon) if hIcon ~= 0 then return true, "OK" else return false, "Can't destroy icon" end end; Tray.LoadIcon = function(sIcon) local hIcon = User32.LoadImageA(0,sIcon,1,0,0,0x0010) if (hIcon ~= nil and hIcon > 0) then return true, hIcon else return false, "Can't load icon" end end; -- Main Functions Tray.CreateTrayIcon = function(iID,sIcon,sTool) if Tray.HostHandle ~= 1 then local x,y = Tray.CreateHost() if not x then return false, y end end local x,Ih = Tray.LoadIcon(sIcon) if not x then return false, Ih end local B = MemoryEx.Allocate(MemoryEx.StructSize(NOTIFYICONDATA) + 100); if(B)then S = MemoryEx.AssignStruct(B + 50,NOTIFYICONDATA); if(S)then S.hWnd = Tray.HostHandle; S.uID = iID; S.uFlags = 0x7; S.uCallbackMessage = 0x4DE; S.hIcon = Ih; S.szTip = sTool; S.dwState = 0x00000001; S.dwStateMask = 0x00000001; S.szInfo = ""; S.uTimeout = 0; S.uVersion = 3; S.szInfoTitle = ""; S.dwInfoFlags = 0x00000000; S.guidItem = 0; S.hBalloonIcon = ""; S.cbSize = 504; else MemoryEx.Free(B); return false, "Can't open structure" end else return false, "Can't create buffer" end local poi = S:GetPointer(); if tonumber(Shell32.Shell_NotifyIcon(0x00000000,poi)) == 0 then MemoryEx.Free(B); S:Close(); Tray.FreeIcon(Ih); return false, "Create tray icon failed" else Shell32.Shell_NotifyIcon(0x00000004,poi) MemoryEx.Free(B); S:Close(); Tray.FreeIcon(Ih); return true, "OK" end end; Tray.DeleteTrayIcon = function(iID) local B = MemoryEx.Allocate(MemoryEx.StructSize(NOTIFYICONDATA) + 100); if(B)then S = MemoryEx.AssignStruct(B + 50,NOTIFYICONDATA); if(S)then S.hWnd = Tray.HostHandle; S.uID = iID; S.uFlags = 0x3; S.uCallbackMessage = 0x4DE; S.hIcon = Ih; S.szTip = ""; S.dwState = 0x00000001; S.dwStateMask = 0x00000001; S.szInfo = ""; S.uTimeout = 0; S.uVersion = 3; S.szInfoTitle = ""; S.dwInfoFlags = 0x00000000; S.guidItem = 0; S.hBalloonIcon = ""; S.cbSize = 504; else MemoryEx.Free(B); return false, "Can't open structure" end else return false, "Can't create buffer" end local poi = S:GetPointer(); if tonumber(Shell32.Shell_NotifyIcon(0x00000002,poi)) == 0 then MemoryEx.Free(B); S:Close(); return false, "Delete tray icon failed" else MemoryEx.Free(B); S:Close(); return true, "OK" end end; Tray.SetToolTip = function(iID,tip) local B = MemoryEx.Allocate(MemoryEx.StructSize(NOTIFYICONDATA) + 100); if(B)then S = MemoryEx.AssignStruct(B + 50,NOTIFYICONDATA); if(S)then S.hWnd = Tray.HostHandle; S.uID = iID; S.uFlags = 0x00000004; S.uCallbackMessage = 0x4DE; S.hIcon = Ih; S.szTip = tip; S.dwState = 0x00000001; S.dwStateMask = 0x00000001; S.szInfo = ""; S.uTimeout = 0; S.uVersion = 3; S.szInfoTitle = ""; S.dwInfoFlags = 0x00000000; S.guidItem = 0; S.hBalloonIcon = ""; S.cbSize = 504; else MemoryEx.Free(B); return false, "Can't open structure" end else return false, "Can't create buffer" end local poi = S:GetPointer(); if tonumber(Shell32.Shell_NotifyIcon(0x00000001,poi)) == 0 then MemoryEx.Free(B); S:Close(); return false, "Set tool tip failed" else MemoryEx.Free(B); S:Close(); return true, "OK" end end; Tray.ChangeIcon = function(iID,sIcon) local x,Ih = Tray.LoadIcon(sIcon) if not x then return false, Ih end local B = MemoryEx.Allocate(MemoryEx.StructSize(NOTIFYICONDATA) + 100); if(B)then S = MemoryEx.AssignStruct(B + 50,NOTIFYICONDATA); if(S)then S.hWnd = Tray.HostHandle; S.uID = iID; S.uFlags = 0x3; S.uCallbackMessage = 0x4DE; S.hIcon = Ih; S.szTip = tip; S.dwState = 0x00000001; S.dwStateMask = 0x00000001; S.szInfo = ""; S.uTimeout = 0; S.uVersion = 3; S.szInfoTitle = ""; S.dwInfoFlags = 0x00000000; S.guidItem = 0; S.hBalloonIcon = ""; S.cbSize = 504; else MemoryEx.Free(B); return false, "Can't open structure" end else return false, "Can't create buffer" end local poi = S:GetPointer(); if tonumber(Shell32.Shell_NotifyIcon(0x00000001,poi)) == 0 then MemoryEx.Free(B); S:Close(); Tray.FreeIcon(Ih); return false, "Change icon failed" else MemoryEx.Free(B); S:Close(); Tray.FreeIcon(Ih); return true, "OK" end end; Tray.ShowBalloon = function(iID,sTitle,sMessage,Icon) local B = MemoryEx.Allocate(MemoryEx.StructSize(NOTIFYICONDATA) + 100); if(B)then S = MemoryEx.AssignStruct(B + 50,NOTIFYICONDATA); if(S)then --[[ local NIIF_NOSOUND = 0x00000010; local NIIF_LARGE_ICON = 0x00000020; local NIIF_RESPECT_QUIET_TIME = 0x00000080; local NIIF_INFO = 0x00000001; local NIIF_WARNING = 0x00000002; local NIIF_ERROR = 0x00000003; local NIIF_USER = 0x00000004; --]] S.hWnd = Tray.HostHandle; S.uID = iID; S.uFlags = 0x00000010 S.uCallbackMessage = 0x4DE; S.hIcon = Icon; S.szTip = 0; S.dwState = 0x00000001; S.dwStateMask = 0x00000001; S.szInfo = sMessage; S.uTimeout = 120000; S.uVersion = 3; S.szInfoTitle = sTitle; S.dwInfoFlags = 0x00000004; S.guidItem = 0; S.hBalloonIcon = Icon; S.cbSize = 504; else MemoryEx.Free(B); return false, "Can't open structure" end else return false, "Can't create buffer" end if tonumber(Shell32.Shell_NotifyIcon(0x00000001,S:GetPointer())) == 0 then MemoryEx.Free(B); S:Close(); return false, "Show balloon failed" else MemoryEx.Free(B); S:Close(); return true, "OK" end end; Tray.SetFocus = function(iID) local B = MemoryEx.Allocate(MemoryEx.StructSize(NOTIFYICONDATA) + 100); if(B)then S = MemoryEx.AssignStruct(B + 50,NOTIFYICONDATA); if(S)then S.hWnd = Tray.HostHandle; S.uID = iID; S.uFlags = 0x00000001; S.uCallbackMessage = 0x4DE; S.hIcon = Ih; S.szTip = ""; S.dwState = 0x00000001; S.dwStateMask = 0x00000001; S.szInfo = ""; S.uTimeout = 0; S.uVersion = 3; S.szInfoTitle = ""; S.dwInfoFlags = 0x00000000; S.guidItem = 0; S.hBalloonIcon = ""; S.cbSize = 504; else MemoryEx.Free(B) end else return false end local poi = S:GetPointer(); if tonumber(Shell32.Shell_NotifyIcon(0x00000003,poi)) == 0 then MemoryEx.Free(B); S:Close(); return false else MemoryEx.Free(B); S:Close(); return true end end; -- CallBack Functions Tray.OnLeftButton = function(nTrayID,bState,xPos,yPos) if bState then --Button Up else --Button Down end end Tray.OnRightButton = function(nTrayID,bState,xPos,yPos) if bState then --Button Up else --Button Down end end Tray.OnBalloonShow = function(nTrayID,xPos,yPos) -- Only used if the balloon was queued end Tray.OnBalloonHide = function(nTrayID,xPos,yPos) --The balloon has disappeared, not sent if the balloon is dismissed due to a timeout or mouse click end Tray.OnBalloonTimeOut = function(nTrayID,xPos,yPos) --The balloon is dismissed because of a timeout end Tray.OnBalloonClick = function(nTrayID,xPos,yPos) --The balloon is dismissed because a mouse click end --[[ -- Refrences [url]http://msdn.microsoft.com/en-us/library/windows/desktop/bb762159%28v=vs.85%29.aspx[/url] [url]http://msdn.microsoft.com/en-us/library/windows/desktop/bb773352%28v=vs.85%29.aspx[/url] -- Sample Usage -- On Show NICO1, Err = Tray.CreateTrayIcon(101,_SourceFolder.."\\Autoplay\\Icons\\1.ico","ID: 101") if not NICO1 then Dialog.Message("", Err) end -- Button On Click if NICO1 then local x, Ico = Tray.LoadIcon(_SourceFolder.."\\Autoplay\\Icons\\1.ico") if x then Tray.ShowBalloon(101,"Test Title","Test Message",Ico) end end -- On Button Right Click if NICO1 then Tray.SetToolTip(101,"New Tooltip ID: 101") end -- On ShutDown if NICO1 then Tray.DeleteTrayIcon(101) end --]]
Comment