SendMessage API and WM_PASTE
-
I have been trying to write the clipboard content to an external windows application (currently running process) from a C#(WPF) application using
SendMessage
API andWM_PASTE
. Can't get it work. Here is the sample codepublic const int WM_PASTE = 0x0302;
[DllImport("User32.dll")]
public static extern Int32 FindWindow(String lpClassName,String lpWindowName);[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern int SendMessage(int hWnd, int msg, int wParam, IntPtr lParam);private void btn_Click(object sender, System.EventArgs e)
{
int hwnd=0;
hwnd = FindWindow(null, "Untitled - Notepad");if (hwnd != 0) { Clipboard.SetText("sample text from clipboard"); hwnd = FindWindow(null, "Untitled - Notepad"); SendMessage(hwnd, WM\_PASTE, 0, IntPtr.Zero); }
}
Note: have to Notepad app opened to test the code tried some other possible combinations
SendMessage, WM_PASTE SendMessage, WM_CHAR PostMessage, WM_PASTE PostMessage, WM_CHAR
An alternate way to send keystrokes to an extern app using SendMessage and PostMessageSendMessage, (WM_KEYDOWN, WM_KEYUP) For CTRL+V PostMessage, (WM_KEYDOWN, WM_KEYUP) For CTRL+V
Windows.System.Forms.SendKeys.SendWait("^V")
-> works but not always any idea?- Regards -
J O N
A good thing is a bad thing if it keeps you from the best thing. - Dr. Adrian Rogers
modified on Wednesday, April 20, 2011 6:17 AM
-
I have been trying to write the clipboard content to an external windows application (currently running process) from a C#(WPF) application using
SendMessage
API andWM_PASTE
. Can't get it work. Here is the sample codepublic const int WM_PASTE = 0x0302;
[DllImport("User32.dll")]
public static extern Int32 FindWindow(String lpClassName,String lpWindowName);[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern int SendMessage(int hWnd, int msg, int wParam, IntPtr lParam);private void btn_Click(object sender, System.EventArgs e)
{
int hwnd=0;
hwnd = FindWindow(null, "Untitled - Notepad");if (hwnd != 0) { Clipboard.SetText("sample text from clipboard"); hwnd = FindWindow(null, "Untitled - Notepad"); SendMessage(hwnd, WM\_PASTE, 0, IntPtr.Zero); }
}
Note: have to Notepad app opened to test the code tried some other possible combinations
SendMessage, WM_PASTE SendMessage, WM_CHAR PostMessage, WM_PASTE PostMessage, WM_CHAR
An alternate way to send keystrokes to an extern app using SendMessage and PostMessageSendMessage, (WM_KEYDOWN, WM_KEYUP) For CTRL+V PostMessage, (WM_KEYDOWN, WM_KEYUP) For CTRL+V
Windows.System.Forms.SendKeys.SendWait("^V")
-> works but not always any idea?- Regards -
J O N
A good thing is a bad thing if it keeps you from the best thing. - Dr. Adrian Rogers
modified on Wednesday, April 20, 2011 6:17 AM
alright, getting the child window of the notepad application works which is the edit control. we can do a PASTE only in the edit panel of the notepad application. modified code below...
GetWindow_Cmd.GW_CHILD = 5
int hwnd=0;
int hwndChild=0;hwnd = FindWindow(null, "Untitled - Notepad");
if (hwnd != 0)
{
Clipboard.SetText("sample text from clipboard");
hwnd = FindWindow(null, "Untitled - Notepad");
hwndChild = GetWindow(hwnd, GetWindow_Cmd.GW_CHILD);
SendMessage(hwndChild , WM_PASTE, 0, IntPtr.Zero);
}now, how do i find the window handle of the static edit control of "winword" and "mspaint" application same approach doesn't work... any idea?
- Regards -
J O N
A good thing is a bad thing if it keeps you from the best thing. - Dr. Adrian Rogers
-
I have been trying to write the clipboard content to an external windows application (currently running process) from a C#(WPF) application using
SendMessage
API andWM_PASTE
. Can't get it work. Here is the sample codepublic const int WM_PASTE = 0x0302;
[DllImport("User32.dll")]
public static extern Int32 FindWindow(String lpClassName,String lpWindowName);[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern int SendMessage(int hWnd, int msg, int wParam, IntPtr lParam);private void btn_Click(object sender, System.EventArgs e)
{
int hwnd=0;
hwnd = FindWindow(null, "Untitled - Notepad");if (hwnd != 0) { Clipboard.SetText("sample text from clipboard"); hwnd = FindWindow(null, "Untitled - Notepad"); SendMessage(hwnd, WM\_PASTE, 0, IntPtr.Zero); }
}
Note: have to Notepad app opened to test the code tried some other possible combinations
SendMessage, WM_PASTE SendMessage, WM_CHAR PostMessage, WM_PASTE PostMessage, WM_CHAR
An alternate way to send keystrokes to an extern app using SendMessage and PostMessageSendMessage, (WM_KEYDOWN, WM_KEYUP) For CTRL+V PostMessage, (WM_KEYDOWN, WM_KEYUP) For CTRL+V
Windows.System.Forms.SendKeys.SendWait("^V")
-> works but not always any idea?- Regards -
J O N
A good thing is a bad thing if it keeps you from the best thing. - Dr. Adrian Rogers
modified on Wednesday, April 20, 2011 6:17 AM
- ok, i think, using WM_PASTE to paste the contents into word or paint is somewhat complex. not able to find a way to get the handle of the edit panel. a simple solution is adopted and it works
Win32.SendMessage(hWnd, Win32.WM_SYSCOMMAND, (IntPtr)Win32.SC_RESTORE, IntPtr.Zero);
Win32.SetForegroundWindow(hWnd);
System.Threading.Thread.Sleep(100);
System.Windows.Forms.SendKeys.SendWait("^v");- Regards -
J O N
A good thing is a bad thing if it keeps you from the best thing. - Dr. Adrian Rogers