Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
proce = Process.Start("mspaint.exe");
proce.WaitForInputIdle();
SetParent(proce.MainWindowHandle, splitContainer1.Panel2.Handle);
SendMessage(proce.MainWindowHandle, WM_SYSCOMMAND, SC_MAXIMIZE, 0);

this is my code for integrate Paint to form,but it only runs when I set breakpoint at
C#
SetParent(proce.MainWindowHandle, splitContainer1.Panel2.Handle);

Where I was wrong?
Posted

1 solution

You were wrong to believe the Microsoft documentation for WaitForInputIdle!

In my experience the MainWindowHandle is usually zero when the process becomes idle for the first time. For example, on my system mspaint gave a valid MainWindowHandle 250milliseconds after input idle and for some applications the delay can be much longer than this. I once saw ~10 seconds for the Videolan Media player.

I think the only way to solve the problem is to use WaitForInputIdle but then poll MainWindowHandle, say once every 100ms, until it is non zero. It would be advisable to work in some kind of timeout in case the handle never appears.

Alan.
 
Share this answer
 
Comments
son0nline 21-Aug-14 6:00am    
Hi Alan!
I use this code and still not work
while (IntPtr.Equals(proce.MainWindowHandle,IntPtr.Zero))
{
System.Threading.Thread.Sleep(100);
}
Alan N 21-Aug-14 6:49am    
I missed out a crucial detail, sorry. Insert a call to the Refresh() method to update the process properties each time around the loop.
while (IntPtr.Equals(proce.MainWindowHandle,IntPtr.Zero))
{
System.Threading.Thread.Sleep(100);
proce.Refresh();
}
son0nline 21-Aug-14 7:17am    
Thanks you, it works!
by the way please let me ask, when I tried it with winword.exe word form still outside my form
Alan N 21-Aug-14 9:10am    
That's a shame. My rather ancient version of Word (2002) can be hosted by setting the parent and if there are multiple documents open then the MainWindowHandle refers to the one at the top of the Z-Order. Perhaps with yours the MainWindowHandle is not one of the document windows. It's difficult to come up with any intelligent guesses and I think you will have to investigate further using a handle spy tool such as Spy++. I have been using Winspector which works well if a little slowly.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900