将TPopupMenu对齐到表单的右侧?

时间:2012-07-25 10:31:07

标签: delphi winapi

如何将TPopupMenu与表单的右侧对齐?问题是在调用Popup(X, Y: Integer)之前似乎没有办法获得弹出菜单的宽度。

我正试图获得类似于Chrome中系统菜单的行为。

enter image description here

2 个答案:

答案 0 :(得分:14)

最简单的解决方案是自己启动弹出菜单:

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;

请参阅TrackPopupMenuTrackPopupMenuEx的文档,了解各种标志的含义。

答案 1 :(得分:13)

您也可以将Alignment设置为paRight并致电

with ClientToScreen(Point(ClientWidth - 1, 0)) do
  Popup(X, Y);