카테고리 없음

[윈도우즈 API] 마우스 위치의 콘트롤(콤포넌트)을 조사하기

쇼핑스크래퍼3 2023. 9. 5. 08:04
// 마우스 포인터 위치의 콘트롤(콤포넌트)의 이름을 폼의 Caption에
// 출력하는 예로 아래의 Label1, Edit1, Memo1, Button1 등은 임의로
// 올려놓으시고 테스트 해보세요
// Panel 안에 있는 콘트롤도 구분할 수 있습니다


unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Edit1: TEdit;
    Memo1: TMemo;
    Button1: TButton;
  private
    { Private declarations }
    procedure WndProc(var Message: TMessage); override;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation
{$R *.DFM}

procedure TForm1.WndProc(var Message: TMessage);
var
  cmpFind: TComponent;
  pntXY: TPoint;
begin
  if Message.Msg = CM_MOUSEENTER then
  begin
    GetCursorPos(pntXY);
    // FindVCLWindow() 를 사용하면 TWinControl 계열 콘트롤만 알 수 있습니다
    cmpFind := FindDragTarget(pntXY, true);
    if cmpFind <> nil then
      Self.Caption := cmpFind.Name;
  end;

  inherited WndProc(Message);
end;

end.