How to control other application windows in Delphi
How to control other application windows in Delphi
Chen Jianbing, Guangdong Agricultural Management Cadre College
Writing Delphi applications often involves operations on other Windows applications. For example, in a database management system, if financial personnel need to use a calculator, they can call the calculator function included in Windows. Every time they use it, they must open the calculator through "Start/Programs/Accessories/Calculator". Obviously, It is very cumbersome. Of course, you can consider creating a shortcut to "Calculator" on the desktop and defining a hotkey. When you need to use the calculator, press this hotkey to open the "Calculator". However, since "Calculator" is a "non-modal" window program, that is, every time the calculator is run, a "Calculator" window will be opened. The result is that dozens of "calculators" appear on the desktop, not only a large number of It increases the system memory overhead and is inconvenient for users to operate. It can be easily controlled in Delphi and the effect is perfect. The implementation methods are proposed below for two main issues.
1. How to control the design?
Create a new form, put two buttons in the form, their Captions are "Open" (that is, start the "calculator") and "Close" (exit and end), and add Click event processing for them respectively. See form Form1 and unit file Unit1 for details.
Among them, the most important statements are:
fwnd:=FindWindow('SciCalc','Calculator');
The function prototype is (see Delphi help for details):
HWND FindWindow(
LPCTSTR lpClassName, // pointer to class name
LPCTSTR lpWindowName // pointer to window name
);
Here, 'SciCalc' is the class name of the calculator, and 'Calculator' is the window title of the calculator}
setWindowPos(fwnd,HWND_NOTOPMOST,0,0,0,0,SWP_SHOWWINDOW or SWP_NOSIZE or SWP_NOMOVE);
The function prototype is:
BOOL SetWindowPos(
HWND hWnd, // handle of window
HWND hWndInsertAfter, // placement-order handle
int X, // horizontal position
int Y, // vertical position
int cx, // width
int cy, // height
UINT uFlags // window-positioning flags
);
ShowWindow(fwnd,SW_RESTORE); //Show the opened "calculator"
The function prototype is:
BOOL ShowWindow(
HWND hWnd, // handle of window
int nCmdShow // show state of window
);
Ret:=WinExec('c:windowscalc.exe',SW_SHOWNORMAL); //Start the calculator
The function prototype is:
UINT WinExec(
LPCSTR lpCmdLine, // address of command line
UINT uCmdShow // window style for new application
);
Run the Calculator program and check the return value (thereby using the return value to determine possible errors)
2. How to clarify the "class name" of the application?
To control an application, you must first clarify the "class name" of the application. The concept of "class" is already very clear in Delphi programs. For example, create a new form Form1, and the "class name" of the form is TForm1. So how to determine the "class name" of other Windows programs?
Fortunately, Delphi provides a practical tool called Winsight, which is like a mirror that reveals the "class names" of monsters wherever they are.
How to use Winsight is as follows:
⑴ Run Winsight (the program name is ws32.exe, in the same directory as the main file delphi32.exe), see Figure ①;
⑵ Select "Follow Focus" in "Spy" from the Winsight menu, as shown in Figure ②;
⑶Run the “calculator” program;
⑷The target is displayed in Winsight, as shown in Figure ③.
Software environment: Chinese Win98/Chinese Delphi5.0.