如何控制弹出菜单向上弹出(how to control popup menu pop direction)

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;


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值