How do I get the coordinates of the mouse when a control is clicked?

后端 未结 4 1473
遥遥无期
遥遥无期 2021-01-01 23:15

In a TImage\'s OnClick event, I would like to extract the x,y coordinates of the mouse. I would prefer them in relation to the image, but in relation to the form or window i

4条回答
  •  长发绾君心
    2021-01-01 23:42

    How about this?

    procedure TForm1.Button1Click(Sender: TObject);
    var
    MausPos: TPoint;
    begin
      GetCursorPos(MausPos);
      label1.Caption := IntToStr(MausPos.x);
      label2.Caption := IntToStr(MausPos.y);
    end;
    
    
    procedure TForm1.Button2Click(Sender: TObject);
    begin
      SetCursorPos(600, 600);
    end;
    

    Found this online somewhere once and saved it in my codesnippet DB :)

    This page will probably solve all your questions however... There appear to be functions to go from client to screen coordinates and back etc..

    Good luck!

提交回复
热议问题