Solved

How to put Program Icon in the status bar near clock?

Posted on 2004-04-06
10
353 Views
Last Modified: 2010-04-05
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.
0
Comment
Question by:Qosai_DBA
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 10772386
go to
www.torry.net

you may find there some components,
which does this for you

keyword: trayicon

meikl ;-)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 10772394
0
 
LVL 11

Expert Comment

by:ZhaawZ
ID: 10772460
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(TForm1, Form1);
    Application.Run;
  end; {added}
end.

This will not allow to open multiple instances of project.
0
 
LVL 11

Accepted Solution

by:
ZhaawZ earned 50 total points
ID: 10772496
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(TForm1, Form1);
    Application.Run;
  end else begin {added}
    SetForegroundWindow(FindWindow('TForm1',nil)); {added}
  end; {added}
end.

Last 2 of added lines will show the project, if it's already opened.
0
 
LVL 17

Assisted Solution

by:geobul
geobul earned 50 total points
ID: 10772550
Hi,

An example of a tray app without using third-party components (with menu when it's in the tray):

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Menus, ShellApi; // ShellApi has been added manually

type
  TForm1 = class(TForm)
    PopupMenu1: TPopupMenu; // tray menu
    Show1: TMenuItem; // show the app
    Exit1: TMenuItem; // close the app
    procedure ToTray;
    procedure FormCreate(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Show1Click(Sender: TObject);
    procedure Exit1Click(Sender: TObject);
  protected
    procedure WndProc(var Msg : TMessage); override;
  public
    IconNotifyData : TNotifyIconData;
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.ToTray;
begin
  Form1.Hide;
end;

procedure TForm1.WndProc(var Msg : TMessage);
var
  p : TPoint;
begin
  case Msg.Msg of
    WM_USER+1:
    case Msg.lParam of
      WM_RBUTTONUP: begin // popup menu on right-click in the tray
        GetCursorPos(p);
        PopupMenu1.Popup(p.x, p.y);
      end;
      WM_LBUTTONUP: begin // show the app on left-click in the tray
         Form1.Show;
      end;
    end;
  end;
  if (Msg.Msg = WM_SYSCOMMAND) and (Msg.wParam = SC_MINIMIZE) then begin
    ToTray;
    Msg.wParam := 0;
    Msg.lParam :=0;
  end;
  inherited;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  with IconNotifyData do begin
    hIcon :=Application.Icon.Handle;
    uCallbackMessage := WM_USER + 1;
    cbSize := sizeof(IconNotifyData);
    Wnd := Handle;
    uID := 100;
    uFlags := NIF_MESSAGE + NIF_ICON + NIF_TIP;
  end;
  StrPCopy(IconNotifyData.szTip, 'Hint Message'); // <-- modify the message
  Shell_NotifyIcon(NIM_ADD, @IconNotifyData);
end;

procedure TForm1.FormActivate(Sender: TObject);
begin
  ShowWindow(Application.Handle, SW_HIDE);
  SetWindowLong(Application.Handle, GWL_EXSTYLE, WS_EX_TOOLWINDOW);
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Shell_NotifyIcon(NIM_DELETE, @IconNotifyData);
  Application.ProcessMessages;
end;

procedure TForm1.Show1Click(Sender: TObject);
begin
  Form1.Show;
end;

procedure TForm1.Exit1Click(Sender: TObject);
begin
  Close;
end;

end.

Regards, Geo
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 12

Expert Comment

by:Ivanov_G
ID: 10772747
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.
0
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 10772755

   Ou, I forgot something :

   when you destroy / close you should remove it from the systray area :

  procedure TForm1.FormDestroy(Sender: TObject);
  begin
    Shell_NotifyIcon(NIM_DELETE, @TrayIcon);
  end;
0
 
LVL 17

Expert Comment

by:geobul
ID: 10773358
Ivanov_G, what's the difference between your code and mine one ?
0
 
LVL 5

Expert Comment

by:Hypoviax
ID: 10779038
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
0
 
LVL 17

Expert Comment

by:geobul
ID: 10781552
Hypoviax, thank you :-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now