【发布时间】:2015-07-21 12:57:58
【问题描述】:
我的字符串变量中有一些字符串。当我单击 Winform 应用程序中的打开按钮时,它会打开带有该字符串的记事本。记事本应临时创建。如果我关闭记事本,它应该被永久删除。
例如,当我单击“打开”按钮时,会生成字符串“结果”值,记事本应显示结果字符串值。
【问题讨论】:
我的字符串变量中有一些字符串。当我单击 Winform 应用程序中的打开按钮时,它会打开带有该字符串的记事本。记事本应临时创建。如果我关闭记事本,它应该被永久删除。
例如,当我单击“打开”按钮时,会生成字符串“结果”值,记事本应显示结果字符串值。
【问题讨论】:
这应该演示如何打开记事本并将文本放入其中。
这是一个启动记事本进程然后向其中添加文本的简单示例。 将打开一个新的 Notepad.exe 进程,然后将文本“Sending a message, a message from me to you”添加到记事本的文本区域。
SendMessage 的常量 0x000c 记录在这里 http://msdn.microsoft.com/en-us/library/windows/desktop/ms632644(v=vs.85).aspx 。 常量表示SETTEXT,这实际上意味着如果您使用此常量发送多条消息,记事本中的文本将被替换。
using System;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace SendMessageTest
{
class Program
{
[DllImport("user32.dll", EntryPoint = "FindWindowEx")]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("User32.dll")]
public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, string lParam);
static void Main(string[] args)
{
DoSendMessage("Sending a message, a message from me to you");
}
private static void DoSendMessage(string message)
{
Process notepad = Process.Start(new ProcessStartInfo("notepad.exe"));
notepad.WaitForInputIdle();
if (notepad != null)
{
IntPtr child = FindWindowEx(notepad.MainWindowHandle, new IntPtr(0), "Edit", null);
SendMessage(child, 0x000C, 0, message);
}
}
}
}
【讨论】: