1. Home
  2. Computing & Technology
  3. Delphi Programming

Get text from the control at pos x,y on screen

By , About.com Guide

This form has 3 labels and a timer component:

~~~~~~~~~~~~~~~~~~~~~~~~~
unit unit1;

interface

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

type
   TForm1 = class(TForm)
     Timer1: TTimer;
     Label1: TLabel;
     Label2: TLabel;
     Label3: TLabel;
     procedure FormCreate(Sender: TObject) ;
     procedure Timer1Timer(Sender: TObject) ;
   private
   procedure ShowHwndAndClassName(CrPos: TPoint) ;
   public
   end;

var
   Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.ShowHwndAndClassName(CrPos: TPoint) ;
var
   hWnd: THandle;
   aName,
   Text : array [0..255] of char;
begin
   hWnd := WindowFromPoint(CrPos) ;
   Label1.Caption := 'Handle : ' + IntToStr(hWnd) ;

   if boolean(GetClassName(hWnd, aName, 256)) then
     Label2.Caption := 'ClassName : ' + string(aName)
   else
     Label2.Caption := 'ClassName : not found';
   SendMessage(hWnd, WM_GETTEXT,
               SizeOf(Text), integer(@Text)) ;
   Label3.Caption := 'Text :' + Text;
end;

procedure TForm1.FormCreate(Sender: TObject) ;
begin
   Form1.FormStyle := fsStayOnTop;
   Timer1.Interval := 50;
end;

procedure TForm1.Timer1Timer(Sender: TObject) ;
var
   rPos: TPoint;
begin
   if boolean(GetCursorPos(rPos))
   then ShowHwndAndClassName(rPos) ;
end;
end.
~~~~~~~~~~~~~~~~~~~~~~~~~

Delphi tips navigator:
» Add columns to a ListBox
« Activate Windows "Start" button from code

Explore Delphi Programming
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. 2000 Delphi Tips
  7. Get text from the control at pos x,y on screen

©2009 About.com, a part of The New York Times Company.

All rights reserved.