问题来源: http://www.cnblogs.com/del/archive/2008/06/14/1218771.html#1225859

代码文件:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

type
  TForm1 = class(TForm)
    procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
    procedure FormMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormClick(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

//方法一: 使用 OnMouseDown 事件的参数:
procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  //参数中的 X,Y 就是当前鼠标所在位置的坐标
  //譬如显示看看:
  Text := Format('OnMouseDown: x=%d; y=%d', [X,Y]);
end;

//方法二: 使用 OnMouseMove 事件的参数:
procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  //参数中的 X,Y 就是当前鼠标所在位置的坐标
  //譬如显示看看:
  Text := Format('OnMouseMove: x=%d; y=%d', [X,Y]);
end;

//方法三: 使用 OnMouseUp 事件的参数:
procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  //参数中的 X,Y 就是当前鼠标所在位置的坐标
  //譬如显示看看:
  ShowMessageFmt('OnMouseUp: x=%d; y=%d', [X,Y]);
end;

//方法四: 使用 API 函数 GetCursorPos:
procedure TForm1.FormClick(Sender: TObject);
var
  pt: TPoint;
begin
  GetCursorPos(pt);         {这是获取的相对于屏幕的坐标}
  pt := ScreenToClient(pt); {转换成本地坐标}
  ShowMessageFmt('API 函数 GetCursorPos: x=%d; y=%d', [pt.X, pt.Y]);
end;

end.

窗体文件:
object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 206
  ClientWidth = 339
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnClick = FormClick
  OnMouseDown = FormMouseDown
  OnMouseMove = FormMouseMove
  OnMouseUp = FormMouseUp
  PixelsPerInch = 96
  TextHeight = 13
end

posted on 2008-06-14 17:41  万一  阅读(8037)  评论(8编辑  收藏  举报