Solved

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

Posted on 2004-04-06
10
352 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
Comment Utility
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
Comment Utility
0
 
LVL 11

Expert Comment

by:ZhaawZ
Comment Utility
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
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 12

Expert Comment

by:Ivanov_G
Comment Utility
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
Comment Utility

   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
Comment Utility
Ivanov_G, what's the difference between your code and mine one ?
0
 
LVL 5

Expert Comment

by:Hypoviax
Comment Utility
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
Comment Utility
Hypoviax, thank you :-)
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 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

13 Experts available now in Live!

Get 1:1 Help Now