Piloter une application externe...
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Menus;
 
type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    procedure MenuItemClick(Sender: TObject);
    procedure Ita(Item: TMenuItem; H: HMENU);
  public
    { Déclarations publiques }
  end;
 
var
  Form1: TForm1;
implementation
{$R *.dfm}
uses ShellApi;
var
 H :THandle;
 S:array[0..63] of AnsiChar;
 
procedure TForm1.MenuItemClick(Sender: TObject);
begin
 if GetActiveWindow <> H then
 begin
   Sendmessage(H,WM_SYSCOMMAND,SC_RESTORE,0);
 end;
 Caption:=Format('Item Id:%d',[TComponent(Sender).Tag]);
 Sendmessage(H,WM_SETFOCUS,0,0);
 Sendmessage(H,WM_COMMAND,TComponent(Sender).Tag,0);
 
end;
 
procedure TForm1.Ita(Item:TMenuItem;H:HMENU);
var
 MF: TMenuItemInfo;
 I : integer;
 N : TMenuItem;
begin
  for I:= 0 to GetMenuItemCount(H)-1 do
  begin
        s:='';
        FillChar(MF,SizeOf(TMenuItemInfo),#0);
        with MF do
        begin
          cbSize :=SizeOf(TMenuItemInfo);
          fMask  :=MIIM_TYPE or  MIIM_ID or MIIM_SUBMENU;
          dwTypeData :=@S[0];
          cch        :=64;
        end;
        if GetMenuItemInfo(H,I,true,MF) then
        with MF do
        begin
             N :=TMenuItem.Create(Item);
             Item.Add(N);
             N.Tag     := wID;
             if cch = 0 then
               N.Caption := '-'
             else
               N.Caption := StrPas(dwTypeData);
 
             if hSubMenu = 0 then
                N.OnClick := MenuItemClick
             else
                Ita(N,hSubMenu);
        end;
   end;
end;
 
procedure TForm1.FormCreate(Sender: TObject);
begin
  H:=FindWindow('Notepad',nil);
  if H = 0 then
   ShellExecute(0,'Open','Notepad.exe',nil,nil,1);
  Sleep(1000);
  H:=FindWindow('Notepad',nil);
  SetWindowPos(H,HWND_TOPMOST,0,0,0,0,SWP_NOSIZE);
  Menu :=TMainMenu.Create(Self);
  Ita(Menu.Items,GetMenu(H));
end;
 
end.