单击TButton时如何显示TPopupMenu?

Use*_*ser 8 delphi button popupmenu cursor-position

我想在单击按钮时显示弹出菜单,但此过程在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;
Run Code Online (Sandbox Code Playgroud)

当点击按钮显示此错误:

[DCC错误] Form1.pas(205):E2010不兼容类型:'HWND'和'TPoint'
[DCC错误] Form1.pas(398):E2197常量对象不能作为var参数传递
[DCC错误] Form1.pas( 398):E2197常量对象不能作为var参数传递

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

And*_*and 24

做就是了

procedure TForm1.Button1Click(Sender: TObject);
var
  pnt: TPoint;
begin
  if GetCursorPos(pnt) then
    PopupMenu1.Popup(pnt.X, pnt.Y);
end;
Run Code Online (Sandbox Code Playgroud)

还有一些讨论

如果由于某种原因需要使用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;
Run Code Online (Sandbox Code Playgroud)

你的代码不起作用,因为

  1. ClientToScreen 是带有签名的Windows API的功能

    function ClientToScreen(hWnd: HWND; var lpPoint: TPoint): BOOL;
    
    Run Code Online (Sandbox Code Playgroud)

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

    function TControl.ClientToScreen(const Point: TPoint): TPoint;
    
    Run Code Online (Sandbox Code Playgroud)

    因此,如果你在一个类的方法,所述类被器的新生代TControl,ClientToScreen将参考后者.如果没有,它将参考前一个.当然,这一个需要知道我们要从哪个窗口转换坐标!

  2. 另外,如果你申报

    var mb1: TMouseButton
    
    Run Code Online (Sandbox Code Playgroud)

    作为参数,只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, Button1.Height)) do
    PopupMenu1.Popup(X, Y);
end;    
Run Code Online (Sandbox Code Playgroud)


RRU*_*RUZ 5

此错误是因为您的代码正在调用Windows.ClientToScreen函数而不是 TControl.ClientToScreen函数

尝试这样的事情

procedure TForm6.Button2Click(Sender: TObject);
var
   pt : TPoint;
begin
    pt.x := TButton(Sender).Left + 1;
    pt.y := TButton(Sender).Top + TButton(Sender).Height + 1;
    pt := Self.ClientToScreen( pt );
    PopupMenu1.popup( pt.x, pt.y );
end;
Run Code Online (Sandbox Code Playgroud)

或者ShowPopupMenuEx在你的Tform1班级内宣布你的程序并且会有效.