Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Align a TPopupMenu to the right hand side of the form?

Tags:

winapi

delphi

How can a TPopupMenu be aligned to the right hand side of a form? The problem is that there doesn't seem to be a way to get the width of the Popup menu before you call Popup(X, Y: Integer).

I'm trying to get a behavior similar to that of the system menu in Chrome.

enter image description here

like image 884
norgepaul Avatar asked Jul 25 '12 10:07

norgepaul


2 Answers

The easiest solution would be to launch the popup menu yourself:

procedure TForm1.Panel1ContextPopup(Sender: TObject; MousePos: TPoint;
  var Handled: Boolean);
var
  PopupPt: TPoint;
begin
  PopupPt := ClientToScreen(Point(ClientWidth, 0));
  TrackPopupMenu(PopupMenu1.Handle, TPM_RIGHTALIGN or TPM_TOPALIGN,
      PopupPt.X, PopupPt.Y, 0, PopupList.Window, nil);
end;

See documentation for TrackPopupMenu or TrackPopupMenuEx for what various flags mean.

like image 147
Sertac Akyuz Avatar answered Oct 18 '22 22:10

Sertac Akyuz


You could also just set Alignment to paRight and call

with ClientToScreen(Point(ClientWidth - 1, 0)) do
  Popup(X, Y);
like image 13
Ondrej Kelle Avatar answered Oct 18 '22 23:10

Ondrej Kelle