How to control window controls on other programs:

zhaozj2021-02-11  174

How to control window controls on other programs:

First of all: I am a rookie, I just want to publish the solution that is surrounded by my problem, I will be forgotten after I have forgotten, and I don't know some of these little knowledge. Some help. Don't laugh at me. At the same time, in order to express my low level, I will talk about some basic things, these are my understanding, it is very incorrect.

Use my method to control window controls on other program forms, you must first understand what is a callback function. My understanding is this:

The callback function is written not to call it, but it makes other things to call, such as a Windows operating system, such as other programs, etc. But when I was called but I didn't know. The callback function generally defines the type of parameters and return values ​​in accordance with the caller's requirements. You provide the caller with the entrance address of your callback function, and then you can follow the address you provide at any time when the caller is. The function notifies you and passes the parameters in a predefined form. So many people fight, saying that the callback function is really like a BP machine that you belled: Tell someone to numbers, Call when there is something!

So after a callback function writes, there must be a registered action, which is to tell the caller, how do you find the function I write. Some Windows API functions require callback function addresses as one of its parameters, such as settimer, lineddda, enumibjects, and the EnumWindows we have to use.

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

Function EnumwindowsProc (Ahwnd: longint; lparam: longint): boolean; stdcall;

The first is that the function name can be casually, but the type of function parameters is generally not chaotic, the order, data types, etc. have specified, because these are all modes of other programs, they have already specified, but the parameter name Can be called casually. Note that Before you must bring "stdcall",

Stdcall is a standard call, that is to say to call the function using the standard Windows parameter transmission mode.

The writing of the function is very simple. You can use the passing parameters. Just remember, these parameters are given to you, you just need to know what these parameters represent.

Then look at the function of registering the callback function inlet address to the caller. Function Enumwindows (LPENUMFUNC: TFNWNDENUMPROC; LPARAM: LPARAM): BOOL; stdcall;

TfnWndenumProc is actually a pointer type. The LPENUMFUNC is the entrance address of the callback function.

Below is the format that calls ENUMWINDOWS: EnumWindows (@ enumwindowsproc, 0);

By registering the entry address of the callback function, the system can call the callback function, pass the parameters to it, maybe these parameters are what we want.

The functionality of the EnumWindows function is: Enumerate the top window in all programs on the screen, passed the window handle to the callback function as a parameter. When you find a window, call a callback function. The condition of the enumeration is: Either an enumerates all the windows, or the function returns FALSE.

LParam: lparam parameters are programs defined values, which is passed to the callback function.

Take it back and then look at EnumWindowsProc:

Function EnumwindowsProc (Ahwnd: longint; lparam: longint): boolean; stdcall;

When the system finds a window, start calling this callback function, passes the handle of the window as the first parameter, and the value defined by the LPARAM: LPARAM this program in EnumWindows is passed as the second parameter. So we can use the two parameters delivered in the EnumWindowsProc function to do some processing.

Below we create a new program listing the top window of all programs in the system, we have to get the title of the window, to get the window class name.

Get window titles:

Function getWindowText (hwnd: hwnd; lpstring: pchar; nmaxcount: integer): integer; stdcall;

This function function is to copy the title of the window handle to the window of the HWND to a buffer lpstring. Nmaxcount is the maximum number of characters that are copied into the buffer.

To get a window title, you can send a message: WM_GETTEXT, in fact, getWindowText is sent to the WM_GetText message.

To get the window class name:

Function getClassName (hwnd: hwnd; lpclassname: pchar; nmaxcount: integer): integer; stdcall;

Its parameters and the above functions are similar. Not explained in detail.

Let's write a callback function: Enumwindowsproc. Now tell yourself, we already have the value of two parameters. These two parameters are the system to us.

To display window titles and class names, we use a TMEMO control.

First declare the function in the interface part.

Function EnumWindowsProc (AhWnd: Longint; AFORM: TFORM1): Boolean; stdcall;

Note that I will change the second parameter, don't tighten, pay attention to it when I call.

Then define functions in the Implementation section: Function EnumWindowsProc (AhWnd: longint; AFORM: TFORM1): Boolean; var lpszclassname, LPSZWINDOWTEXT: Array [0..254] of char; // Defines two buffers. Begin getWindowText (AHWND, LPSZWINDOTEXT, 254); // Get window title getClassName (AhWnd, LpszclassName, 254); // Get window class name. AFORM.MEMO1.LINES.ADD (STRPAS (LPSZWindowText); AFORM.MEMO1.LINES.ADD (StrPas (LpszclassName)); AFORM.MEMO1.LINES.ADD ('------------- ------- '); Result: = true;

Then you need to do it is to call the EnumWindows function, register the callback function entry address, let the system call the callback function, and list the window. So add a TButton: btn_listwindowprocedure tform1.btn_listwindowclick (sender: TOBJECT); Begin EnumWindows (@ enumwindowsproc, longint (self));

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; btn_listwindow: TButton; procedure btn_listwindowClick (Sender: TObject); private {Private declarations} public {Public declarations} end; var Form1: TForm1;

Function EnumWindowsProc (AhWnd: Longint; AFORM: TFORM1): Boolean; stdcall;

implementation {$ R * .dfm} function EnumWindowsProc (AhWnd: LongInt; AForm: TForm1): boolean; var lpszClassName, lpszWindowText: array [0..254] of char; begin GetWindowText (AhWnd, lpszWindowText, 254); GetClassName (AhWnd , LPSZCLASSNAME, 254; AFORM.MEMO1.LINES.ADD (StrPas (LpszWindowText); AFORM.MEMO1.LINES.ADD (StrPas (LpszclassName)); AMM.MEMO1.LINES.ADD ('------- ------------- '); Result: = true;

Procedure TFORM1.BTN_LISTWINDOWCLICK (Sender: TOBJECT); Begin EnumWindows (@ enumwindowsproc, longint);

End.f9, run, look at the results. It is best that F7 single-step tracking is debugged, see how the callback function is called.

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

New Post(0)