Pages

Monday, September 6, 2010

clipboard to Notepad in c# Example

This can be a bit tricky in some scenarios, but it's actually quite simple and easy to do. Below is an example on how to get some text using a text box, (called uxData in this case), open Notepad from code, and to paste the text from the clipboard to Notepad.

public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}

[DllImport("user32.dll", SetLastError = true)]
private static extern bool BringWindowToTop(IntPtr hWnd);

private void OnClicked_PasteToNotepad(object sender, EventArgs e) {

// Let's start Notepad
Process process = new Process();
process.StartInfo.FileName = "C:\\Windows\\Notepad.exe";
process.Start();

// Give the process some time to startup
Thread.Sleep(10000);

// Copy the text in the datafield to Clipboard
Clipboard.SetText(uxData.Text, TextDataFormat.Text);

// Get the Notepad Handle
IntPtr hWnd = process.Handle;

// Activate the Notepad Window
BringWindowToTop(hWnd);

// Use SendKeys to Paste
SendKeys.Send("^V");
}
}

No comments:

Post a Comment