Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. SendMessage API and WM_PASTE

SendMessage API and WM_PASTE

Scheduled Pinned Locked Moved C#
csharpwpfjsonquestion
3 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    John ph
    wrote on last edited by
    #1

    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 and WM_PASTE. Can't get it work. Here is the sample code

    public 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 PostMessage SendMessage, (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

    J 2 Replies Last reply
    0
    • J John ph

      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 and WM_PASTE. Can't get it work. Here is the sample code

      public 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 PostMessage SendMessage, (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

      J Offline
      J Offline
      John ph
      wrote on last edited by
      #2

      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


      1 Reply Last reply
      0
      • J John ph

        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 and WM_PASTE. Can't get it work. Here is the sample code

        public 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 PostMessage SendMessage, (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

        J Offline
        J Offline
        John ph
        wrote on last edited by
        #3

        - 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


        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups