Solved

Help Needed creating a Popup in the Icon Tray

Posted on 2004-03-24
9
238 Views
Last Modified: 2010-04-05
I have created an application that runs in the Icon Tray, that when clicked, a Menu Appears with applications that I wish to Run.

This works well.

I now want to modify the Application (Version 2), where my application reads the Following INI File.

[App1]
AppDesc='Run App1'
AppPath='D:\My Apps\App1.exe'

[App2]
AppDesc='Run App2'
AppPath='D:\My Projects\Projects\App2.exe'

[App3]
AppDesc='Run App3'
AppPath='D:\My Projects\App3\App3.exe'

[App4]
AppDesc='Run App4'
AppPath='D:\My Projects\App4\App4.exe'


I want the Application to then Create a Menu Item for each AppDesc, and then allows the user to EXECUTE the application contained within the AppPath.


I basically have the following code, my previous application had 4 procedures
    MyPopupHandler1(Sender: TObject);
    MyPopupHandler2(Sender: TObject);
    MyPopupHandler3(Sender: TObject);
    MyPopupHandler4(Sender: TObject);
Which were hardcoded with the Application Path and EXE Name and all worked well

type
  TForm1 = class(TForm)
    abcTrayIcon1: TabcTrayIcon;
    abcSingle: TabcSingleInstance;
    mPopup: TPopupMenu;
    procedure FormCreate(Sender: TObject);
    procedure MyPopupHandler(Sender: TObject);
    procedure ExitApp(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
  MyItem: array[0..4] of TMenuItem;
  i: Integer;
  SystemIni: TIniFile;
  secList: TStrings;
begin
  secList:=TStringList.Create;

  SystemIni := TIniFile.Create('C:\data\Popup.ini');
  SystemIni.ReadSections(secList);

  for I := 0 to secList.Count - 1 do begin    // Iterate
      MyItem[i] := TMenuItem.Create(Self);
      MyItem[i].Caption := SystemIni.ReadString(secList.Strings[I],'AppDesc','-');
      appPath := SystemIni.ReadString(secList.Strings[I],'AppPath','-');
      MyItem[i].OnClick := MyPopupHandler;
      mPopup.Items.Add(MyItem[i]);
  end;    // for
  MyItem[i+1] := TMenuItem.Create(Self);
  MyItem[i+1].Caption := 'Exit';
  MyItem[i+1].OnClick := ExitApp;
  mPopup.Items.Add(MyItem[i+1]);
end;

procedure TForm1.MyPopupHandler(Sender: TObject);
begin
  WinExec(appPath,SW_SHOW);    // application Path and EXE here
end;

procedure TForm1.ExitApp(Sender: TObject);
begin
  Application.Terminate;
end;


Any help would be greatly appreciated

Thanks
PE
0
Comment
Question by:pjelias
  • 5
  • 3
9 Comments
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10674439
OK, here is what i came up with:

I added a main section with a count to my inifile


[MAIN]
COUNT= 3
[App1]
AppDesc='Preso'
AppPath='C:\Program Files\Borland\Delphi7\Projects\Preso\Preso.exe'

[App2]
AppDesc='GCM'
AppPath='C:\Program Files\Borland\Delphi7\Projects\GCM\GCM.exe'

[App3]
AppDesc='Poker'
AppPath='C:\Program Files\Borland\Delphi7\Projects\Poker\Project1'


you will need this to execute the applications

uses
  ShellAPI;


//this is the OnClick event you will assign to each menuitem


procedure TForm1.MyCLick(Sender: TObject);
var
 IFile: TIniFile;
 IApp: String;
begin
 //open ini file
 IFile:= TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini'));
//get menu item path
 IApp:= IFile.ReadString('APP' + IntToStr(TMenuItem(Sender).MenuIndex),'AppPath','');
//execute menu item
 ShellExecute(Handle, 'open', PChar(IApp), nil, nil, SW_SHOWNORMAL);
end;

//create menu items on form startup

procedure TForm1.FormActivate(Sender: TObject);
var
 IFile: TIniFile;
 MItem : TMenuItem;
 I, Count: Integer;
 IName: String;
begin
//open inifile
 IFile:= TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini'));
//get count
 Count:= IFile.ReadInteger('MAIN','COUNT',0);
 for I:= 1 to Count do
 begin
 //get app name
  IName:= IFile.ReadString('APP' + IntToStr(I),'AppDesc','');
  MItem := TMenuItem.Create(PopupMenu1);
  MItem.Caption:= IName;
  MItem.OnClick := MyCLick;
  PopupMenu1.Items.Add(MItem);
 end;
end;

end.

Hope this helps

Shane
0
 
LVL 33

Accepted Solution

by:
Slick812 earned 125 total points
ID: 10674520
hello pjelias, I would use the Hint property of the menu Item to store the App Path for each Memu Item-
you can try this code for a button click


procedure TForm1.MyPopupHandler(Sender: TObject);
begin
if (Sender as TMenuItem).Hint <> '-' then
  WinExec(PChar((Sender as TMenuItem).Hint),SW_SHOW);
// by casting the Sender to a menu Item you can just have ONE procedure for ALL of your memu Itens
end;

procedure TForm1.sbut_ReadIniClick(Sender: TObject);
var
SystemIni: TIniFile;
secList: TStringList;
MyItem: TMenuItem;
appPath: String;
i: Integer;
begin
SystemIni := TIniFile.Create('E:\Popup.ini');
secList:=TStringList.Create;
SystemIni.ReadSections(secList);
for i := 0 to secList.Count - 1 do
  begin
  MyItem := TMenuItem.Create(PopUpMenu1); //  I use only ONE menu Item variable for all of the items
  MyItem.Caption := SystemIni.ReadString(secList.Strings[i],'AppDesc','Run '+secList.Strings[i]);
  MyItem.Hint := SystemIni.ReadString(secList.Strings[i],'AppPath','-');
  MyItem.OnClick := MyPopupHandler; // all of the Items will use the same click procedure
  PopUpMenu1.Items.Add(MyItem);
  end;
  MyItem := TMenuItem.Create(PopUpMenu1);
  MyItem.Caption := 'Exit';
  MyItem.OnClick := ExitApp;
  PopUpMenu1.Items.Add(MyItem);
PopupMenu1.Popup(300,300);
end;


 - - - - - - - - - - - - - - - - - - - - - - - - - -

ask questions if you need more info
0
 
LVL 33

Expert Comment

by:Slick812
ID: 10674589
? ?
I forgot the

FreeAndNil(SystemIni);
FreeAndNil(secList);

and the
appPath: String;    is not used
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10681740
What...............

May I ask what was wrong with my answer...?

Shane
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 11

Expert Comment

by:shaneholmes
ID: 10681762
Hey Slick812, thanks for stepping on me?

Shane
0
 

Author Comment

by:pjelias
ID: 10681974
Shane,

I probably could have split the points, I used Slick182's because the code looked a bit "cleaner" ie. didn't need to modify INI - Add Count, PopUpHandler (MyClick) Used 1 line instead of 3 etc.....

My apologies
PE
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10682050

You got to be kidding me, you couldn't tell he ran with my code..... beautifying it up.

Oh well, some people just don't get it.

Good luck!

Shane
0
 
LVL 33

Expert Comment

by:Slick812
ID: 10684377
shaneholmes, I did my code compleatly independently of anything you have ever posted here, the creative part of my code is using the hint for the extra string for appPath, where in your code is that? And on your click event, why do you do something as ineficient as reading the ini file a second time?
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10686841
im not gonna give you the satifisfaction of answering you slick218 - enjoy the points and have a good day - end of subject!

Shane
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
delphi exception 7 63
Correct Component for Shopping Cart. 2 96
Find and Replace Stream with 0s 8 57
Unique identifier on a terminal server (rdp) 4 46
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…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

861 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

30 Experts available now in Live!

Get 1:1 Help Now