Top.Mail.Ru
Мастера DELPHI, Delphi programming community Титульная страница Поиск, карта сайта Написать письмо 
| Новости | Поиск | FAQ | Статьи | Книги | Новости VCL |
| Форумы ЧАТ | Орешник | KOL и MCK |
Форумы -» Основная («Начинающим»)/ Базы / WinAPI / Компоненты / Сети / Media / Игры / Corba и COM / KOL / FreePascal / .Net / Прочее / RSDN.ORG

« предыдущая ветвь | форум | следующая ветвь »

MainMenu1 нажатие кнопки в меню [D7, NT4, Win2k, WinXP]


buben ©   (2007-03-03 14:29:00) 

Как нажать, нужную мне кнопку, в меню MainMenu1, чужого приложения?

Пример чужого приложения:
unit Unit1;

interface

uses
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
 Dialogs, Menus;

type
 TForm1 = class(TForm)
   MainMenu1: TMainMenu;
   N1: TMenuItem;
   N111: TMenuItem;
   N222: TMenuItem;
   N333: TMenuItem;
   procedure N111Click(Sender: TObject);
   procedure N222Click(Sender: TObject);
   procedure N333Click(Sender: TObject);
 private
   { Private declarations }
 public
   { Public declarations }
 end;

var
 Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.N111Click(Sender: TObject);
begin
showmessage('1');
end;

procedure TForm1.N222Click(Sender: TObject);
begin
showmessage('2');
end;

procedure TForm1.N333Click(Sender: TObject);
begin
showmessage('3');
end;

end.


Нужно нажать на N222 компонента MainMenu1


Leonid Troyanovsky   (2007-03-03 21:55:00) [1]


> buben ©   (03.03.07 21:45) [4]

> Leonid Troyanovsky, дай я тебе расцелую)))

Вообще-то, я лесбиян.

--
Regards, LVT.


buben   (2007-03-03 21:45:00) [2]

Leonid Troyanovsky, дай я тебе расцелую)))


Leonid Troyanovsky   (2007-03-03 20:07:00) [3]


> buben ©   (03.03.07 18:25) [2]

> SendMessage(handle,  так как хэндэл, назначается случайным
> оброзом, при каждом открытии меню он разный.

Это сообщение отсылается окну, чье меню.

См. также
http://groups.google.com/group/fido7.ru.delphi/msg/925bebee0309bec9

--
Regards, LVT.


buben   (2007-03-03 18:25:00) [4]

Лёля, прив, спасиб.
так открывается в своем SendMessage(handle,WM_Command,3,0);

вопрос все еще открыт. Думаю, что это не реально сделать SendMessage(handle,  так как хэндэл, назначается случайным оброзом, при каждом открытии меню он разный.


Лёля   (2007-03-03 15:42:00) [5]

Привет!
Вот кусочек учебно-тренировочного кода, без проверок:

type
 TfrmGetMenuTest = class(TForm)
   btnGetMenu: TButton;
   TreeView1: TTreeView;
   rgWin: TRadioGroup;
   CheckBox1: TCheckBox;
   Memo1: TMemo;
   edtWinClass: TEdit;
   Label1: TLabel;
   procedure btnGetMenuClick(Sender: TObject);
   procedure TreeView1Click(Sender: TObject);
 private
   function GetAppWin: HWND;
   procedure FillChildren(ParentNode: TTreeNode; Menu: hMenu);
 end;

var
 frmGetMenuTest: TfrmGetMenuTest;

implementation

{$R *.dfm}

//получаем меню выбранного окна
procedure TfrmGetMenuTest.btnGetMenuClick(Sender: TObject);
var
 Win: HWND;
 hMnu: HMenu;
begin
 Win := GetAppWin;
 if Win = 0 then
   Exit;
 hMnu := GetMenu(Win);
 TreeView1.Items.BeginUpdate;
 try
   TreeView1.Items.Clear;
   FillChildren(nil, hMnu);
 finally
   TreeView1.Items.EndUpdate;
 end;
end;

//строим дерево меню
procedure TfrmGetMenuTest.FillChildren(ParentNode: TTreeNode; Menu: hMenu);
var
 i, MenuItemCount: Integer;
 NewNode: TTreeNode;
 ItemID: Cardinal;
 ItemSubMenu: HMenu;
 ItemString1: array[0..127] of char;
begin
 MenuItemCount := GetMenuItemCount(Menu);
 for i := 0 to MenuItemCount - 1 do
 begin
   ItemID := GetMenuItemID(Menu, i);
   ItemSubMenu := GetSubMenu(Menu, i);
   GetMenuString(Menu, i, ItemString1, SizeOf(ItemString1), MF_BYPOSITION);  

   NewNode := TreeView1.Items.AddChild(ParentNode, Format('ItemID = %d, «%s» = %d ', [{IntToHex(}ItemID {, 0)}, ItemString1, ItemSubMenu]));
   NewNode.Data := Pointer(ItemID);
   if ItemSubMenu <> 0 then
     FillChildren(NewNode, ItemSubMenu)
 end;
end;

//возвращает хэндл окна (по идее, содержащего меню)
function TfrmGetMenuTest.GetAppWin: HWND;
var
 s: string;
begin
 case rgWin.ItemIndex of
   0: s := 'MSPaintApp';
   1: s := 'TTOTAL_CMD';
   2: s := 'Notepad';
   3: s := edtWinClass.Text;
 end;
 Result := FindWindow(PChar(s), nil);
 if Result = 0 then
   MessageBox(Handle, 'БУ!', '', MB_OK or MB_ICONERROR);
end;

//посылаем команду меню
procedure TfrmGetMenuTest.TreeView1Click(Sender: TObject);
begin
 if (TreeView1.SelectionCount <> 0) and (CheckBox1.Checked) then
   SendMessage(GetAppWin, WM_COMMAND, Integer(TreeView1.Selected.Data), 0)
end;


Удачи!


Наверх

Титульная страница Поиск, карта сайта Написать письмо