Solved

TPopupmenu (Very difficult 'i think)

Posted on 1998-12-02
9
310 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:emiltor
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 10

Expert Comment

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

Expert Comment

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

762 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

11 Experts available now in Live!

Get 1:1 Help Now