C#⾥调⽤系统计算器()的代码应该怎么写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("");
}
//获取⽂本框的结果
[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));
}
}
}
}
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("");
}
//获取⽂本框的结果
[DllImport("user32.dll", EntryPoint = "FindWindow")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
sendmessage返回值
[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);
}
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。