Solved

system tray

Posted on 2000-04-14
1
305 Views
Last Modified: 2010-04-04
how to start program in system tray?
0
Comment
Question by:Jurica2505
1 Comment
 
LVL 17

Accepted Solution

by:
inthe earned 20 total points
Comment Utility
some code to do it:

const
  WM_MYTRAYICONCALLBACK = WM_USER + 1000;

type
  TForm1 = class(TForm)
    PopupMenu1 : TPopupMenu;
    Close1     : TMenuItem;
    Restore1   : TMenuItem;
    Timer1     : TTimer;
    procedure Close1Click(Sender: TObject);
    procedure Restore1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    { Private declarations }
    HIcon1, HIcon2 : HICON;
    MyTrayIcon: TNotifyIconData;
    procedure AppMinimize(Sender:TObject);
    procedure WMMyTrayIconCallback(var Msg:TMessage);
                       message WM_MYTRAYICONCALLBACK;
  public
    { Public declarations }
  end;

{...}

procedure TForm1.FormCreate(Sender: TObject);
begin
  { load our icons }
  HIcon1 := LoadIcon(0, IDI_WINLOGO);
  HIcon2 := LoadIcon(0, IDI_HAND);

  { set TApplictaion.OnMinimize to our new event }
  Application.OnMinimize := AppMinimize;

  { start our application minimized to the System Tray }
  Visible := FALSE;
  Application.ShowMainForm := Visible;

  { required to set cbSize to SizeOf our structure }
  MyTrayIcon.cbSize := SizeOf(TNotifyIconData);

  { handle to main window }
  MyTrayIcon.Wnd    := Handle;

  { identifier of our icon }
  MyTrayIcon.uId    := 1;

  { flags showing what we are using in the other members below }
  MyTrayIcon.uFlags := NIF_ICON or NIF_TIP or NIF_MESSAGE;

  { our user defined message }
  MyTrayIcon.uCallBackMessage := WM_MYTRAYICONCALLBACK;

  {  set our icon to the first one loaded above }
  MyTrayIcon.hIcon  := HIcon1;

  { help message of the icon }
  MyTrayIcon.szTip  := 'Lou''s Delphi Tip of the Day [MyTrayIcon]';

  { add our icon to the tray }
  Shell_NotifyIcon(NIM_ADD, @MyTrayIcon);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  { remove our icon from the tray }
  Shell_NotifyIcon(NIM_DELETE, @MyTrayIcon);
end;

procedure TForm1.AppMinimize(Sender:TObject);
begin
  { hide our application }
  Visible := FALSE;
  Application.ShowMainForm := Visible;
end;

procedure TForm1.WMMyTrayIconCallback(var Msg:TMessage);
var
  CursorPos : TPoint;
begin
  case Msg.lParam of
    WM_LBUTTONDOWN :
      begin
        { get mouse cursor position }
        GetCursorPos(CursorPos);
        { popup our TPopupMenu }
        PopupMenu1.Popup(CursorPos.X,CursorPos.Y);
      end;
  end;
end;

procedure TForm1.Close1Click(Sender: TObject);
begin
  { close our application }
  Close;
end;

procedure TForm1.Restore1Click(Sender: TObject);
begin
  { restore our application, or minimize if already
    in a restore mode }
  Visible := TRUE;
  Application.ShowMainForm := Visible;
  SetForegroundWindow(Application.Handle);
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  { change our icon to the opposite one loaded }
  if MyTrayIcon.hIcon = HIcon1 then
    MyTrayIcon.hIcon := HIcon2
  else
    MyTrayIcon.hIcon := HIcon1;

  { tell the SystemTray to modify our changes }
  Shell_NotifyIcon(NIM_MODIFY, @MyTrayIcon);
end;







or go to www.delphipages.com and search for "trayicon" to get a component to do it.

Regards Barry
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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…
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

771 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

15 Experts available now in Live!

Get 1:1 Help Now