Solved

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

Posted on 2004-04-06
10
359 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
Industry Leaders: 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

679 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