Qosai_DBA
asked on
How to put Program Icon in the status bar near clock?
Hi all,
I have a pragram that manage frequently background process, and I want to start the program and show an Icon near clock, same as Messenger for example.
also
I want to know how to prevent the user to run the program in case of Its already running?
thanks,
Khalid.
I have a pragram that manage frequently background process, and I want to start the program and show an Icon near clock, same as Messenger for example.
also
I want to know how to prevent the user to run the program in case of Its already running?
thanks,
Khalid.
Project --> Show Source
There will be some lines about your Project. Just add some lines there:
program Project1;
uses
Windows, {added}
Forms,
Unit1 in 'Unit1.pas' {Form1};
{$R *.res}
begin
if CreateMutex(nil, true, 'ProjectAlreadyOpened') <> 0 then {added}
if GetLastError <> ERROR_ALREADY_EXISTS then begin {added}
Application.Initialize;
Application.CreateForm(TFo rm1, Form1);
Application.Run;
end; {added}
end.
This will not allow to open multiple instances of project.
There will be some lines about your Project. Just add some lines there:
program Project1;
uses
Windows, {added}
Forms,
Unit1 in 'Unit1.pas' {Form1};
{$R *.res}
begin
if CreateMutex(nil, true, 'ProjectAlreadyOpened') <> 0 then {added}
if GetLastError <> ERROR_ALREADY_EXISTS then begin {added}
Application.Initialize;
Application.CreateForm(TFo
Application.Run;
end; {added}
end.
This will not allow to open multiple instances of project.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
shellapi, Menus;
const
WM_SYSTRAYCLICK = WM_USER + 1;
type
TForm1 = class(TForm)
PopupMenu1: TPopupMenu;
File1: TMenuItem;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
TrayIcon : TNotifyIconData;
procedure InitNotifyIconData;
procedure OnSysTrayClick(var Msg : TMsg); message WM_SYSTRAYCLICK;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
{ TForm1 }
procedure TForm1.InitNotifyIconData;
begin
with TrayIcon do
begin
cbSize := sizeof(TrayIcon);
Wnd := Handle;
uID := 1;
uFlags := NIF_MESSAGE + NIF_ICON + NIF_TIP;
uCallbackMessage := WM_SYSTRAYCLICK;
hIcon := Application.Icon.Handle;
StrPCopy(szTip, Application.Title);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
InitNotifyIconData;
Shell_NotifyIcon(NIM_ADD, @TrayIcon);
end;
procedure TForm1.OnSysTrayClick(var Msg: TMsg);
var Crs : TPoint;
begin
// check which button was clicked
case Msg.wParam of
WM_LBUTTONDBLCLK : begin
// double click in the tray - left mouse
Form1.Show;
end;
WM_RBUTTONDOWN : begin
// right button - show the popup menu
GetCursorPos(Crs);
PopupMenu1.Popup(Crs.X, Crs.Y);
end;
end;
end;
end.
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
shellapi, Menus;
const
WM_SYSTRAYCLICK = WM_USER + 1;
type
TForm1 = class(TForm)
PopupMenu1: TPopupMenu;
File1: TMenuItem;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
TrayIcon : TNotifyIconData;
procedure InitNotifyIconData;
procedure OnSysTrayClick(var Msg : TMsg); message WM_SYSTRAYCLICK;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
{ TForm1 }
procedure TForm1.InitNotifyIconData;
begin
with TrayIcon do
begin
cbSize := sizeof(TrayIcon);
Wnd := Handle;
uID := 1;
uFlags := NIF_MESSAGE + NIF_ICON + NIF_TIP;
uCallbackMessage := WM_SYSTRAYCLICK;
hIcon := Application.Icon.Handle;
StrPCopy(szTip, Application.Title);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
InitNotifyIconData;
Shell_NotifyIcon(NIM_ADD, @TrayIcon);
end;
procedure TForm1.OnSysTrayClick(var Msg: TMsg);
var Crs : TPoint;
begin
// check which button was clicked
case Msg.wParam of
WM_LBUTTONDBLCLK : begin
// double click in the tray - left mouse
Form1.Show;
end;
WM_RBUTTONDOWN : begin
// right button - show the popup menu
GetCursorPos(Crs);
PopupMenu1.Popup(Crs.X, Crs.Y);
end;
end;
end;
end.
Ou, I forgot something :
when you destroy / close you should remove it from the systray area :
procedure TForm1.FormDestroy(Sender:
begin
Shell_NotifyIcon(NIM_DELET
end;
Ivanov_G, what's the difference between your code and mine one ?
geobul's code works well. I accepted his answer to a similar question a while ago using the same code. For the traybar part of the question his works brilliantly
Regards,
Hypoviax
Regards,
Hypoviax
Hypoviax, thank you :-)
www.torry.net
you may find there some components,
which does this for you
keyword: trayicon
meikl ;-)