How to control window controls on other program forms:

zhaozj2021-02-11  156

With the concept of callback function and the example above, we can continue. In fact, I want to find a title known window handle, using an API function: findwindow.

Its 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 lot of tools. Such as Winsignt32.lpwindowname: window title.

Calling method example:

Var WNDHWND: HWND; WNDHWND: = FINDWINDOW (NIL, 'a window title'); if WNDHWND <> 0 THEN FILE: // Find this window handle. Begin XXXXEndelse Begin MessageBox (Self.Handle, 'Didn't find the window handle' , 'Tips', 0); End;

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

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

Its function is: function enumchildwindows (hwndparent: hwnd; lpenumfunc: tfnwndenumproc; lparam: lparam): Bool; stdcall;

This function and the enumwindow function are very imagined. The role is also very similar. Its function is to list the handle of all window controls on the HWNDParent form. It is also given in the form of a callback function parameter.

We will give a practical example, and the usage of this function is to let the user enter a window title, then call the FindWindow function to find this window handle. Through this handle, we display all the windows in a MEMO. Window control.

A callback function is written before. 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 (wndcaption); lines.add); lines.add ('-------') End; Result: = true;

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 'has the following control class name'); hwnd: = findwindow (nil, pchar (edit1 .Text)); if hwnd <> 0 Then Begin enumchildwindows (hwnd, @ enumchildwndproc, 0); Else MessageBox (self.handle, 'Didn't find the window handle', 'prompt', 0);

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 control Label1: TLabel; edit1: tedit; File: // Enter the title. Button1: tbutton; procedure button1click (sender: TOBJECT); private {private declarations}

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; becom getclassname (Ahwnd, WndclassName, 254); getWindowText (AhWnd, Wndcaption, 254); with form1.memo1 do begin lines.add (string (wndcaption); lines.add (' ----- '); end; result: = true;

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); Else MessageBox (self.handle, 'Didn't find the window handle', 'prompt', 0);

End.

With a control handle, we can certainly you want to do it. For example:

SendMessage (HWND, WM_SETTEXT, 0, LONGINT (PCHAR ('sdafdsf'))); you can send text to the control. Other messages can also be sent a lot of things.

However, there is a big question: assume that there is a lot of the same control on a form, and there is no way to distinguish them. Even if we can find all the control handles, we can't distinguish which is what we want, it is also dry Anxious.

I thought for a long time, and later found the answer in the bigaire, as long as I used a small skill, I can solve it.

转载请注明原文地址:https://www.9cbs.com/read-5952.html

New Post(0)