?
Solved

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

Posted on 2004-04-06
10
Medium Priority
?
362 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 11

Accepted Solution

by:
ZhaawZ earned 200 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 200 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses
Course of the Month15 days, 14 hours left to enroll

741 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