2011-04-17

FreeBASIC -- systray

這是為了能夠在windows的system tray做出圖示效果,而找到的範例



#define WIN_INCLUDEALL
#include "windows.bi"
#include "fbgfx.bi"

#define WM_SHELLNOTIFY WM_USER + 1

Declare Function NewWindowProc(As HWND,As Uinteger,wParam As WPARAM,lParam As LPARAM) As LRESULT


dim as HWND hWin
Dim shared as WNDPROC OldWindowProc 

ScreenRes 640,480,32
WindowTitle "Minimize me, press any key key for quit"
ScreenControl FB.GET_WINDOW_HANDLE,cast(integer,hWin)
OldWindowProc = cptr(WNDPROC,SetWindowLong(hWin,GWL_WNDPROC,cast(DWORD,@NewWindowProc)))

' Set up our notify icon
Dim Shared As NOTIFYICONDATA NID
With NID
  .cbSize           = SizeOf (NOTIFYICONDATA)
  .hWnd             = hWin
  .hIcon            = LoadIcon (NULL, IDI_QUESTION)
  .uFlags           = NIF_INFO or NIF_MESSAGE or NIF_TIP or NIF_ICON
  .uCallbackMessage = WM_SHELLNOTIFY
  .szTip            = "The Tip"
  .szInfoTitle      = "The Tile"
  .szInfo           = "The Info"
End With

sleep

Function NewWindowProc(Byval hWin   As HWND, _
                       Byval Msg    As Uinteger, _
                       Byval wParam As WPARAM, _
                       Byval lParam As LPARAM) As LRESULT
  Dim As RECT rcWindow,rcTray
  Dim As HWND hWndTaskBar,hWndTray
    
  Select Case As Const Msg
    Case WM_SYSCOMMAND
      If wParam = SC_MINIMIZE Then
        ' Get the system tray's hWnd
        hWndTaskBar = FindWindow("Shell_TrayWnd", NULL)
        hWndTray = FindWindowEx(hWndTaskBar, 0, "TrayNotifyWnd", NULL)
        ' Show the notify icon
        Shell_NotifyIcon (NIM_ADD, @NID) 
        ' Animate the window closing to the tray
        GetWindowRect hWin, @rcWindow
        GetWindowRect hwndTray, @rcTray
        DrawAnimatedRects hWin, IDANI_CAPTION, @rcWindow, @rcTray

        ' Hide the window
        ShowWindow(hWin, SW_HIDE)
        Return False
      End If
    Case WM_SHELLNOTIFY
      If lParam = WM_LBUTTONDBLCLK Then
        ' Restore the window
        ShowWindow (hWin, SW_SHOWMINIMIZED)
        SendMessage(hWin, WM_SYSCOMMAND, SC_RESTORE,0)
        Shell_NotifyIcon(NIM_DELETE,@NID) 
    End If
  End Select

  Return OldWindowProc(hWin, Msg, wParam, lParam)
End Function

沒有留言: