Solved

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

Posted on 2004-04-06
10
360 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

737 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