Controls window controls on other program forms must first understand what is a callback function. My understanding is this: The callback function writes from its own program to call, but let other things to call, such as Windows operating systems, 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; first is the function name can be casual, but the type of function parameters is generally unable to come, The order, data types, etc. have specified, because these are all invoked by other programs, they have already specified, but the parameter name can be called casually. Note Before you have to bring "stdcall", StdCall is the standard call, that is, the standard Windows parameter transfer mode is used to invoify the function. 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 a callback function to the system, 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. Go 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, transfer the handle of the window to the first parameter 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 is to copy the title of the window handle 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 a window name name: function getclassname (hwnd: hwnd; lpclassname: pchar; nmaxcount: integer): integer; stdcall; its parameter meaning and the above function is almost. 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 system to us. In order 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; end; 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.