delphi取鼠标所指窗体信息  
官方Delphi 学习QQ群: 682628230(三千人)
频道

delphi取鼠标所指窗体信息


打开诸如“计算器”程序,鼠标指向某个按钮,按Ctrl+Delete键。


取得了鼠标所指控件与其父窗体的信息。
 

源程序代码:
-------------------------------------------------------------------

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  p rivate
    { Private declarations }
    procedure hotKeyOn;
    procedure hotKeyOff;
    procedure hotKeyDown(var msg:TMessage);message WM_HOTKEY;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
{$APPTYPE CONSOLE}
var
  hotKeyID:integer;

procedure TForm1.FormCreate(Sender: TObject);
begin
  memo1.Lines.Clear;
  hotKeyOn;
end;

procedure TForm1.hotKeyDown(var msg: TMessage);
var
  pt:TPoint;
  hwnd:THandle;
  hwndParent:THandle;
  str:array[0..255] of char;
begin
  if (msg.LParamHi=VK_DELETE) and (msg.LParamLo=MOD_CONTROL) then
  begin
    GetCursorPos(pt);
    hwnd:=WindowFromPoint(pt);
    //Writeln('ok');

    memo1.Lines.Add('坐标窗体句柄:'+IntToStr(hwnd));
    GetWindowText(hwnd,str,256);
    memo1.Lines.Add('坐标窗体标题:'+str);
    GetClassName(hwnd,str,256);
    memo1.Lines.add('坐标窗体类名'+str);
    hwndParent:=GetParent(hwnd);
    memo1.Lines.Add('父窗口句柄'+IntToStr(hwndParent));
    GetWindowText(hwndParent,str,256);
    memo1.Lines.Add('父窗口标题:'+str);
    GetClassName(hwndParent,str,256);
    memo1.Lines.Add('父窗口类名'+str);

    memo1.Lines.Add(#13);


  end;
end;

procedure TForm1.hotKeyOff;
begin
  UnregisterHotKey(self.Handle,hotKeyID);
end;

procedure TForm1.hotKeyOn;
var
  r1:BOOL;
begin
  r1:=RegisterHotKey(self.Handle,
    hotKeyID,MOD_CONTROL,VK_DELETE
  );
  if not r1 then ShowMessage('RegisterHotKey error!');
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  hotKeyOff;
end;

end.


推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.042808055877686 seconds