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

Source: Internet
Author: User
Tags what callback

How to control window controls on other program forms:

First, I declare that I am a cainiao. I just want to publish a solution to the problem that has been around me for a long time, so that I will not forget it in the future, at the same time, it provides some help to colleagues who do not know this little knowledge. Don't laugh at me. At the same time, in order to express my low level, I will talk about some basic things, which are my understanding and inaccurate.

To control window controls on other program forms, you must first understand what callback functions are. My understanding is as follows:

The callback function is not called by your own program. Instead, you can call other things, such as Windows operating systems and other programs. But I don't know when to call it. The callback function defines the type of parameters and return values according to the caller's requirements. You provide the caller with the entry address of your callback function, then, when an event occurs, the caller can call this function at any time according to the address you provided, and PASS Parameters in a pre-defined form. For example, the callback function is a bit like the bpserver you bring with you: tell someone else the number and call you when something happens!

So after a callback function is written, there must be a registration action to tell the caller how to find the function I wrote. Some Windows API functions require the callback function address as one of their parameters, such as settimer, linedda, enumobjects, And the enumwindows we will use below.

The format of declaring a callback function in Delphi is very simple, for example:

Function enumwindowsproc (ahwnd: longint; lparam: longint): Boolean; stdcall;

First, the function name can be randomly obtained, but the type of function parameters should not be in disorder. The order and data type are specified, because these are called by other programs, they have already specified this, but the parameter names can be randomly called. Note that "stdcall" must be included later ",

Stdcall is a standard call, that is, a function is called by passing standard Windows parameters.

Writing the function body is simple. You can use the passed parameters. Remember that these parameters are sent to you by someone else. You only need to know what these parameters mean.

Let's look at a function that registers the callback function entry address with 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.

The format of calling enumwindows is as follows:
Enumwindows (@ enumwindowsproc, 0 );

By registering the callback function entry address with the system, the system can call the callback function as needed and pass the parameters to it. Maybe these parameters are what we want.

The function of enumwindows is to enumerate top-level windows in all programs on the screen and pass the window handle as a parameter to the callback function. Find a window and call the callback function once. If the enumeration ends, either all windows are enumerated, or the callback function returns false.

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

Let's look at enumwindowsproc again:

Function enumwindowsproc (ahwnd: longint; lparam: longint): Boolean; stdcall;

When the system finds a window, it starts to call the callback function and passes the window handle as the first parameter. In enumwindows, lparam: the value defined by lparam is passed as the second parameter.

So we can use the two passed parameters in the enumwindowsproc function for some processing.

Next we will create a program to list the top-level windows of all programs in the system. We will get the title of the window and the name of the window class.

To obtain the window title, use:

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

This function is used to merge the title of the window whose window handle is hwnd into a buffer lpstring. Nmaxcount is the maximum number of characters in the SWAp buffer.

To get the window title, you can also send the message wm_gettext. In fact, getwindowtext is used to send the wm_gettext message.

To get the window class name, use:

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

The parameter meaning is similar to that of the above function. Not explained in detail.

Write the callback function enumwindowsproc first. Now let us know that we already have two parameter values. These two parameters are provided by the system.

To display the window title 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 have changed the second parameter. It doesn't matter. Pay attention to it when calling it.

Then define the function in implementation:
Function enumwindowsproc (ahwnd: longint; AForm: tform1): Boolean;
VaR
Lpszclassname, lpszwindowtext: array [0 .. 254] of char; // defines two buffers.
Begin
Getwindowtext (ahwnd, lpszwindowtext, 254); // obtain the window title.
Getclassname (ahwnd, lpszclassname, 254); // get the window class name.
AForm. memo1.lines. Add (strpas (lpszwindowtext ));
AForm. memo1.lines. Add (strpas (lpszclassname ));
AForm. memo1.lines. Add ('--------------------');
Result: = true;
End;

Next, you need to call the enumwindows function, register the callback function entry address, and let the system call the callback function to list the window. So add another tbutton: btn_listwindow
Procedure tform1.btn _ listwindowclick (Sender: tobject );
Begin
Enumwindows (@ enumwindowsproc, longint (Self ));
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;
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 ));
AForm. memo1.lines. Add ('--------------------');
Result: = true;
End;

Procedure tform1.btn _ listwindowclick (Sender: tobject );
Begin
Enumwindows (@ enumwindowsproc, longint (Self ));
End;

End.
F9, run and check the result. It is best to track and debug F7 in a single step to see how the callback function is called.

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.