Thursday, April 23, 2009

How to Right Align a Menu Item


In most applications all (top level) menu items are aligned at the =
left side=20
of the menu bar. I'm sure you have seen applications with at least one =
item=20
aligned on the right side. In most cases this was the "Help" menu item.=20

Here's how to align a form's Help menu item (for example) on the =
right side=20
of the menu bar, in Delphi applications.=20



  1. Add a TMainMenu component to a form (Form1)=20

  2. Add several (top level) menu items (with sub items)=20

  3. Have a menu item named "HelpMenuItem"=20

  4. Use the code below in the form's OnCreate event.=20

  5. Run the project ... note that the "Help" item is aligned on the =
    right side=20
    of the menu bar.


procedure TForm1.FormCreate(Sender: =
TObject)=20
;
var
mii: TMenuItemInfo;
=
MainMenu:=20
hMenu;
Buffer: array[0..79] of=20
Char
;
begin
MainMenu :=3D=20
Self.Menu.Handle;

//GET Help Menu Item=20
Info

mii.cbSize :=3D SizeOf(mii) ;
=
mii.fMask=20
:=3D MIIM_TYPE;
mii.dwTypeData :=3D =
Buffer;
=20
mii.cch :=3D SizeOf(Buffer) ;
=
GetMenuItemInfo(MainMenu,=20
HelpMenuItem.Command, false, mii) ;

//SET Help =
Menu=20
Item Info

mii.fType :=3D mii.fType or=20
MFT_RIGHTJUSTIFY;
SetMenuItemInfo(MainMenu,=20
HelpMenuItem.Command, false, mii) ;
end;

No comments: