How to control window controls on other program forms (medium)

Source: Internet
Author: User
In fact, if you want to find a window handle with a known title, you can use an API function: findwindow.

The original function is:

Function findwindow (lpclassname, lpwindowname: pchar): hwnd; stdcall;

Lpclassname: the name of the window class. If you only know the title, it can be blank. The window class name can be obtained using many tools, such as winsignt32.
Lpwindowname: window title.

Call method example:

VaR wndhwnd: hwnd;
Wndhwnd: = findwindow (nil, 'window title ');
If wndhwnd <> 0 then // find the handle of this window.
Begin
XXXXX
End
Else begin
MessageBox (self. Handle, 'The handle to this Windows' is not found, 'hs', 0 );
End;

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

Similarly, you must first obtain the handle of the window control on other forms. We use this API function: enumchildwindows.

The original function is:
Function enumchildwindows (hwndparent: hwnd; lpenumfunc: tfnwndenumproc;
Lparam: lparam): bool; stdcall;

This function and the enumwindow function are somewhat imaginary. its role is also very similar. its function is to list the handles of all window controls on a form with a window handle of hwndparent. it is also given in the form of callback function parameters.

Let's take another practical example to illustrate the usage of this function. the function of the program is to allow the user to enter a window title, and then call the findwindow function to find the window handle. with this handle, all the window controls in this window are displayed in a memo.

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 (ahwndnd, wndcaption, 254 );
With form1.memo1 do
Begin
Lines. Add (string (wndclassname ));
Lines. Add (string (wndcaption ));
Lines. Add ('-------');
End;
Result: = true;
End;

Then, call the enumchildwindows function in an event.
Procedure tform1.button1click (Sender: tobject );
VaR
Hwnd: longint;
Begin
Memo1.lines. Clear;
Memo1.lines. Add (edit1.text + 'controls with the following class name ');
Hwnd: = findwindow (nil, pchar (edit1.text ));
If hwnd <> 0 then
Begin
Enumchildwindows (hwnd, @ enumchildwndproc, 0 );
End
Else MessageBox (self. Handle, 'The handle to this Windows' is not found, 'hs', 0 );
End;

The program list is as follows:
Unit unit1;

Interface

Uses
Windows, messages, sysutils, variants, classes, graphics, controls, forms,
Dialogs, stdctrls;

Type
Tform1 = Class (tform)
Memo1: tmemo; // display the found Control
Label1: tlabel;
Edit1: tedit; // enter the 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 (ahwndnd, 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 + 'controls with the following class name ');
Hwnd: = findwindow (nil, pchar (edit1.text ));
If hwnd <> 0 then
Begin
Enumchildwindows (hwnd, @ enumchildwndproc, 0 );
End
Else MessageBox (self. Handle, 'The handle to this Windows' is not found, 'hs', 0 );
End;

End.

With the control handle, we can certainly do what we like. For example:

Sendmessage (hwnd, wm_settext, 0, longint (pchar ('sdafdsf'); you can send text to the control. You can also send different messages to do many things.

However, there is a big problem: Assume that a form has many identical controls and there is no way to differentiate them, even if we can find all the control handles, we cannot tell which one is what we want.

After thinking for a long time, I found the answer in the monopoly. I can solve the problem by using a small skill.

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.