C#里调用系统计算器(calc.exe)的代码应该怎么写?

小冬zml2099 2010-08-22 11:18:30
C#里调用系统计算器(calc.exe)的代码应该怎么写?

我想单击窗体上的按钮时,调用出操作系统自带的计算器(calc.exe),请教各位代码怎么写?

我写的代码如下:
private void label13_Click(object sender, EventArgs e)//调用系统计算器
{
System.Diagnostics.ProcessStartInfo Info = new System.Diagnostics.ProcessStartInfo();
//设置外部程序名(计算器)
Info.FileName = "calc.exe";
//设置外部程序的启动参数
Info.Arguments = "";
//设置外部程序工作目录为c:\windows
Info.WorkingDirectory = "C:/WINDOWS/system32";
//声明一个程序类
System.Diagnostics.ProcessProc;
try
{
//
//启动外部程序
//
Proc = System.Diagnostics.Process.Start(Info);
}
catch
{
MessageBox.Show("系统找不到指定的程序文件", "错误提示!");
return;
}
}


生成解决方案的时候系统提示有3个错误:
1、System.Diagnostics.ProcessProc;这句话不能作语句
2、命名空间“System.Diagnostics”中不存在类型或者命名空间名称“ProcessProc”,是否缺少程序集引用?
3、当前上下文中不存在 Proc


请教各位代码怎么写?急急!!!
...全文
1621 22 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
hoodlum0053 2011-02-11
  • 打赏
  • 举报
回复
其实你们都偏离楼主的意思了
修改方法是
把System.Diagnostics.ProcessProc;
修改成System.Diagnostics.Process Proc; Proc前面加个空格就可以了
曾经的拥有 2010-10-24
  • 打赏
  • 举报
回复
我想要这个测试项目可以吗?邮箱是li_sengwangso@126.eom
醒子宇 2010-08-23
  • 打赏
  • 举报
回复
不懂·····
zhongkundehao 2010-08-23
  • 打赏
  • 举报
回复
天天学习
小冬zml2099 2010-08-23
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wuyazhe 的回复:]
激活计算器,然后使用SendKeys.Send("^C");
在自己程序里读剪贴板。

[/Quote]
感谢3楼的回答,我想请教下上文你说的这样能实现吗?怎么实现的?具体的过程是不是这样的:
就是计算器算出结果后,等我关闭计算器,里面的结果就在剪贴板里面了吗?然后直接鼠标在textbox里右键“粘帖”就好了吗?整个过程是这样的吗?如果是,请问具体代码能赐教给我吗?

还有一种方法:我就是想当我算完结果后关闭计算器,同时,结果自动填入进textbox里?这样能不能实现?
如果能,请问代码是怎么来实现的?谢谢!
小冬zml2099 2010-08-23
  • 打赏
  • 举报
回复
6楼的代码我也试验了,没的反应啊!
小冬zml2099 2010-08-23
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 zhaodog 的回复:]
button2_Click 就是取得计算器的数值,
你可以用用个定时器去查询计算器的数值,当取不到窗口句柄的时候就认为计算机被关闭了,把结果放到textbox里就好了
[/Quote]


请问具体该怎么写代码?能否贴出代码来?我主要是不想再加上一个button2了,要是还加个button2,那我不如手动把数值填进textbox里就好了,还调计算器出来算完后还点击button2才输入数值进textbox,觉得这就多余了。可能是我的水平有限,不能参透6楼的代码?刚才9楼提出的解决方案我觉得也很好,就是不知道具体该怎么代码来实现?55555,初学者啊!希望赐教!!!
兔子-顾问 2010-08-23
  • 打赏
  • 举报
回复

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace CSharpWin04
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}

Process pcalc = null;
//启动计算器
private void button1_Click(object sender, EventArgs e)
{
pcalc = Process.Start("calc.exe");
}

//获取文本框的结果
[DllImport("user32.dll", EntryPoint = "FindWindow")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", EntryPoint = "FindWindowEx")]
public static extern IntPtr FindWindowEx(IntPtr hWnd1, IntPtr hWnd2, string lpsz1, string lpsz2);
[DllImport("User32 ")]
public static extern bool SendMessage(IntPtr hWnd, int Msg, int wParam, IntPtr lParam);
public const int WM_GETTEXT = 0xD;
private void button2_Click(object sender, EventArgs e)
{
if (pcalc == null || pcalc.HasExited) return;
IntPtr hEdit = FindWindowEx(pcalc.MainWindowHandle, IntPtr.Zero, "Edit", null);
string w = " ";
IntPtr ptr = Marshal.StringToHGlobalAnsi(w);
if (SendMessage(hEdit, WM_GETTEXT, 100, ptr))
{
MessageBox.Show(Marshal.PtrToStringAnsi(ptr));
}
}
}
}
suncloud1988 2010-08-23
  • 打赏
  • 举报
回复
学习了
guyehanxinlei 2010-08-23
  • 打赏
  • 举报
回复
学习!!
zhaodog 2010-08-23
  • 打赏
  • 举报
回复
button2_Click 就是取得计算器的数值,
你可以用用个定时器去查询计算器的数值,当取不到窗口句柄的时候就认为计算机被关闭了,把结果放到textbox里就好了
小冬zml2099 2010-08-23
  • 打赏
  • 举报
回复
6楼说的我看到了,我也试过了,可没有效果啊,也不是我想要的效果

首先:你的
//获取文本框的结果
[DllImport("user32.dll", EntryPoint = "FindWindow")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", EntryPoint = "FindWindowEx")]
public static extern IntPtr FindWindowEx(IntPtr hWnd1, IntPtr hWnd2, string lpsz1, string lpsz2);
[DllImport("User32 ")]
public static extern bool SendMessage(IntPtr hWnd, int Msg, int wParam, IntPtr lParam);
public const int WM_GETTEXT = 0xD;
是什么意思?
我需要的是获取计算器里计算出来的结果,然后把这个结果放入textbox里,也不需要button2,我也不理解你这个button2_Click事件是干什么的?
请帮忙解释下,谢谢!
c2716266 2010-08-23
  • 打赏
  • 举报
回复
呵呵 人家的代码都贴在这了。Quote=引用 4 楼 wuyazhe 的回复:]
C# code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms……
[/Quote]
兔子-顾问 2010-08-23
  • 打赏
  • 举报
回复
你看不到四楼贴给你的完整测试代码?
「已注销」 2010-08-23
  • 打赏
  • 举报
回复
人家给得很好的代码了
wuyq11 2010-08-23
  • 打赏
  • 举报
回复
[DllImport ( "user32.dll", EntryPoint = "FindWindow", SetLastError = true )]
private static extern IntPtr FindWindow( string lpClassName, string lpWindowName );
[DllImport ( "user32.dll", EntryPoint = "FindWindowEx", SetLastError = true )]
private static extern IntPtr FindWindowEx( IntPtr hwndParent, uint hwndChildAfter, string lpszClass, string lpszWindow );
[DllImport ( "user32.dll", EntryPoint = "SendMessage", SetLastError = true, CharSet = CharSet.Auto )]
private static extern int SendMessage( IntPtr hwnd, uint wMsg, int wParam, int lParam );
[DllImport ( "user32.dll", EntryPoint = "SetForegroundWindow", SetLastError = true )]
private static extern void SetForegroundWindow( IntPtr hwnd )
const uint BM_CLICK = 0xF5;
IntPtr hwndCalc = FindWindow ( null, "计算器" );
if ( hwndCalc != IntPtr.Zero )
{
IntPtr hwnd = FindWindowEx ( hwndCalc, 0, null, "1" );
SetForegroundWindow ( hwndCalc );
SendMessage (hwnd , BM_CLICK, 0, 0 );
}

object o = new System.Data.DataTable().Compute("", "")
WTPMCheng 2010-08-23
  • 打赏
  • 举报
回复
我一般直接启动计算器的进程来实现调用计算器
兔子-顾问 2010-08-23
  • 打赏
  • 举报
回复
如果只是想求解四则运算
string result = new System.Data.DataTable().Compute("1+2+5", "").ToString();
兔子-顾问 2010-08-23
  • 打赏
  • 举报
回复
winxp和win7/vista不同。偏偏问到计算器。就这个在win7彻底重写不同了。

我给你的是例子,至于你是否放在button1,2,3一点关系都没有。至于你想如何用,你不会复制粘贴到合适位置么?

新建个窗体,拖2个按钮。分别是默认名字,注册对应的click事件。对应方法如下。

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace CSharpWin02
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}

Process pcalc = null;
//启动计算器
private void button1_Click(object sender, EventArgs e)
{
pcalc = Process.Start("calc.exe");
}

//获取文本框的结果
[DllImport("user32.dll", EntryPoint = "FindWindow")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", EntryPoint = "FindWindowEx")]
public static extern IntPtr FindWindowEx(IntPtr hWnd1, IntPtr hWnd2, string lpsz1, string lpsz2);
[DllImport("User32 ")]
public static extern bool SendMessage(IntPtr hWnd, int Msg, int wParam, IntPtr lParam);
public const int WM_GETTEXT = 0xD;
[DllImport("user32.dll", EntryPoint = "SetForegroundWindow")]
public static extern int SetForegroundWindow(IntPtr hwnd);
private void button2_Click(object sender, EventArgs e)
{
if (pcalc == null || pcalc.HasExited) return;
string result = "";//最终获得计算器的结果
if (Environment.OSVersion.Version.Major >= 6)//win7 or vista
{
//vista or win7发送ctrl+c获取返回值
SetForegroundWindow(pcalc.MainWindowHandle);
SendKeys.Send("^(C)");
result = Clipboard.GetText();
}
else
{
//xp或更早版本寻找对应控件获得返回值
IntPtr hEdit = FindWindowEx(pcalc.MainWindowHandle, IntPtr.Zero, "#32770", null);
string w = " ";
IntPtr ptr = Marshal.StringToHGlobalAnsi(w);
if (SendMessage(hEdit, WM_GETTEXT, 100, ptr))
{
result = Marshal.PtrToStringAnsi(ptr);
}
}
MessageBox.Show(result);
}
}
}


这个针对win7/xp/vista做了兼容性优化的。
另外,我贴出的代码都是自己测试过的,说效果达不到或没结果的检查一下自己测试方法。实在不会测试的留下邮箱发测试项目给你。
兔子-顾问 2010-08-22
  • 打赏
  • 举报
回复
点击按钮,Findwindow获得Calc的窗体,然后FindWindowEx找到对话框,GetWindowText获得结果。

或是

激活计算器,然后使用SendKeys.Send("^C");
在自己程序里读剪贴板。
加载更多回复(2)

111,093

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧