Link to home
Start Free TrialLog in
Avatar of flasht
flasht

asked on

Handle under mouse pointer.

How do i get handle of control under mouse pointer (of other application of course) and how do i enable this control if its disabled by this handle? (Like Greatis WinDowse can do)
Avatar of tobjectpascal
tobjectpascal

Getting the handle of an enabled window is easy just do..


(May have to strip it a bit)

Var
 Mpos: DWord;
 X,Y: Word;
 P: Tpoint;
 Wnd: Hwnd;
Begin
 Mpos:=getmessagepos();
 Y:=hiword(Mpos);
 X:=loword(Mpos);
 P.x:=X;
 P.Y:=Y;
 Wnd:=WindowFromPoint(P);
//Wnd = window's handle do was you please.

 End;
There is a small and smart tool, called Showin that can get the hwnd of a control, show/hide or enable/disable it, etc.
It can be downloaded here: www.foundstone.com/resources/forensics.htm

If you keep to doing this in Delphi, some useful Winapi functions:

Functions connected to mouse cursor
GetCursorPos(var Pt:TPoint);
SetCursorPos(x, y:integer);

Simulating mouse click at the position of the mouse, etc.
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);

Getting window handle at a point:
WindowFromPoint(coordinates: TPoint);
ChildWindowFromPoint(parent_hwnd: THandle, relative_coordinates: TPoint);

Getting window/widget position to calculate relative coordinates:
GetWindowRect(hwnd:THandle, var rect:TRect);

Enable/disable window:
EnableWindow(hwnd: THandle, enable: boolean);

You'll find more information about these functions in MSDN.
ASKER CERTIFIED SOLUTION
Avatar of CodedK
CodedK
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of flasht

ASKER

But it still doesnt work as WinDowse... It gets the handle of the window but it does not show handle of component on that window...
Try to get the handle by a small freeware program called Showin
http://baixaki.ig.com.br/site/detail2516.htm

If it can't get the control's handle with Showin, I think you can't get it at all.
By the way, certain controls don't have hwnd (e.g. TSpeedButton in Delphi).
The code i gave you does get the handle of some components like buttons, radio buttons, forms.
You need different code for menus...even WinDowse cant get menus.