"Kidnapping form": As the name suggests, it is to drag the forms of other applications or even system programs into the form of this program alive. This kind of forced "kidnapping" without the consent of other programs can play a very unique role in certain situations. This article will discuss the technology and application of the "kidnapping" form, and give an example of a complete "bandit" program.
Technically speaking, in order to implement "kidnapping", there must be an MDIForm object in the "robber" program.
The second condition for realizing "kidnapping" is that you must know the handle of the "kidnapped" form. It's not difficult to do this. For example, when the mouse slides on the screen, we can use the API function GetCursorPos to obtain the current position coordinates of the mouse pointer, and then use another API function WindowFromPoint to obtain the handle of the specified point window. Or use FindWindow to get the window handle.
With the above two conditions, the "robber" program can start to "kidnap": first use the GW_CHILD command of the API function GetWindow to find the handle of the first sub-window of the program; then use another API function SetParent to "kidnap" "Kidnapping" form specifies a "new parent", which is a handle to the child window above. Immediately, the two originally unrelated forms had a "father-son" relationship: the "kidnapped" window appeared inside the "robber" form!
2. The safety of “kidnapping”
Indiscriminate "kidnapping" is very dangerous. However, safety can be guaranteed as long as the following two principles are followed:
First, before closing the "robber" program, use the SetParent function to "release" the "kidnapped" form, that is, restore the original "father" of the "kidnapped" person.
Second, don't "kidnap" the system window. System windows generally include: taskbar window, window generated by pressing "Start", window generated by pressing "Ctrl+Alt+Del", window generated by right-clicking the mouse, etc.
"Kidnapping" technology is widely used in VB. For example, controls can be placed inside a container control while the program is running (such as setting a button as a child window of an image or form control), or moving a control from a container. to another in the form.
Code:
Defined in the PRivate of the form
hwndOldParent:HWND;
hwndNotePad:HWND;
Set the window's FormStyle to fsMDIForm
procedure TForm1.Button1Click(Sender: TObject);
begin
hwndNotePad:=FindWindow(PChar('Notepad'),0);
hwndOldParent:=GetParent(hwndNotePad);
Windows.SetParent(hwndNotePad,handle);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
Windows.SetParent(hwndNotePad,hwndOldParent);
end;
For more information, please see: http://lincosoft.go.nease.net/