C builder master advanced
(2) System Window Analyzer
nxyc_twz@163.com
I remember that the author of the "Super Solver", Mr. Liang Yixin, published a series of articles on the computer newspaper, and the very depth of writing, so that I have benefited from a lot. It is said in the text that each time you have to write a new function or a new interface, always like to use SPY to analyze existing similar software, which is very easy to get what kind or message mechanism it adopted, so that it can easily imitate A related function. I often do this in the specific software development work, sometimes it can actually play a half-time role. But this article is not teach you how to use Spy , but to teach you how to write a software similar to SPY .
First, system analysis
To implement SPY features, you must first know how to get the current window handle, and the current window class. The program is mainly used to implement real-time window handle acquisition.
Second, the development frontier
The API functions used here are:
SystemParametersInfo: Acquisition of system parameters
SetWindowPos: Settings Window Location
GetCursorpos: / / Get the location of the cursor
WindowFromPoint: // Take the window handle of the cursor position
GetClassName: // Get the name
SendMessage: // Get window information
Third, design process
1. Initialize the application window to make it always displayed at the top layer:
Void __fastcall tspyform :: formcreate (TOBJECT * SENDER)
{
//initialization
TRECT R;
// Get the desktop size and calculate the location of the program window
SystemParametersInfo (SPI_GetWorkarea, 0, & R, 0);
Left = r.right - width; // Window upper left angle coordinate
TOP = R.BOTTOM - Height; // Window upper left corner coordinate
SetStayontop (this, true); // Set to always display
}
2. Related settings are always in the top-level display function:
Void TspyForm :: SetStayontop (TForm * Form, Bool Value)
{
/ / Set whether the window is always displayed in front
IF (value) setwindowpos (form-> handle, hwnd_topmost, 0, 0, 0, 0, swp_nomove | swp_nosize);
Else SetWindowPos (Form-> Handle, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
}
3. Timer response function:
Void __fastcall tspyform :: timertimer (TOBJECT * Sender)
{
// Use timers to perform every judgment every 0.1 second to achieve real-time display of system information.
TPOINT POS;
HWND HANDLE;
Char BUF [1024];
GetCursorpos (& POS); // Get the position of the cursor
Handle = WindowFromPoint (POS); // Take the window handle of the cursor position
HandleEdit-> text = INTOSTR ((int) handle); // Display window handle
GetClassName (Handle, BUF, 1024); // Get class name
Classedit-> text = buf; // Display class name
SendMessage (Handle, WM_GETTEXT, 1024, Integer (& BUF)); // Get window information
Textedit-> text = buf; // Display window information
}
4. Set whether it is displayed at the top layer:
Void __fastcall tspyform :: ONTOPCHECKBOXCLICK (TOBJECT * SENDER)
{
// Set the front display
SetStayontop (this, ontopcheckbox-> checked);
}