简体   繁体   English

如何捕获鼠标 cursor (IDE Delphi) 的坐标以调用上下文菜单

[英]How to catch coordinates of mouse cursor (IDE Delphi) to invoke Context Menu

How to catch coordinates of mouse cursor (IDE Delphi) when I invoke Context Menu to create a new control?当我调用上下文菜单创建新控件时,如何捕获鼠标 cursor (IDE Delphi) 的坐标?

I'd like to create a new component via Context Menu at same coordinates where Context Menu was invoked.我想通过上下文菜单在调用上下文菜单的相同坐标处创建一个新组件。

I'm creating my own component editor to do this, then I need the coordinates of mouse to create the control there.我正在创建自己的组件编辑器来执行此操作,然后我需要鼠标的坐标来在那里创建控件。

I don't know if I understood your question well, but there are some ways to capture position of your mouse:我不知道我是否很好地理解了您的问题,但是有一些方法可以捕获鼠标的 position:

Method 1 - Capture the mouse position on your screen : Here you can use TMouse class like this:方法 1 - 在屏幕上捕获鼠标 position :在这里您可以像这样使用TMouse class :

var
  m: TMouse;
begin
  lbl_cordinate_screen.Caption := format('Mouse cordinate on screen: x:%d, y:%d', 
                                          [m.CursorPos.X, m.CursorPos.y]);
end;

Method 2 - Capture the mouse position on a control: Here you can use GetCursorPos , I declared a function called cursorCordinate , it will receive a control name (I used my form named frm_main as given control but it can be any other control like a button, label or anything else) and it will return a TPoint value containing position of mouse on given control:方法 2 - 在控件上捕获鼠标 position:在这里您可以使用GetCursorPos ,我声明了一个名为 cursorCordinate 的cursorCordinate ,它将收到一个控件名称(我使用名为frm_main的表单作为给定控件,但它可以是任何其他控件,例如按钮, label 或其他任何东西),它将返回一个包含给定控件上鼠标 position 的TPoint值:

//function to capture mouse position on a control
function cursorCordinate(myCtrl: TWinControl): TPoint;
var
  mouse_p: TPoint;
begin
  GetCursorPos(mouse_p);
  ScreenToClient(myCtrl.Handle, mouse_p );
  result := mouse_p;
end;

usage example:用法示例:

begin
  lbl_cordinate_form_1.Caption := format('Mouse cordinate on form: x:%d, y:%d',  
                                          [cursorCordinate(frm_main).X, cursorCordinate(frm_main).y]);
end;

Method 3 - Another way to capture the mouse position on a control: Here you can use control's OnMouseMove event and its X and Y parameters, just place your code block in this event.方法 3 - 在控件上捕获鼠标 position 的另一种方法:这里您可以使用控件的OnMouseMove事件及其XY参数,只需将代码块放在此事件中即可。 I used it to show mouse position on my form ( frm_main ) in a label ( lbl_cordinate_form_2 ), but you can use any other control's OnMouseMove event:我用它在 label ( lbl_cordinate_form_2 ) 中的表单 ( frm_main ) 上显示鼠标 position ,但您可以使用任何其他控件的OnMouseMove事件:

procedure Tfrm_main.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
lbl_cordinate_form_2.Caption := format('Mouse cordinate on form: x:%d, y:%d', [x, y]);
end;

You can see the result in image;您可以在图像中看到结果; first line is result of Method 1 , second line for Method 2 and third line belongs to Method 3 :第一行是方法 1的结果,第二行是方法 2 ,第三行属于方法 3

捕获鼠标位置

By add this cod to FormContextPopup can obtain mouse position通过将此代码添加到FormContextPopup可以获得鼠标 position

   uses FMX.Forms;
   ...
   ...
 procedure TForm88.FormContextPopup(Sender: TObject; MousePos: TPoint;
      var Handled: Boolean);
    begin
      Label1.Caption:=FMX.Forms.Screen.MousePos.X.ToString+' 
           '+FMX.Forms.Screen.MousePos.Y.ToString;
    
    end;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM