1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Buttons, ComCtrls; type TForm1 = class(TForm) TreeView1: TTreeView; BitBtn1: TBitBtn; procedure BitBtn1Click(Sender: TObject); procedure TreeView1DblClick(Sender: TObject); private hnotepad: THandle; procedure AddChildMenus(pnd:TTreenode;m:hMenu); public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.AddChildMenus(pnd:TTreenode;m:hMenu); VAR i,id:integer; nd:TTreenode; MText : ARRAY[0..255] OF Char; begin for i:=0 to GetMenuItemCount(m)-1 do begin Id := Integer(GetMenuItemID(m, i)); GetMenuString(m, i, MText, 255, MF_BYPOSITION); if id = -1 then begin nd:=Treeview1.items.AddChild(pnd, mText); AddChildMenus(nd, GetSubMenu(m, i)); end else if length(Trim(MText)) > 0 THEN Treeview1.items.AddChildObject(pnd, Format('%s {ID=%d}',[MText, Id]), Pointer(Id)); END; END; procedure TForm1.BitBtn1Click(Sender: TObject); var root:TTreenode; begin hnotepad:=FindWindow(nil, '记事本'); if hnotepad <> 0 then if GetMenu(hnotepad) <> 0 then begin root:=Treeview1.items.Add(nil, 'Main Menu'); AddChildMenus(root, GetMenu(hnotepad)); Treeview1.FullExpand; Refresh; end; end; procedure TForm1.TreeView1DblClick(Sender: TObject); begin BringWindowToTop(hNotePad); PostMessage(hNotePad,WM_COMMAND,Integer(Treeview1.Selected.Data), 0); end; end. |
Use Delphi to Get external program's menu and simulate clicking on it
分类:Delphi
阅读 (2,052)
No Responses »
7月 102012