|
■No75448 (Sendkeys さん) に返信 >>対象者が不特定なのであれば、SeneKeys を使うよりも、 >>EM_REPLACESEL や EM_SETTEXT で送りつけた方が安定するかも。 > 勉強になりました。
EM_SETTEXT → WM_SETTEXT の間違いです。m(_ _)m
以下サンプル:
using System; using System.Diagnostics; using System.Runtime.InteropServices; using System.Windows.Forms;
namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); this.textBox1.Text = "FirstIndex^SecondIndex\r\n"; }
[DllImport("user32", CharSet = CharSet.Unicode)] static extern IntPtr FindWindowEx(IntPtr hWnd, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
private void button1_Click(object sender, EventArgs e) { var message = this.textBox1.Text; foreach (var p in Process.GetProcessesByName("notepad")) { var h = FindWindowEx(p.MainWindowHandle, IntPtr.Zero, "Edit", null); if (h != IntPtr.Zero) { Sample.SetText(h, message); //Sample.AppendText(h, message); } } }
public static class Sample { [DllImport("user32", CharSet = CharSet.Unicode)] static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam); [DllImport("user32", CharSet = CharSet.Unicode)] static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
public static void SetText(IntPtr hwnd, string text) { const int WM_SETEXT = 0xC; if (text == null) text = ""; SendMessage(hwnd, WM_SETEXT, IntPtr.Zero, text); }
public static void AppendText(IntPtr hwnd, string text) { const int WM_GETTEXTLENGTH = 0xE; const int EM_SETSEL = 0xB1; const int EM_REPLACESEL = 0xC2; if (text == null) text = ""; IntPtr textLength = SendMessage(hwnd, WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero); SendMessage(hwnd, EM_SETSEL, textLength, textLength); SendMessage(hwnd, EM_REPLACESEL, IntPtr.Zero, text); } } } }
|