Skip to content

Instantly share code, notes, and snippets.

@IvanStoychev
Last active June 23, 2020 21:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IvanStoychev/ac5b877080502cfe363b04fd9cbcf987 to your computer and use it in GitHub Desktop.
Save IvanStoychev/ac5b877080502cfe363b04fd9cbcf987 to your computer and use it in GitHub Desktop.
C# code samples
This is a collection of useful pieces of C# code that are ready to be used as they are.
// The following two DllImports are required for the "ExportToNotepad" method to work.
[DllImport("user32.dll")]
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);
/// <summary>
/// Opens notepad.exe and send the given text to it.
/// </summary>
/// <param name="text">The text to send to notepad.exe.</param>
void ExportToNotepad(string text)
{
ProcessStartInfo startInfo = new ProcessStartInfo("notepad");
startInfo.UseShellExecute = false;
Process notepad = Process.Start(startInfo);
notepad.WaitForInputIdle();
IntPtr child = FindWindowEx(notepad.MainWindowHandle, new IntPtr(0), null, null);
SendMessage(child, 0x000c, 0, text);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment