Obtain the window handle through the pointing operation

Source: Internet
Author: User

Obtain the window handle through the pointing operation

Zhao Wei:
I used sendkeys of VB to write a tool that simulates the keyboard for sending characters to other programs. because the size of the program compiled by VB is too large,
I want to rewrite it with Delphi and use sendmessage and other API functions, but I want to find
Window to obtain the thread ID and handle of the window. (Use findwindow to get the window)
The handle must enter the window title .)
Answer:
To implement the sendkeys function in Delphi, you should use the journal playback hook function,
Instead of using the sendmessage function. Next we will introduce how to use the mouse to move the user to select a window, and the program
Obtain the handle of the window. Windows API has a function windowfrompoint. You only need to know the cursor position.
(Screen coordinates), you can get the handle of the window to which the position belongs. With the handle, you can use other functions to get
More information. If you move the mouse in the program window, you can get the mousemove event. To move the mouse outside the window
When moving, you can still get the mouse event. You must use the setcapture function. The following example uses the two functions.
To implement the required functions.

Type
Tform1 = Class (tform)
............
Public
Procedure inverttracker (hwnddest: integer );
End;
............
VaR
Form1: tform1;
Mlnghwndcaptured: integer;
Hwndlast: integer;
............
Procedure tform1.formmousemove (Sender: tobject; shift: tshiftstate; X,
Y: integer );
VaR PT: tpoint;
Begin
If getcapture () <> 0 then // capture status
Begin
PT. X: = X;
PT. Y: = y;
Clienttoscreen (PT); // obtain the mouse's screen position
// Obtain the window handle
Mlnghwndcaptured: = windowfrompoint (PT );

If hwndlast <> mlnghwndcaptured then
Begin
If hwndlast <> 0 then // bold the window border
Inverttracker (hwndlast );
Inverttracker (mlnghwndcaptured );
Hwndlast: = mlnghwndcaptured;
End
End;
// Display coordinates and window handle
Caption: = 'x: '+ inttostr (x) +', Y: '+ inttostr (y)
+ ', Hwnd:' + inttostr (mlnghwndcaptured );
End;

Procedure tform1.formmousedown (Sender: tobject; button: tmousebutton;
Shift: tshiftstate; X, Y: integer );
Begin
If setcapture (handle) <> 0 then // start capturing
Cursor: = cruparrow;
End;

Procedure tform1.formmouseup (Sender: tobject; button: tmousebutton;
Shift: tshiftstate; X, Y: integer );
VaR strcaption: pchar;
Begin
If mlnghwndcaptured <> 0 then
Begin // obtain the window title
Strcaption: = stralloc (1000 );
Getwindowtext (maid, strcaption, 1000 );
Caption: = strpas (strcaption );
Invalidaterect (0, prect (0), true );
Mlnghwndcaptured: = 0;
Cursor: = crdefault;
Releasecapture;
Strdispose (strcaption );
Hwndlast: = 0;
End
End;
// Bold the window border
Procedure tform1.inverttracker (hwnddest: integer );
VaR
Hdcdest, Hpen, holdpen, holdbrush: integer;
Cxborder, cxframe, cyframe, cxscreen, cyscreen, Cr: integer;
RC: trect;
Const null_brush = 5;
Const r2_not = 6;
Const ps_insideframe = 6;
Begin
Cxscreen: = getsystemmetrics (0 );
Cyscreen: = getsystemmetrics (1 );
Cxborder: = getsystemmetrics (5 );
Cxframe: = getsystemmetrics (32 );
Cyframe: = getsystemmetrics (33 );
Getwindowrect (hwnddest, RC );

Hdcdest: = getwindowdc (hwnddest );

Setrop2 (hdcdest, r2_not );
Cr: = clblack;
Hpen: = createpen (ps_insideframe, 3 * cxborder, Cr );

Holdpen: = SelectObject (hdcdest, Hpen );
Holdbrush: = SelectObject (hdcdest, getstockobject (null_brush ));
Rectangle (hdcdest, 0, 0, RC. Right-RC. Left, RC. Bottom-RC. Top );
SelectObject (hdcdest, holdbrush );
SelectObject (hdcdest, holdpen );

Releasedc (hwnddest, hdcdest );
Deleteobject (Hpen );
End;
// Move the window to the upper left corner and reduce the window height for easy operation
Procedure tform1.formcreate (Sender: tobject );
Begin
Left: = 0;
Top: = 0;
Clientheight: = 76;
End;

When running the program, first open the point in the program window, then press and hold the left mouse button, move the mouse, then you will
You can see that the title Position of the program window constantly displays the current position of the mouse (window coordinates) and the handle of the window where the mouse is located.
At the same time, the selected window border is bold. Once the left button is opened, the title of the program window is changed to the title of the selected window.
I believe many people will be familiar with this operation method, because it is like spy ++ (Visual C ++), magic mouse,
Many software such as capture professional use similar operations to select windows.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.