C#调用记事本并填写内容的功能实现是如何的呢?在我们编程开发的过程中,实现C#调用记事本要用到什么方法呢?那么这里我们通过实例的形式向你介绍具体的过程:
C#调用记事本并填写内容实例:
- using System.Runtime.InteropServices;
- using System.Diagnostics;
- [DllImport("User32.DLL")]
- public static extern int SendMessage(IntPtr hWnd,
- uint Msg, int wParam, string lParam);
- [DllImport("User32.DLL")]
- public static extern IntPtr FindWindowEx(IntPtr hwndParent,
- IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
- public const uint WM_SETTEXT = 0x000C;
- private void button1_Click(object sender, EventArgs e)
- {
- Process vProcess = Process.Start("notepad.exe");
- while (vProcess.MainWindowHandle ==
- IntPtr.Zero) vProcess.Refresh();
- IntPtr vHandle = FindWindowEx(vProcess.MainWindowHandle,
- IntPtr.Zero, "Edit", null);
- SendMessage(vHandle, WM_SETTEXT, 0, "Zswang 路过");
- }
C#调用记事本并填写内容之发送回车:
- [DllImport("User32.DLL")]
- public static extern int SendMessage(IntPtr hWnd,
- uint Msg, int wParam, string lParam);
- [DllImport("User32.DLL")]
- public static extern int SendMessage(IntPtr hWnd,
- uint Msg, int wParam, int lParam);
- [DllImport("User32.DLL")]
- public static extern IntPtr FindWindowEx(IntPtr hwndParent,
- IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
- public const uint WM_SETTEXT = 0x000C;
- public const uint WM_CHAR = 0x0102;
- public const uint EM_SETSEL = 0x00B1;
- private void button1_Click(object sender, EventArgs e)
- {
- Process vProcess = Process.Start("notepad.exe");
- while (vProcess.MainWindowHandle ==
- IntPtr.Zero) vProcess.Refresh();
- IntPtr vHandle = FindWindowEx(vProcess.MainWindowHandle,
- IntPtr.Zero, "Edit", null);
- SendMessage(vHandle, WM_SETTEXT,
- 0, "Zswang 路过\r\n换行"); // 用"\r\n"换行
- SendMessage(vHandle, EM_SETSEL,
- int.MaxValue, int.MaxValue); // 光标移到最后
- SendMessage(vHandle, WM_CHAR, (int)Keys.Enter, 0);
- SendMessage(vHandle, WM_CHAR, (int)'h', 0);
- SendMessage(vHandle, WM_CHAR, (int)'e', 0);
- SendMessage(vHandle, WM_CHAR, (int)'l', 0);
- SendMessage(vHandle, WM_CHAR, (int)'l', 0);
- SendMessage(vHandle, WM_CHAR, (int)'o', 0);
- }
C#调用记事本并填写内容的基本内容就向你介绍到这里,希望对你了解和学习C#调用记事本并填写内容有所帮助。
【编辑推荐】