加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – 如何在单击TButton时显示TPopupMenu?

发布时间:2020-12-15 04:08:39 所属栏目:大数据 来源:网络整理
导读:我想在单击按钮时显示弹出菜单,但此过程在Delphi XE中有错误. procedure ShowPopupMenuEx(var mb1:TMouseButton;var X:integer;var Y:integer;var pPopUP:TPopupMenu);var popupPoint : TPoint;begin if (mb1 = mbLeft) then begin popupPoint.X := x ; popu
我想在单击按钮时显示弹出菜单,但此过程在Delphi XE中有错误.
procedure ShowPopupMenuEx(var mb1:TMouseButton;var X:integer;var Y:integer;var pPopUP:TPopupMenu);
var
  popupPoint : TPoint;
begin
  if (mb1 = mbLeft) then begin
    popupPoint.X := x ;
    popupPoint.Y := y ;
    popupPoint := ClientToScreen(popupPoint);   //Error Here
    pPopUP.Popup(popupPoint.X,popupPoint.Y) ;   
  end;
end;

procedure TForm1.Button1MouseUp(Sender: TObject; Button: TMouseButton;Shift: TShiftState; X,Y: Integer);
begin
  ShowPopupMenuEx(button,Button1.Left,Button1.Top,PopupMenu1); //Error Here
end;

当点击按钮显示此错误时:

[DCC Error] Form1.pas(205): E2010 Incompatible types: ‘HWND’ and ‘TPoint’
[DCC Error] Form1.pas(398): E2197 Constant object cannot be passed as var parameter
[DCC Error] Form1.pas(398): E2197 Constant object cannot be passed as var parameter

当点击按钮时,是否有更好的方式来显示弹出菜单?

解决方法

做就是了
procedure TForm1.Button1Click(Sender: TObject);
var
  pnt: TPoint;
begin
  if GetCursorPos(pnt) then
    PopupMenu1.Popup(pnt.X,pnt.Y);
end;

还有一些讨论

如果由于某种原因需要使用OnMosuseUp,则可以这样做

procedure TForm1.Button1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X,Y: Integer);
var
  pnt: TPoint;
begin
  if (Button = mbLeft) and GetCursorPos(pnt) then
    PopupMenu1.Popup(pnt.X,pnt.Y);
end;

你的代码不起作用,因为

> ClientToScreen是具有签名的Windows API的功能

function ClientToScreen(hWnd: HWND; var lpPoint: TPoint): BOOL;

但是,还有一个带签名的TControl.ClientToScreen

function TControl.ClientToScreen(const Point: TPoint): TPoint;

因此,如果你是一个类方法,该类是TControl的后代,ClientToScreen将引用后者.如果没有,它将参考前一个.当然,这一个需要知道我们要从哪个窗口转换坐标!
>另外,如果你申报

var mb1: TMouseButton

作为参数,只接受TMouseButton类型的变量.但我看不出你为什么想要这个ShowPopupMenuEx函数的签名.事实上,我认为根本不需要这样的功能……

替代

上面的代码将弹出光标位置的菜单.如果你需要相对于按钮的一个角来固定点,你可以这样做

// Popup at the top-left pixel of the button
procedure TForm1.Button1Click(Sender: TObject);
begin
  with Button1.ClientToScreen(point(0,0)) do
    PopupMenu1.Popup(X,Y);
end;

// Popup at the bottom-right pixel of the button
procedure TForm1.Button1Click(Sender: TObject);
begin
  with Button1.ClientToScreen(point(Button1.Width,Button1.Height)) do
    PopupMenu1.Popup(X,Y);
end;

// Popup at the bottom-left pixel of the button
procedure TForm1.Button1Click(Sender: TObject);
begin
  with Button1.ClientToScreen(point(0,Y);
end;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读