Delphi finds a window handle with a title known to traverse the window control handle (GO)

Source: Internet
Author: User

Delphi finds a window handle with a title known to traverse the window control handle (GO)


Using my method to control the window controls on other programs ' forms, you must first understand what is callback function。 My understanding is this:


The callback function is written out not by its own program to invoke, but to let other things to invoke, such as the Windows operating system, such as other programs and so on. But when it was called, I didn't know. The callback function typically defines the type of parameters and return values as required by the caller, you provide the caller with the entry address of your callback function, and then you can call the function to notify you at any time when the caller has an event, and pass the parameters in a predetermined form. So a lot of people say that the callback function is a bit like the BP machine you're carrying: Tell someone the number and call you when it's something!

So after a callback function is written out, there must be a registered action that tells the caller how you found the function I wrote. Some Windows API functions require a callback function address as one of its parameters, such as SetTimer, LineDDA, EnumObjects, and the enumwindows we want to use below.

Declaring a callback function in Delphi is a simple format, for example:

function Enumwindowsproc (ahwnd:longint;lparam:longint): Boolean;stdcall;

first of all, the function name can be arbitrarily random, but the type of function parameters generally not disorderly, its order, data type, etc. are stipulated, because these are to let other programs call, they have been prescribed, but the parameter name can be arbitrarily called. Note that you must take "stdcall" in the back.

StdCall is a standard call, which means calling a function with standard Windows parameter passing.

Writing function body is very simple, using the parameters passed over it, just remember that these parameters are sent to you, you just know what these parameters mean.

Then look at the function that registers the entry address of the callback function to the caller.
function EnumWindows (lpenumfunc:tfnwndenumproc; LParam:LPARAM): BOOL; stdcall;

Tfnwndenumproc is actually a pointer type. The Lpenumfunc is the entry address of the callback function.

Here is the format for calling EnumWindows:
EnumWindows (@EnumWindowsProc, 0);

by registering the entry address of the callback function with the system, the system can invoke the callback function when needed, passing parameters to it, perhaps these parameters are what we want.

The function of the EnumWindows function is to enumerate the top-level windows in all the programs on the screen and pass the window handle to the callback function as an argument. Once a window is found, a callback function is called. The condition at the end of the enumeration is either to enumerate through all the windows, or the callback function returns FALSE.

The Lparam:lparam parameter is a program-defined value that is passed to the callback function.

Go back and look at the Enumwindowsproc:

function Enumwindowsproc (ahwnd:longint;lparam:longint): Boolean;stdcall;

When a window is found, the callback function is called, and the handle of the window is passed as the first parameter, and the value of the program definition is passed as the second parameter in EnumWindows Lparam:lparam.

So we can use the two parameters passed over in the Enumwindowsproc function to do some processing.

Below we create a new program to enumerate all the programs in the system top-level window, we want to get the window title, to get the window class name.

Get window titles by:

function GetWindowText (hwnd:hwnd; lpstring:pchar; nmaxcount:integer): Integer; stdcall;

The function is to copy the caption of the window with the window handle to the HWND into a buffer lpstring. The nMaxCount is the maximum number of characters that are copied into the buffer.

To get the window caption can also send a message: Wm_gettext, in fact, GetWindowText is to send wm_gettext messages.

To get the window class name, use:

function GetClassName (hwnd:hwnd; lpclassname:pchar; nmaxcount:integer): Integer; stdcall;

Its parameter meaning is similar to the above function. Not explained in detail.

Let's write the callback function first: Enumwindowsproc. Now tell yourself that we've got the values for two parameters. These two parameters are given to us by the system.

In order to display the window caption and class name, we use a TMemo control.

Declare the function in the interface section first.

function Enumwindowsproc (ahwnd:longint; AFORM:TFORM1): Boolean;stdcall;

Note that I changed the second parameter, it does not matter, when the call to watch.

Then define the function in the implementation section:
function Enumwindowsproc (ahwnd:longint; AFORM:TFORM1): boolean;
Var
LPSZCLASSNAME,LPSZWINDOWTEXT:ARRAY[0..254] of Char; Defines a two buffer.
Begin
GetWindowText (ahwnd,lpszwindowtext,254);//Get Window Caption
GetClassName (ahwnd,lpszclassname,254);//Gets the window class name.
Aform.memo1.lines.add (Strpas (Lpszwindowtext));
Aform.memo1.lines.add (Strpas (lpszClassName));
Aform.memo1.lines.add ('--------------------');
Result:=true;
End

The next thing to do is call the EnumWindows function, register the callback function entry address, let the system call the callback function, list the window. So add one more Tbutton:btn_listwindow
Procedure Tform1.btn_listwindowclick (Sender:tobject);
Begin
EnumWindows (@EnumWindowsProc, Longint (self));
End

===================================================================================================

With the concept of a callback function and the above example, we can continue. Actually want to find a title known window handle, with an API function can be: FindWindow.

The prototype of the function is:

function FindWindow (Lpclassname, Lpwindowname:pchar): HWND; stdcall;

Lpclassname: Window class name. If you only know the title, you can be empty. The window class name can be obtained with a number of tools, such as Winsignt32.
Lpwindowname: Window caption.

Examples of invocation methods:

var Wndhwnd:hwnd;
Wndhwnd:=findwindow (Nil, ' a window title ');
If Wndhwnd<>0 then file://finds this window handle.
Begin
Xxxxx
End
ELSE begin
MessageBox (Self.handle, ' did not find the window handle ', ' hint ', 0);
End

With this window handle, it's not far from our initial goal: control the window controls on other forms.

Again, you first get a handle to the window control on the other form. We use this API function: Enumchildwindows.

The prototype of the function is:
function Enumchildwindows (hwndparent:hwnd; lpenumfunc:tfnwndenumproc;
Lparam:lparam): BOOL; stdcall;

This function and the Enumwindow function are quite imaginative. Its function is to enumerate the handles of all the window controls on a form that have a window handle of hwndparent. The same is given in the form of a callback function parameter.

Let's take a practical example to illustrate the use of this function. The function of the program is to have the user enter a window caption, and then call the FindWindow function to find the window handle. With this handle, we display all the window controls on the window in a memo.

Also write the callback function first.
function Enumchildwndproc (ahwnd:longint;
Alparam:lparam): Boolean;stdcall;
Var
WNDCLASSNAME:ARRAY[0..254] of Char;
WNDCAPTION:ARRAY[0..254] of Char;
Begin
GetClassName (ahwnd,wndclassname,254);
GetWindowText (ahwnd,wndcaption,254);
With Form1.memo1 do
Begin
Lines.add (String (wndclassname));
Lines.add (String (wndcaption));
Lines.add ('-------');
End
Result:=true;
End


The Enumchildwindows function is then called in an event.
Procedure Tform1.button1click (Sender:tobject);
Var
Hwnd:longint;
Begin
Memo1. Lines.clear;
MEMO1.LINES.ADD (edit1.text+ ' has the following control class name ');
Hwnd:=findwindow (Nil,pchar (Edit1.text));
If Hwnd<>0 Then
Begin
Enumchildwindows (hWnd, @EnumChildWndProc, 0);
End
else MessageBox (Self.handle, ' did not find the window handle ', ' hint ', 0);
End

The list of procedures is as follows:
Unit Unit1;

Interface

Uses
Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms,
Dialogs, Stdctrls;

Type
TForm1 = Class (Tform)
Memo1:tmemo; FILE://used to display the found controls
Label1:tlabel;
Edit1:tedit; file://enter a title.
Button1:tbutton;
Procedure Button1Click (Sender:tobject);
Private
{Private declarations}
Public
{Public declarations}
End

Var
Form1:tform1;

function Enumchildwndproc (ahwnd:longint;
Alparam:lparam): Boolean;stdcall;

Implementation


{$R *.DFM}
function Enumchildwndproc (ahwnd:longint;
Alparam:lparam): Boolean;stdcall;
Var
WNDCLASSNAME:ARRAY[0..254] of Char;
WNDCAPTION:ARRAY[0..254] of Char;
Begin
GetClassName (ahwnd,wndclassname,254);
GetWindowText (ahwnd,wndcaption,254);
With Form1.memo1 do
Begin
Lines.add (String (wndclassname));
Lines.add (String (wndcaption));
Lines.add ('-------');
End
Result:=true;
End


Procedure Tform1.button1click (Sender:tobject);
Var
Hwnd:longint;
Begin
Memo1. Lines.clear;
MEMO1.LINES.ADD (edit1.text+ ' has the following control class name ');
Hwnd:=findwindow (Nil,pchar (Edit1.text));
If Hwnd<>0 Then
Begin
Enumchildwindows (hWnd, @EnumChildWndProc, 0);
End
else MessageBox (Self.handle, ' did not find the window handle ', ' hint ', 0);
End

End.


With a handle to the control, of course we can do whatever we want. For example:

SendMessage (Hwnd,wm_settext,0,longint (Pchar (' SDAFDSF ')); You can send text to the control. Others can also send different messages to do a lot of things.


Original url:http://blog.sina.com.cn/s/blog_4ad042e50102dxdu.html



Delphi finds a window handle with a title known to traverse the window control handle (GO)

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.