How to find TMainMenu parent of TMenuItem?

550 Views Asked by At

I have a standard TMainMenu which contains some TMenuItem elements. How can I find dynamically a parent main menu object from one of them?

The following code brings a compiler error already in the first executable line:

procedure TMenuItemHelper.AlignToRight;
const
  P3 = False;
var
  info: TMenuItemInfo;
  buffer: array[0..78] of WideChar;
  mainMenu: TMainMenu;
begin
  if Self.Parent is TMainMenu then
    mainMenu := (Self.Parent as TMainMenu)
  else
    raise EMenuItemHelper.Create(strExcMenuItemNotFirstLevel);

  info.cbSize := SizeOf(info);
  info.fMask := MIIM_TYPE;
  info.dwTypeData := buffer;
  info.cch := SizeOf(buffer);
  GetMenuItemInfo(mainMenu.Handle, Self.Command, P3, info);

  info.fType := info.fType or MFT_RIGHTJUSTIFY;
  SetMenuItemInfo(mainMenu.Handle, Self.Command, P3, info);
end;

The error message is following:

[dcc32 Error] E2010 Incompatible types: 'TMenuItem' and 'TMainMenu'

I can not see any related question, but the solution is probably quite simple.

1

There are 1 best solutions below

0
On BEST ANSWER

The solution from Victoria:

  if Self.GetParentMenu is TMainMenu then
    mainMenu := (Self.GetParentMenu as TMainMenu)
  else
    raise EMenuItemHelper.Create(strExcMenuItemParentIsNotAMainMenu);