1.Witam was, ostatnio zacząłem interesować się ingerowaniem w inne programy za pomocą C#.. Na potrzeby testów napisałem program, który wpisuje jakiś tekst ("wpisany przez użytkownika") do notatnika, jeżeli ten jest włączony.. Jednak chciałbym zmodyfikować go aby zamiast wpisywania znaków, symulował ich wciśnięcie + klawisze systemowe (Enter, F1,F2 etc.)..
[DllImport("User32.dll", CharSet = CharSet.Auto)]
extern static IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, [In] string lpClassName, [In] string lpWindowName);
[DllImport("User32.dll", EntryPoint = "SendMessage")]
extern static int SendMessageGetTextLength(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
[DllImport("User32.dll")]
public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, string lParam);
[DllImport("User32.dll")]
public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, int lParam);
const int WM_GETTEXTLENGTH = 0x000E;
const int EM_SETSEL = 0x00B1;
const int EM_REPLACESEL = 0x00C2;
public void testAppendText(string text)
{
Process[] notepads = Process.GetProcessesByName("gg");
if (notepads.Length == 0) return;
if (notepads[0] != null)
{
IntPtr editBox = FindWindowEx(notepads[0].MainWindowHandle, new IntPtr(0), "Edit", null);
int length = SendMessageGetTextLength(editBox, WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero);
SendMessage(editBox, EM_SETSEL, length, length);
SendMessage(editBox, EM_REPLACESEL, 1, text);
}
}