Author: Andreas Heidenreich
How can I Delete Menu Items in Applications other than my own?
Answer:
Solve 1:
When You want to control an external Application via OLE, you sometimes have to
remove some Menue-Points.
Generally, this is possible by putting the OLE-Application into a OLE-Container
instead of opening it off-site. In an OLE-Container, the application tries to open
its own Menu. When You now put a menu onto Your own application, OLE mixes the
menues. Every second main menu-point from the external application is visible, the
first, third, fifth ... menu-point is replaced by the menu You have written. In
this way, You can write for example Your own FILE-menu for WORD. Now you just have
to align the OLE-Container to Your form and make the form resizable, then You have
Word customized as You wanted it.
Solve 2:
To Create a WORD-Like Look-and-feel in Your application, You have to follow these
steps:
Create a new Form
insert an OLE-Container (NOT the WORD OLE-Object You have shipped with
Delphi!!!)and Set following properties: Align: alClient, AutoActivate: aaManual,
AllowInPlace : TRUE, AllowActiveDoc: True.
Insert a Menu and fill the first menu point with 'file' and the sub-menue with the
approptiate Items. Now You will have a Listing like that:
1 type
2 TOLE_Testform = class(TForm)
3 OleContainer1: TOleContainer;
4 MainMenu1: TMainMenu;
5 DocOpenDialog: TOpenDialog;
6 File1: TMenuItem;
7 New1: TMenuItem;
8 Open1: TMenuItem;
9 Save1: TMenuItem;
10 Exit1: TMenuItem;
11 private
12 { Private-Deklarationen }
13 public
14 { Public-Deklarationen }
15 end;
When You now run the program, You will just see Your 'File'-Menu.
Now add OnClick-Events for each menu item.
For Open, it will go like this:
16 procedure TOLE_Testform.Open1Click(Sender: TObject);
17 var
18 continue: boolean;
19 filename: string;
20 begin
21 continue := DocOpenDialog.Execute();
22 if continue = TRUE then
23 begin
24 filename := DocOpenDialog.Filename;
25 // OleContainer1.DoVerb(ovShow);
26 OleContainer1.DestroyObject;
27 OleContainer1.CreateObjectFromFile(filename, FALSE);
28 OleContainer1.DoVerb(ovShow);
29 end;
30 end;
31
32 //CreateObjectFromFile will create the appropriate OLE-Object for the given file
33 extension, e.g. WORD for *.DOC.
34
35 //A new document You will get from this statement:
36
37 procedure TOLE_Testform.New1Click(Sender: TObject);
38 begin
39 OleContainer1.DestroyObject;
40 OleContainer1.CreateObject('WORD.Document', FALSE);
41 OleContainer1.DoVerb(ovShow);
42 end;
When you opened an existing or created a new document, You will see that the
default WORD file menu has been replaced by Your own. And now You can go on
experimenting... ;-))
Solve 3:
I've been playing around. Trying to make a menu/menuitem in notepad. And I've
succeded. But sadly I can not work out how to tell when the menu/menuitem is
clicked. The two methods I've used to add the menuitem are listed now
43 var
44 mnuItem: TMenuItem;
45 mnuItemHandle: hmenu;
46 notepad: hwnd;
47 notepadmenu: hmenu;
48 menucount: integer;
49 MenuItemInfo: TMenuItemInfo;
50
51 {Method 1}
52
53 procedure TForm1.AddMenu1;
54 notepad := findwindow('Notepad', 'Untitled - Notepad');
55 notepadmnu := getmenu(notepad);
56 mnuItem := TMenuItem.Create(self);
57 mnuitem.Caption := 'Test';
58 mnuItemHandle := mnuitem.handle;
59 appendmenu(notepadmenu, 0, mnuitemhandle, pchar(mnuitem.caption));
60 end;
61
62 {Method 2}
63
64 procedure TForm1.AddMenu2;
65 notepad := findwindow('Notepad', 'Untitled - Notepad');
66 notepadmnu := getmenu(notepad);
67 menucount := getmenuitemcount(notepadmnu);
68 MenuItemInfo.fMask := MIIM_DATA + MIIM_TYPE + MIIM_STATE + MIIM_ID;
69 MenuItemInfo.fType := MFT_STRING;
70 MenuItemInfo.fState := MFS_ENABLED;
71 MenuItemInfo.wID := 2000;
72 MenuItemInfo.dwTypeData := '!CLICK ME!';
73 MenuItemInfo.cch := 10;
74 MenuItemInfo.cbSize := sizeof(MenuItemInfo);
75 insertmenuitem(notepadmnu, c + 1, true, MenuItemInfo);
76 end;
|