artykuły

Jak "kliknąć" polecenie w menu innego programu?

22:56
Fri, 26 June 2015
Musimy posłużyć się funkcjami WinAPI, aby uzyskać wymagany przez nas efekt. Spójrzmy na poniższy kod:
function GetMenuItemCaption(const hSubMenu: HMENU; const Id: Integer): string; var MenuItemInfo: TMenuItemInfo; begin MenuItemInfo.cbSize := 44; // Required for Windows 95. not sizeof(AMenuInfo) MenuItemInfo.fMask := MIIM_STRING; // to get the menu caption, 1023 first chars should be enough SetLength(Result, 1023 + 1); MenuItemInfo.dwTypeData := PChar(Result); MenuItemInfo.cch := Length(Result)-1; if not GetMenuItemInfo(hSubMenu, Id, False, MenuItemInfo) then begin // RaiseLastOSError; Exit; end; // real caption's size. Should call GetMenuItemInfo again if was too short SetLength(Result, MenuItemInfo.cch); {$WARN SYMBOL_PLATFORM OFF} if DebugHook > 0 then OutputDebugString(MenuItemInfo.dwTypeData); end;
function executeCommand(className, commandName : string) : boolean; var hwindow, hmenu : THandle; hsubmenu : Windows.HMENU; z, k,i,id : cardinal; menuCaption : string; begin Result := False; hwindow := FindWindow(nil, PChar(className)); if (hwindow = 0) then hwindow := FindWindow(PChar(className), nil); if (hwindow = 0) then begin Result := False; raise EClassNotFound.Create('Okno o podanym tytule lub klasie nie zostało znalezione.'); end; hmenu := GetMenu(hwindow); if not IsMenu(hmenu) then begin raise Exception.Create('Okno zostało znalezione, lecz wydaje się nie posiadać menu.'); Exit; end; for k:=0 to GetMenuItemCount(hmenu)-1 do begin hsubmenu := GetSubMenu(hmenu, k); if not IsMenu(hsubmenu) then begin raise Exception.Create('Okno i jego menu główne zostało znalezione, lecz wydaje się że to menu nie posiada submenu.'); Exit; end; for i:=0 to GetMenuItemCount(hsubmenu)-1 do begin id := GetMenuItemID(hsubmenu, i); menuCaption := GetMenuItemCaption(hsubmenu,id); for z:=1 to Length(menuCaption) do begin if (menuCaption[z] = '&') then Delete(menuCaption, z, 1); end; if (menuCaption = commandName) then begin PostMessage(hwindow, WM_COMMAND, id, 0); Result := True; Exit; end; end; end; end;
Posługujemy się nim tak: executeCommand('Tytuł okna programu lub nazwa klasy okna', 'Etykieta tekstowa polecenia w menu');

Podobne faq:

Skomentuj

Aby zamieścić komentarz, proszę włączyć JavaScript - niestety roboty spamujące dają mi niezmiernie popalić.






Komentarze czytelników

    Nie ma jeszcze żadnych komentarzy.
    Dexter