1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
[DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern bool SendMessage(
IntPtr hWnd, uint msg, UIntPtr wParam, IntPtr lParam);
[DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern IntPtr FindWindowEx(
IntPtr hWndParent, IntPtr hWndChild, string className, string WindowName);
[DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern IntPtr FindWindow(string className, string WindowName);
private void WriteToNotepad(string text)
{
if (String.IsNullOrEmpty(text))
throw new ArgumentException();
Process p;
try
{
p = Process.Start("Notepad.exe");
}catch(Exception){
return;
}
p.WaitForInputIdle(1000);
IntPtr hWnd = p.MainWindowHandle;
//IntPtr hWnd = p.FindWindow("WordPadClass", Nothing);
p.Close();
p = null;
//FindWindow();
//hWnd = FindWindow("WordPad.exe", "Untitled - Notepad");
//if (hWnd == IntPtr.Zero)
// return;
hWnd = FindWindowEx(hWnd, IntPtr.Zero, "Edit", null);
if (hWnd == IntPtr.Zero)
return;
text += '\0';
GCHandle gch = GCHandle.Alloc(text, GCHandleType.Pinned);
SendMessage(hWnd, 0x000C /*WM_SETTEXT*/, UIntPtr.Zero, gch.AddrOfPinnedObject());
gch.Free();
int pos = text.Length - 1;
SendMessage(hWnd, 0x00B1 /*EM_SETSEL*/, (UIntPtr)pos, (IntPtr)pos);
SendMessage(hWnd, 0x0102 /*WM_CHAR*/, (UIntPtr)'\n', IntPtr.Zero);
} |
Partager