11

I know I can open Windows Calculator with the following code :

System.Diagnostics.Process.Start("calc");

But I wanna open it in my C# Win Application, i.e : I don't want to open it in the independent window, I wanna open it in my window.
How can I do it ?

Mohammad Dayyan
  • 21,578
  • 41
  • 164
  • 232

6 Answers6

15

You cannot embed another application into your form.

However, you can move the calculator window on top of your form and set your form as its parent. This might accomplish the visual effect that you're looking for. You might check into the SetParent API function. For example:

System.Diagnostics.Process p = System.Diagnostics.Process.Start("calc.exe");
p.WaitForInputIdle();
NativeMethods.SetParent(p.MainWindowHandle, this.Handle);

A better solution might be just to roll your own calculator control in C# if you really need that functionality embedded in your app. Banging together a simple calculator really isn't at all difficult, and its infinitely customizable to do and look exactly as you want.

Something like this, perhaps, would be a good starting point if you wanted to roll your own calculator: http://www.codeproject.com/KB/cs/Scientific_Calculator.aspx

And I've always thought this type of a control would be ridiculously useful someday if I ever wrote an app that relied heavily on numerical input: http://www.codeproject.com/KB/miscctrl/C__Popup_Calculator.aspx

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • the calculator will open in the independent window. – Ali Vojdanian Jun 03 '12 at 10:03
  • @aliboy38 Yes, of course it will. Where did I ever say otherwise? – Cody Gray - on strike Jun 03 '12 at 10:09
  • I thought it will be open in the winform. because that the question was how to open it in the C# win and your answer marked as answer. btw you used SetParent and MainWindowHandle. so what is the difference between your answer and the question because that code will show calculator too. Thanks – Ali Vojdanian Jun 03 '12 at 10:16
  • 1
    @aliboy38 I have the sneaking suspicion that you never actually read my whole answer, but just looked at the block of code. The answer starts with the line *"You cannot embed another application into your form."* That's basically saying "What you're asking for in the question is not possible." I then go on to provide an admittedly hackish workaround—reparenting the calculator window to your application's window. You do that by calling the `SetParent` function, the documentation for which I linked to in the paragraph immediately preceding the code block. – Cody Gray - on strike Jun 03 '12 at 10:28
  • Yes, `Process.Start` is the same. That's not the point of the answer. The point of the answer is the `SetParent` function. – Cody Gray - on strike Jun 03 '12 at 10:28
  • If you won't be mad i have another question. what SetParent function do in your code? – Ali Vojdanian Jun 03 '12 at 10:31
  • @aliboy38 http://msdn.microsoft.com/en-us/library/ms633541.aspx *"Changes the parent window of the specified child window."* – Cody Gray - on strike Jun 03 '12 at 10:32
5

MS Windows Calculator is not a GUI control, it is a standalone application. If you are looking for a .NET calculator control, there are some commercial controls from third party vendors, for example

here

http://download.cnet.com/Softgroup-Net-Calculator-Control/3000-10250_4-10909672.html

or here

http://www.softpedia.com/get/Programming/Components-Libraries/Net-Calculator-Control.shtml

Doc Brown
  • 19,739
  • 7
  • 52
  • 88
5

You can pinvoke SetParent(), the child window handle should be the Process.MainWindowHandle of Calc, the parent window should be the handle of the window in which you want to embed it. Form.Handle gives you that value. You'll also need MoveWindow to get the window in the right place. Use pinvoke.net to get the required pinvoke declarations.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Would you please post a sample code for better understanding ? Thanks – Mohammad Dayyan Nov 06 '10 at 16:27
  • 2
    Google +setparent +mainwindowhandle and take the first hit. – Hans Passant Nov 06 '10 at 16:41
  • Where in the SDK documentation does it state that child windows must belong to the same process? – jonsb Nov 09 '10 at 11:38
  • @hans passant: Do you have a URL? There's nothing here about processes: http://msdn.microsoft.com/en-us/library/ms633541(VS.85).aspx – jonsb Nov 09 '10 at 13:38
  • 1
    @jonsb - can't find one. This seems to have changed, the docs for SetWindowLongPtr now mention a restriction for XP only and direct to not use GWLP_HWNDPARENT anymore. I'll edit accordingly. – Hans Passant Nov 09 '10 at 14:16
2
using System.Diagnostics;

try
     {
         Process p = null;
         if (p == null)
          {
            p = new Process();
            p.StartInfo.FileName = "Calc.exe";
            p.Start();
          }
         else
             {
                p.Close();
                p.Dispose();
             }
         }
        catch (Exception e)
            {
                MessageBox.Show("Excepton" + e.Message);
            }
     }
callmebob
  • 6,128
  • 5
  • 29
  • 46
Ram
  • 21
  • 5
2

try below; run for me.

    using System.Diagnostics;

    private void button1_Click(object sender, EventArgs e) 

      {
        string filename= "calc.exe";

        Process runcalc= Process.Start(filename);

        while (runcalc.MainWindowHandle == IntPtr.Zero)
        {

            System.Threading.Thread.Sleep(10);

            runcalc.Refresh();

        }
    }
mui
  • 21
  • 1
2
System.Diagnostics.Process.Start("calc.exe");
Ali Hamza
  • 55
  • 4