Click here to Skip to main content
15,903,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I was able to show the notepad in the Winform by using a panel and inserting it in the form like this code below and using SetParent API:
C#
[DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

Process proc = Process.Start(
    new ProcessStartInfo()
    {
                                                   //here I put notepad.exe and it works   
        FileName = Environment.GetEnvironmentVariable("windir") + @"\system32\calc.exe", 
        WindowStyle = ProcessWindowStyle.Minimized
    });
            Thread.Sleep(200);
           
            SetParent(proc.MainWindowHandle, this.panel1.Handle);


What I have tried:

My problem is for the calculator, I can't show the Calcultator in the winform it shows me an error.(System.InvalidOperationException)
Thank you.
Posted
Updated 24-Aug-16 9:34am
Comments
Richard MacCutchan 19-Aug-16 8:05am    
System.InvalidOperationException means some instruction in your code has done something illegal. Often caused by a jump to a location that does not contain a valid instruction, even though it is within your application's address space. The only way to fix it is to use your debugger to find out where and why it occurred.
TatsuSheva 19-Aug-16 8:19am    
It is possible to show the calculator in a WinForm ? The error comes here SetParent(proc.MainWindowHandle, this.panel1.Handle); but when I remove the Thread.Sleep(200); the calculator appears but not on the winform
Richard MacCutchan 19-Aug-16 9:17am    
I don't think Windows applications are designed with this in mind, so whether they work in such an environment is not guaranteed.
BillWoodruff 19-Aug-16 9:14am    
Take a look at using the Windows Automation facility to do this; there's a complete article on CodeProject on automating Calc.exe:

http://www.codeproject.com/Articles/141842/Automate-your-UI-using-Microsoft-Automation-Framew
Richard Deeming 19-Aug-16 9:45am    
Are you using Windows 10? AFAIK, the desktop calculator application no longer exists. If you run calc.exe, it launches the "modern app" calculator instead.

Win32 APIs almost certainly won't work with a "modern app".

1 solution

 
Share this answer
 

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