whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1541)

Database (90)
Files (137)
Forms (107)
Graphic (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Math (76)
Misc (126)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (266)
VCL (242)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

41 Visitors Online


 
... find Item by Name(Caption or Tag) in TMenu.Items?
Autor: Khomutov
[ Print tip ]  

Tip Rating (9):  
     


//... find Item by Name(Caption or Tag) in TMenu.Items?

interface

uses
  
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, StdCtrls, ComCtrls, Menus;


//Bypass of Source.Items (it's clone of IterateMenus from Menus unit)
procedure BypassMenuItem(Func: Pointer; Source: TMenuItem);
//Finds Item.Name = Value in the aMenuItem.Items
function FindItemByName(aMenuItem: TMenuItem; Value: string): TMenuItem;

var
  
//global vars
  
StringValue: string;
  IntegerValue: Integer;...implementation
//...
//Bypass of Source.Items (it's clone of IterateMenus from Menu unit)

procedure BypassMenuItem(Func: Pointer; Source: TMenuItem);
var
  
I, J: Integer;
  MenuSize: Integer;
  Done: Boolean;

  function ByPass(var I: Integer; aMenuItem: TMenuItem; AFunc: Pointer): Boolean;
  var
    
Item: TMenuItem;
  begin
    if 
aMenuItem = nil then Exit;
    Result := False;

    while not Result and (I < aMenuItem.Count) do
    begin
      
Item := aMenuItem[I];
      asm
                
MOV     EAX,Item
                MOV     EDX,[EBP+8]
                PUSH    DWORD PTR [EDX]
                CALL    DWORD PTR AFunc
                ADD     ESP,4
                MOV     Result,AL
      end;
      Inc(I);
    end;
  end;
begin
  
I        := 0;
  J        := 0;
  MenuSize := 0;
  if Source <> nil then MenuSize := Source.Count;
  Done := False;
  while not Done and (I < MenuSize) do
  begin
    
Done := Bypass(I, Source, Func);
    while (I < MenuSize) do Inc(I);
  end;
end;

//Finds Item.Name = ItemName  in the aMenuItem.Items
// Returns found item or nil.
function FindItemByName(aMenuItem: TMenuItem; ItemName: string): TMenuItem;
  // As variant: ItemTag:Integer; instead ItemName

  
function Find(Item: TMenuItem): Boolean;
  var
    
I: Integer;
  begin
    
Result := False;
    if (StringValue = Item.Name) then
    
// As variant: if (IntegerValue = Item.Tag)  then
    
begin
      
FoundItem := Item;
      Result := True;
      Exit;
    end
    else
      for 
I := 0 to Item.Count - 1 do
        if 
Find(Item[I]) then
        begin
          
Result := True;
          Exit;
        end;
  end;
begin
  
FoundItem   := nil;
  StringValue := ItemName; // Assigns Find argument to global variable.
  // As variant: IntegerValue := ItemTag;

  
BypassMenuItem(@Find, aMenuItem);
  Result := FoundItem;
end;



 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners