Dauezevy
Dauezevy

Reputation: 1080

Delphi Mouse Click Coordinates

enter image description here

Hi, i' ve 3 panels consisting of form. I assigned a click event to 3rd panel to getting coordinates.

procedure TformMain.Panel3Click(Sender: TObject);
var
  pt : tPoint;
begin
  pt := Mouse.CursorPos;
  ShowMessage('X : ' + IntToStr(pt.X) + ' & Y : ' + IntToStr(pt.Y));
end;

This code works, but i don' t know how to Y coord. starting from beginning of the panel 3. I mean when i click the panel3' s top, it's y coord must be 0.

Thanks in advice.

p.s. : My form has a main menu, so i tried to subtract panel1 height from the pt.y but i couldnt get main menu's height.

Upvotes: 2

Views: 6206

Answers (2)

SilverWarior
SilverWarior

Reputation: 8331

Why don't you use OnMouseDown event and OnMouseUp event.

Instead of OnClick event which already provide you with X,Y coordinates of the mouse click/release, not to mention with the information about which mouse button was used and state of special keys like Shift, CTRL and ALT.

For better explanation of what information these events provide check the TMouseEvent documentation.

Upvotes: 5

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186668

To convert coordinates use ClientToScreen() and ScreenToClient() methods:

procedure TformMain.Panel3Click(Sender: TObject);
var
  pt: TPoint;
begin
  // Converting from screen coordinates into Sender (that's Panel3)
  // client area coordinates
  pt := TPanel(Sender).ScreenToClient(Mouse.CursorPos);
  ShowMessage('X : ' + IntToStr(pt.X) + ' & Y : ' + IntToStr(pt.Y));
end;

Upvotes: 3

Related Questions