日期:2014-05-17 浏览次数:20933 次
//引用
using System.Runtime.InteropServices;
using System.Diagnostics;
//在类中声明外部函数
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Winapi)]
internal static extern IntPtr GetFocus();
[DllImport("user32.dll", EntryPoint = "FindWindowEx")]
internal static extern IntPtr FindWindowEx(IntPtr hWndParent, IntPtr hWndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll",EntryPoint="SendMessageA")]
internal static extern int SendMessage(IntPtr hWnd, int msg, IntPtr wparam, string lparam);
// 声明消息标识
public const int WM_SETTEXT = 0x000C;
//在Button_click事件中添加以下代码
Process.Start("Notepad");
IntPtr myNotePad = IntPtr.Zero;
myNotePad = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "无标题 - 记事本", null);
IntPtr myNote = IntPtr.Zero;
myNote = FindWindowEx(myNotePad, IntPtr.Zero, "Edit", null);
//SendMessage(myNote, WM_SETTEXT, IntPtr.Zero, strData);
SendMessage(myNote, WM_SETTEXT, IntPtr.Zero, "display str");
#region [ 启动记事本 ]
System.Diagnostics.Process Proc;
try
{
// 启动记事本
Proc = new System.Diagnostics.Process();
Proc.StartInfo.FileName = "notepad.exe";
Proc.StartInfo.UseShellExecute = false;