Solved

TPopupmenu (Very difficult 'i think)

Posted on 1998-12-02
9
321 Views
Last Modified: 2011-04-14
Hi!!
I have 1. Ini File, and with this ini file i want to do this.. I like to add all of ini string into popupmenu item
,when i have do that i will push the right mouse button and the popup menu will appear with items of all string in ini file.. and now when i click any item of popupmenu item i will then execute a program such a notepad
Example of my ini file
[Programs]
Notepad=C:\Windows\notepad.exe
i like to execute the notepad from popupmenu
And How? Thanks...
0
Comment
Question by:emiltor
9 Comments
 
LVL 10

Expert Comment

by:viktornet
ID: 1348991
to execute the program just do something like this...

ShellExecute(0, 'open', PChar(PMnu.Items.Strings[PMnu.ItemIndex]), nil, nil, SW_NORMAL);

To load the strings to a opo up menu simply create items of type TMenuItem and insert them in the pop up menu...

To show the popup menu you could do spomething like PopUpMenu.PopUp; and maybe you'd have to give some x, and y values as the coordinates... Hope this info helps//

Cheers,
Viktor
0
 
LVL 4

Expert Comment

by:erajoj
ID: 1348992
Are you satisfied with Viktors answer or do you need sample code?
Just a comment; instead of entering "Notepad=C:\Windows\notepad.exe" in the ini-file, enter something like "1=&Notepad,C:\Windows\notepad.exe". Then you will be able to use custom shortcut keys in the popup menus using the ampersand(&).

/// John
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1348993
Oh shoot... I didn't mean to answer this... **** Please reject my answer.. I did it in my break in school so I didn't really noticed I was answering the question... That's not an answer at all..that's just to get you started....

Cheers,
Viktor
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:emiltor
ID: 1348994
i´m finish doing this..... add all of ini file to popupmenu item. And now lets say the 1. caption in popupmenu item is notepad, then i will get the url behind that caption so i can execute notepad. Is this do on onchange event in popupmenu( i´m stop with this)
Thanks......
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1348995
this is in OnClick() if i understand yuor q'n right.,...
0
 
LVL 3

Expert Comment

by:williams2
ID: 1348996
Maybe this would help you.

You can use the Item.Tag property to set a specific index in a list. Then you should assign the OnChange event on all items to some procedure TForm1.BlaBlaOnChange(Sender: TObject)
(Do it with item.OnChange:=BlaBlaOnChange;)

Then you will be able to read the tag again with:

TForm1.BlaBlaOnChange(Sender: TObject);
var
  Index: Integer;
Begin
  index:= TMenuItem(Sender).Tag;
  //Here you should index the item in the list, and then go to execute the thing
End;

Cheers
/Williams
0
 

Author Comment

by:emiltor
ID: 1348997
procedure TAGluggi.FormCreate(Sender: TObject);
Var i                  : Integer;
    Men3,MenPassword      : TMEnuItem;
    Section,Fname,Furl : String;
begin
Ini := TIniFile.Create('D:\Down2\emmi.ini');
i := 0;
While popupmenu1.Items.Count > 0 Do
popupmenu1.Items.Delete(0);
Repeat
      Section := 'URL'+IntToStr(i);
      FName := Ini.ReadString(Section,'NAME','');
      FUrl := Ini.ReadString(Section,'URL','');
If FName <> '' Then
   Begin
   ListBox1.Items.Add(FName);
   ListBox2.Items.Add(FUrl);
   End;
   Inc(i);
Until FName = '';
For i := 0 to listbox1.Items.Count - 1 do
Begin
Men3 := TMenuItem.Create(Self);MenPassword := TMenuItem.Create(Self);
Men3.Caption := listbox1.Items.Strings[i];
Popupmenu1.Items.add(Men3);
End;
MenPassword.Caption := '&Password';
PopupMenu1.Items.Add(MenPassword);
PopupMenu1.Items[0].Tag := 5;
End;

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

procedure TAGluggi.PopupMenu1Change(Sender: TObject; Source: TMenuItem;
  Rebuild : Boolean);
var
    Index : Integer;
Begin
    Index:= TMenuItem(Sender).Tag;
    If Index = 5 Then
    AGluggi.Close;
    //Here you should index the item in the list, and then go to execute the thing


Here are text of my code

Let's say for example, i like to add to the form.caption any of my popupmenu item "click"
text there...Thanks
0
 

Author Comment

by:emiltor
ID: 1348998
procedure TAWindow.FormCreate(Sender: TObject);
Var i                  : Integer;
    Men3,MenPassword      : TMEnuItem;
    Section,Fname,Furl : String;
begin
Ini := TIniFile.Create('D:\Down2\emmi.ini');
i := 0;
While popupmenu1.Items.Count > 0 Do
popupmenu1.Items.Delete(0);
Repeat
      Section := 'URL'+IntToStr(i);
      FName := Ini.ReadString(Section,'NAME','');
      FUrl := Ini.ReadString(Section,'URL','');
If FName <> '' Then
   Begin
   ListBox1.Items.Add(FName);
   ListBox2.Items.Add(FUrl);
   End;
   Inc(i);
Until FName = '';
For i := 0 to listbox1.Items.Count - 1 do
Begin
Men3 := TMenuItem.Create(Self);MenPassword := TMenuItem.Create(Self);
Men3.Caption := listbox1.Items.Strings[i];
Popupmenu1.Items.add(Men3);
End;
MenPassword.Caption := '&Password';
PopupMenu1.Items.Add(MenPassword);
PopupMenu1.Items[0].Tag := 5;
End;

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

procedure TAWindow.PopupMenu1Change(Sender: TObject; Source: TMenuItem;
  Rebuild : Boolean);
var
    Index : Integer;
Begin
    Index:= TMenuItem(Sender).Tag;
    If Index = 5 Then
    AWindow.Close;
    //Here you should index the item in the list, and then go to execute the thing
   // This is from william2


Here are text of my code

Let's say for example, i like to add to the form.caption any of my popupmenu item "click"
text there...Thanks
0
 

Accepted Solution

by:
JStan earned 100 total points
ID: 1348999
{This is what I use to write filenames to the registry. You could easily change this
code to write to an '.ini' file instead.
You may be able to delete some of this code. Some of it is necessary to my prog
because the user can add programs to the list.
Also, I hope all the necessary code is here. I extracted it from 3000 lines of
code and it is possible that I may have missed some.
The code reads filenames from the reg and adds a hotkey to the menu item ( ie: &1,&2 etc.)
Then the code removes the hotkey part of the menu caption before trying to run the program.}
Also, the menu item shows only the Filename minus the .exe It just looks better.
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////

procedure TForm18.FormCreate(Sender: TObject);
var
Buffer: Array[0..255] of Char;
C,I: Integer;
W,F,S: string;
N: TMenuItem;
begin
      
      GetWindowsDirectory(Buffer,255);
           W := StrPas(Buffer);
           Externals := TStringList.Create;
      Externals.Sorted := True;
      Externals.Duplicates := dupIgnore;
      E  := TRegIniFile.Create('SoftWare\Company\Program');
      E.WriteInteger('Externals','0',5);                        //Write files to the reg
      E.WriteString('Externals','1',W+'\Explorer.exe');      //This could also go in
      E.WriteString('Externals','2',W+'\Calc.exe');          //a close procedure
      E.WriteString('Externals','3',W+'\Charmap.exe');      
      E.WriteString('Externals','4',W+'\PBrush.exe');
      E.WriteString('Externals','5',W+'\Notepad.exe');
      E.Free;
      E  := TRegIniFile.Create('SoftWare\Company\Program');  //Read files from the reg
      C := E.ReadInteger('Externals', IntToStr(0),0);
       if C > 0 then
          begin
               for I := 1 To C do
                begin
                     F := E.ReadString('Externals', IntToStr(I), 'Error');
                     if FileExists(ExpandFileName(F)) then
                     Externals.Add(F);                       //Populate the stringlist
              end;
         end;
      if PopupMenu1.Items.Count > 0 then                    //I use the popup over and
     for I := PopupMenu1.Items.Count - 1 downto 0 do         //again so lets clear it
        PopupMenu1.Items[I].Free;                            //from the last use
     if Externals.Count > 0 then
        for I := 1  To Externals.Count do                    //Now lets populate the
            begin                                            //popup using the stringlist
                 N := TMenuItem.Create(Self);
                 S := ExtractFileName(Externals[I-1]);
                 Delete(S,Pos('.',S),4);                     //set up the OnClick event
                 N.Caption := '&' + I + '  ' + S;            //Add a hotkey
                 N.OnClick := RunButtonClick;
                 N.Tag := I;
                 PopupMenu1.Items.Add(N);                    //Add to the popup
            end;

      E.Free;
end;

procedure TForm18.RunButtonClick(Sender: TObject);
var
   Command: string;
   D,E: string;
   I: Integer;
begin
   D := TMenuItem(Sender).Caption;                      //Get the menu caption
   Delete(D,1,4);                                       //Delete the Hotkey  
   for I := 0 to Externals.Count - 1 do                 //
       begin                                            //
            E := ExtractFileName(Externals[I]);         //Compare
            Delete(E,Pos('.',E),4);                     //       with
       if AnsiCompareFileName(E,D) = 0 then             //            the
          begin                                         //               Externals
               D := Externals[I];                       //assign filename to 'D'
               Break;
          end;
       end;
   Command := D + #0;
   if Winexec(@Command[1], SW_SHOWNORMAL) < 32 then     //Try to Run the program
      MessageDlg('Failed to execute ' + D,              //Or error out
      mtError,[mbOK], 0);
end;


             
             
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

789 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