The so-called "kidnapping" is to put other Win32 Program The form is embedded into our hosted winform. Many Java and Delphi versions and WPF have been used on the Internet. Here I will add C # versions.
Define the required Win32 API
[Dllimport ( " User32.dll " )]
Private Static Extern Int Setparent (intptr hwndchild, intptr hwndparent );
[dllimport ( " user32.dll " )]
private static extern bool showwindowasync (intptr hwnd, int ncmdshow);
[Dllimport ("User32.dll", Setlasterror= True)]
Private Static Extern BoolPostmessage (intptr hwnd,UintMSG,IntWparam,IntLparam );
[Dllimport ( " User32.dll " , Entrypoint = " Setwindowpos " )]
Private Static Extern Bool Setwindowpos (intptr hwnd, Int Hwndinsertafter,
Int X, Int Y, Int CX, Int Cy, Uint Uflags );
[dllimport ( " user32.dll " )]
private static extern int sendmessage (intptr hwnd, uint MSG, int wparam, int lparam);
[dllimport ( " user32.dll " , setlasterror = true , charset = charset. auto)]
private static extern uint setwindowlong (intptr hwnd, int nindex, uint newlong);
[dllimport ( " user32.dll " , setlasterror = true , charset = charset. auto)]
private static extern uint getwindowlong (intptr hwnd, int nindex);
[Dllimport ("User32.dll", Charset=Charset. Auto)]
Private Static Public Extern BoolShowwindow (intptr hwnd,ShortState );
Private Const Int Hwnd_top = 0x0 ;
Private Const Int Wm_command = Zero X 0112 ;
Private Const Int Wm_qt_paint = 0xc2dc ;
Private Const Int Wm_paint = 0x000f ;
Private Const Int Wm_size = Zero X 0005 ;
Private Const Int Swp_framechanged = Zero X 0020 ;
Start the program we need to kidnap Private Void Form1_load ( Object Sender, eventargs E)
{
P = New Process ();
// Programs to be started
P. startinfo. filename = @" C: \ windows \ system32 \ notepad.exe " ;
// To be beautiful, minimize the program at startup
P. startinfo. windowstyle = Processwindowstyle. minimized;
// Start
P. Start ();
// Wait. Otherwise, the handler of the Startup Program has not been created and cannot be controlled.
Thread. Sleep ( 1000 );
// Programs to maximize startup
Showwindow (P. main1_whandle ,( Short ) Showwindowstyles. sw_maximize );
// Set the parent window of the kidnapped Program
Setparent (P. main1_whandle, This . Handle );
// Change size
Resizecontrol ();
}
// Control the position and size of the embedded Program
Private Void Resizecontrol ()
{
Sendmessage (P. main1_whandle, wm_command, wm_paint, 0 );
Postmessage (P. main1_whandle, wm_qt_paint, 0 , 0 );
Setwindowpos (
P. main1_whandle,
Hwnd_top,
0 - 5 , // Set the offset to cover the menu of the original window
0 - 41 ,
( Int ) This . Width,
( Int ) This . Height + 36 ,
Swp_framechanged );
Sendmessage (P. main1_whandle, wm_command, wm_size,0);
}
Private VoidForm1_sizechanged (ObjectSender, eventargs E)
{
Resizecontrol ();
}
It should be noted that the resizecontrol () method is very important, otherwise it will not achieve our expected results.