2009-04-08 5 views

Répondre

3

Le code suivant dans Python active l'élément de menu Affichage/Barre d'état. Vous ne devriez avoir aucun problème à le convertir en Delphi car il ressemble à un pseudocode. Il sélectionne les 4èmes éléments du menu ("Vue") et le 1er élément ("Barre d'état"). Si vous le souhaitez, vous pouvez le modifier pour rechercher l'élément souhaité par le texte en parcourant les éléments et en utilisant GetMenuString. Voir le MSDN pour plus de détails.

Notez qu'il ne vérifie pas les erreurs. Notez également qu'il s'attend à ce que le titre du Bloc-notes soit «Sans titre - Bloc-notes». (Vous pouvez changer cela à None pour chercher quoi que ce soit, je pense que ce serait nil à Delphes.)

from win32gui import * 
from win32con import * 
hwnd = FindWindow('Notepad', 'Untitled - Notepad') # use Winspector Spy to find window class name and title 
hmenu = GetMenu(hwnd) 
hviewmenu = GetSubMenu(hmenu, 3)     # 3rd menu item across, starting from 0 
id = GetMenuItemID(hviewmenu, 0)     # 0th menu item down ("Status Bar") 
PostMessage(hwnd, WM_COMMAND, id, 0) 
2

Et voici un code Delphi. Sachez que cela ne fonctionnerait pas si vous n'avez pas de vrais menus.
"GetMenu ne fonctionne pas sur les barres de menus flottantes.Les barres de menus flottantes sont des commandes personnalisées qui imitent les menus standard, ce ne sont pas des menus.Pour obtenir le handle sur une barre de menus flottante, utilisez les API Active Accessibility.

Par exemple, il ne fonctionne pas avec Delphi lui-même ...

// Grab sub menu for a Window (by handle), given by (0 based) indices in menu hierarchy 
function GetASubmenu(const hW: HWND; const MenuInts: array of Integer): HMENU; 
var 
    hSubMenu: HMENU; 
    I: Integer; 
begin 
    Result := 0; 
    if Length(MenuInts) = 0 then 
    Exit; 

    hSubMenu := GetMenu(hW); 
    if not IsMenu(hSubMenu) then 
    Exit; 

    for I in MenuInts do 
    begin 
    Assert(I < GetMenuItemCount(hSubMenu), format('GetASubmenu: tried %d out of %d items',[I, GetMenuItemCount(hSubMenu)])); 
    hSubMenu := GetSubMenu(hSubMenu, I); 
    if not IsMenu(hSubMenu) then 
     Exit; 
    end; 

    Result := hSubMenu; 
end; 

// Get the caption for MenuItem ID 
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 
    RaiseLastOSError; 
    // 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; 

procedure Test; 
var 
    hwnd, hSubMenu: Cardinal; 
    id : Integer; 
begin 
// hwnd := FindWindow('Afx:00400000:8:00010013:00000000:03F61829', nil); // UltraEdit 
// hSubMenu := GetASubmenu(hwnd, [5,0]); 
    hwnd := FindWindow('Notepad', nil); // get the 1st instance of Notepad... 
    hSubMenu := GetASubmenu(hwnd, [3]); // 4th submenu Menu aka &View 

    if hSubMenu > 0 then 
    begin 
    id := GetMenuItemID(hSubMenu, 0); // 1st Item in that sub menu (must not be a submenu itself) 
    if id > -1 then 
    begin 
     PostMessage(hwnd, WM_COMMAND, id, 0); 
     ShowMessage('Done: ' + GetMenuItemCaption(hSubMenu, id)); 
    end 
    else 
     RaiseLastOSError; 
    end 
    else 
    RaiseLastOSError; 
end; 
Questions connexes