mainunit.pas
上传用户:psxgmh
上传日期:2013-04-08
资源大小:15112k
文件大小:3k
源码类别:

Delphi/CppBuilder

开发平台:

Delphi

  1. unit mainunit;
  2. interface
  3. uses
  4.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  5.   Dialogs, StdCtrls;
  6. type
  7.   TForm1 = class(TForm)
  8.     GroupBox1: TGroupBox;
  9.     Button1: TButton;
  10.     Button2: TButton;
  11.     Label1: TLabel;
  12.     Label2: TLabel;
  13.     Label3: TLabel;
  14.     Label4: TLabel;
  15.     Label5: TLabel;
  16.     Label6: TLabel;
  17.     Label7: TLabel;
  18.     
  19.     procedure FormCreate(Sender: TObject);
  20.     procedure FormPaint(Sender: TObject);
  21.     procedure Button2Click(Sender: TObject);
  22.     procedure Button1Click(Sender: TObject);
  23.   private
  24.     HookHandle: THandle;
  25.     { Private declarations }
  26.   public
  27.     procedure WMSysCommand(var Msg: TWMSysCommand); message WM_SYSCOMMAND;
  28.     procedure GetRectArea;
  29.     { Public declarations }
  30.   end;
  31. var
  32.   Form1: TForm1;
  33.   FormHandle:Hwnd;
  34.   FrmLeft,FrmTop,FrmRight,FrmBottom:Integer;
  35. implementation
  36. {$R *.dfm}
  37. procedure JudgePointPosition(CurPoint:TPoint);
  38. begin
  39.  if ((CurPoint.X >= FrmLeft) and (CurPoint.X <= Frmright) and
  40.      (CurPoint.Y >= FrmTop) and (CurPoint.Y <= FrmBottom)) then
  41.     begin
  42.      Form1.Show;
  43.     end
  44.     else
  45.     begin
  46.      Form1.hide;
  47.     end;
  48. end;
  49. function MouseProc(Code: Integer; W: WPARAM; L: LPARAM): LRESULT; stdcall;
  50. var
  51.  CurPoint:TPoint;
  52. begin
  53.  if Code < 0 then
  54.   Result := CallNextHookEx(Form1.HookHandle, Code, W, L)
  55.  else
  56.  begin
  57.   if Code=HC_ACTION then
  58.    begin
  59.     if W = WM_MOUSEMOVE then
  60.      begin
  61.       Form1.label6.caption:='横坐标:'+InttoStr(PMouseHookStruct(L).pt.x);
  62.       Form1.label7.caption:='纵坐标:'+InttoStr(PMouseHookStruct(L).pt.y);
  63.       CurPoint:=PMouseHookStruct(L).pt;
  64.       JudgePointPosition(CurPoint);
  65.      end;
  66.     Result := 0;
  67.    end;
  68.  end;
  69. end;
  70. procedure TForm1.WMSysCommand(var Msg: TWMSysCommand);
  71. begin
  72.  GetRectArea;
  73.  DefaultHandler(Msg);
  74. end;
  75. procedure TForm1.GetRectArea;
  76. var
  77.  rect:TRect;
  78. begin
  79.  GetWindowRect(FormHandle,rect);
  80.  label1.Caption:='窗体左上角坐标:'+'('+Inttostr(rect.left)+','+Inttostr(rect.top)+')';
  81.  label2.Caption:='窗体右上角坐标:'+'('+Inttostr(rect.right)+','+Inttostr(rect.top)+')';
  82.  label3.Caption:='窗体左下角坐标:'+'('+Inttostr(rect.left)+','+Inttostr(rect.bottom)+')';
  83.  label4.Caption:='窗体右下角坐标:'+'('+Inttostr(rect.right)+','+Inttostr(rect.bottom)+')';
  84.  FrmLeft:=rect.left;
  85.  FrmTop:=rect.top;
  86.  FrmRight:=rect.right;
  87.  FrmBottom:=rect.bottom;
  88. end;
  89. procedure TForm1.FormCreate(Sender: TObject);
  90. begin
  91.  FormHandle:=Form1.Handle;
  92.  HookHandle := 0;
  93. end;
  94. procedure TForm1.FormPaint(Sender: TObject);
  95. begin
  96.  GetRectArea;
  97. end;
  98. procedure TForm1.Button2Click(Sender: TObject);
  99. begin
  100.  if HookHandle <> 0 then
  101.  begin
  102.   UnhookWindowsHookEx(HookHandle);
  103.   HookHandle := 0;
  104.   Form1.Caption:='鼠标钩子成功卸载!';
  105.  end;
  106. end;
  107. procedure TForm1.Button1Click(Sender: TObject);
  108. begin
  109. if HookHandle = 0 then
  110.  begin
  111.   HookHandle := SetWindowsHookEx(WH_MOUSE, @MouseProc, hInstance, 0);
  112.   if HookHandle<>0 then
  113.    Form1.Caption:='成功加载了鼠标钩子函数!';
  114.  end;
  115. end;
  116. end.