果然没人答呵还是我来吧、这里就是列子。 掉用其他窗口也是一样的。 using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; using System.Diagnostics;namespace ShowNotepad { /* 1、启动 notepad.exe “内容” 2、改变记事本标题 3、改变记事本内容
*/ class Program { [DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, string lParam); public const int WM_SETTEXT = 0xC;
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] public static extern IntPtr GetWindow(IntPtr hWnd, int uCmd);
public const int GW_HWNDFIRST = 0; public const int GW_HWNDLAST = 1; public const int GW_HWNDNEXT = 2; public const int GW_HWNDPREV = 3; public const int GW_OWNER = 4; public const int GW_CHILD = 5; //这里用的这个常量、其他的根据字面意识就可以看懂了
调用
SendMessage
PostMessage
掉用其他窗口也是一样的。
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;namespace ShowNotepad
{
/*
1、启动 notepad.exe “内容”
2、改变记事本标题
3、改变记事本内容
*/
class Program
{
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, string lParam);
public const int WM_SETTEXT = 0xC;
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetWindow(IntPtr hWnd, int uCmd);
public const int GW_HWNDFIRST = 0;
public const int GW_HWNDLAST = 1;
public const int GW_HWNDNEXT = 2;
public const int GW_HWNDPREV = 3;
public const int GW_OWNER = 4;
public const int GW_CHILD = 5; //这里用的这个常量、其他的根据字面意识就可以看懂了
static void Main(string[] args)
{
Process proc = Process.Start("notepad.exe");
IntPtr hTextBox = IntPtr.Zero;
if(proc.WaitForInputIdle(5000))
{
hTextBox = GetWindow(proc.MainWindowHandle, GW_CHILD);
if (hTextBox.Equals(IntPtr.Zero))
{
proc.Kill();
throw new ApplicationException("调用出错");
}
SendMessage(proc.MainWindowHandle, WM_SETTEXT, IntPtr.Zero, "设置,记事本窗口的标题");
SendMessage(hTextBox, WM_SETTEXT, IntPtr.Zero, "设置内容\r\n2008-11-31曲滨");
}
}
}
}