Google
 

Thursday, July 30, 2009

Make System Tray Application in delphi,


Make System Tray Application in Delphi in order to run background or placed on system tray is very easy. We Only need one API function that is Shell_NotifyIcon,the Application will be run as system tray with there function.
The First at private you must declaration
private
{ Private declarations }
systemTrayIcon:TNotifyIconData;

And Then, make a form, on event on Create add script below this

with systemTrayIcon do
begin
cbSize := Sizeof(systemTrayIcon);
Wnd:=Handle;
uID := 0;
uFlags := NIF_MESSAGE + NIF_ICON + NIF_TIP;
uCallbackMessage := WM_USER;
hIcon := Application.Icon.Handle;
StrCopy(szTip,'Aplikasi System Tray');
End;
Shell_NotifyIcon(NIM_ADD,@systemTrayIcon);

xplanation script above are :
Wnd is parameter from systemTrayIcon that use for appear icon on system tray and receive message information from system.
hIcon is parameter for appear icon on system tray
szTip is parameter for appear icon tips on system tray
And then the most important part is Shell_NotifyIcon(NIM_ADD,@systemTrayIcon); that useful to place application on system tray
To make our application disable on taskbar we must add some line in project part :

begin
Application.Initialize;
Application.ShowMainForm := false; //tambahkan baris ini
Application.CreateForm(TForm1, Form1);
Application.Run;
end.

If you want to disable icon when you close yout application you must add some code below on even OnDestroy at Form1
Shell_NotifyIcon(NIM_DELETE,@systemTrayIcon);

After you finished, now we make our application can run normally or foreground, we can make a procedure,
Example i have declaration procedure gotoTray at private section

procedure gotoTray(pesan :TMessage);
message WM_USER;

if you have been declaration, we must make implementation of procedure gotoTray.
This script below is implementation of procedure gotoTray

procedure TForm1.gotoTray(pesan :TMessage);
begin
case pesan.LParam of
WM_LBUTTONDOWN:
begin
ShowMessage('Form ditampilkan');
Form1.Show;
end;
WM_RBUTTONDOWN:
begin
ShowMessage('Form Menuju System Tray');
Form1.Hide;
end;
end;
end;

No comments: