Open Windows' Calculator in my C# Win Application?
Asked Answered
P

6

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 ?

Prowl answered 6/11, 2010 at 15:36 Comment(0)
T
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

Trieste answered 6/11, 2010 at 15:44 Comment(7)
the calculator will open in the independent window.Blackhead
@aliboy38 Yes, of course it will. Where did I ever say otherwise?Trieste
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. ThanksBlackhead
@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.Trieste
Yes, Process.Start is the same. That's not the point of the answer. The point of the answer is the SetParent function.Trieste
If you won't be mad i have another question. what SetParent function do in your code?Blackhead
@aliboy38 msdn.microsoft.com/en-us/library/ms633541.aspx "Changes the parent window of the specified child window."Trieste
R
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

Rebate answered 6/11, 2010 at 15:41 Comment(0)
P
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.

Preoccupied answered 6/11, 2010 at 16:3 Comment(5)
Would you please post a sample code for better understanding ? ThanksProwl
Google +setparent +mainwindowhandle and take the first hit.Preoccupied
Where in the SDK documentation does it state that child windows must belong to the same process?Nefen
@hans passant: Do you have a URL? There's nothing here about processes: msdn.microsoft.com/en-us/library/ms633541(VS.85).aspxNefen
@Nefen - 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.Preoccupied
J
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);
            }
     }
Jape answered 27/6, 2016 at 11:38 Comment(0)
S
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();

        }
    }
Selfimprovement answered 9/9, 2016 at 6:37 Comment(0)
A
2
System.Diagnostics.Process.Start("calc.exe");
Adolpho answered 14/4, 2018 at 7:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.