Open Windows' Calculator in my C# Win Application?

大城市里の小女人 提交于 2019-11-29 18:45:46

问题


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 ?


回答1:


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




回答2:


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




回答3:


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.




回答4:


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);
            }
     }



回答5:


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();

        }
    }



回答6:


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


来源:https://stackoverflow.com/questions/4113755/open-windows-calculator-in-my-c-sharp-win-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!