Popup Menu是windows组件,默认只能控制弹出点,但弹出方向是向上还是向下是系统自动控制的。如果有屏幕下方有足够的空间,默认是向下的。但有的时候需要菜单向上弹出,如图所示:
以前我的做法是计算菜单高度,然后从左上角向下弹出,看似向上弹出的。可是这个菜单高度是个系统相关的东西,没有一个直接的属性在菜单弹出前得到。比如xp和vista的默认高度不一样,大字体的系统和标准字体的高度也不一样。
最近又找了一下,终于找到解决方案,放到这里供大家参考:
procedure TBusCaseManager.BitBtnPrintClick(Sender: TObject);
var
P: TPoint;
begin
inherited;
P := PanelBottom.ClientToScreen(Point(BitBtnPrint.Left,BitBtnPrint.Top));
TrackPopupMenu(PopupMenuPrint.Items.Handle, TPM_BOTTOMALIGN, P.X, P.Y, 0, BitBtnPrint.Handle, nil);
end;
关于TrackPopupMenu的用法请参考windows api帮助。
===================================================================
更新: 2012.04.10
上面的代码弹出的菜单点击没有反应的,具体的原因一两句话还说不清楚,简单的解决方案如下:
procedure TBusCaseManager.BitBtnPrintClick(Sender: TObject);
var
P: TPoint;
MenuID: LongBool;
idx: Cardinal;
begin
P := PanelBottom.ClientToScreen(Point(BitBtnPrint.Left,BitBtnPrint.Top));
MenuID := TrackPopupMenu(PopupMenuPrint.Items.Handle, TPM_BOTTOMALIGN or TPM_RETURNCMD, P.X, P.Y, 0, BitBtnPrint.Handle, nil);
idx := Ord(MenuID);
if GetMenuItemID(PopupMenuPrint.Handle,self.muPreviewCaseTag.MenuIndex)=idx then
self.muPreviewCaseTag.Click
else if GetMenuItemID(PopupMenuPrint.Handle,self.muPreviewCaseReceipt.MenuIndex)=idx then
self.muPreviewCaseReceipt.Click
else if GetMenuItemID(PopupMenuPrint.Handle,self.muPrintCaseTag.MenuIndex)=idx then
self.muPrintCaseTag.Click
else if GetMenuItemID(PopupMenuPrint.Handle,self.muPrintCaseReceipt.MenuIndex)=idx then
self.muPrintCaseReceipt.Click;
end;