CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    Join Date
    Apr 2001
    Location
    Las Vegas
    Posts
    539

    Question Pasting to any window

    I managed to paste text that is currently in the clipboard using this:

    CString csText = "Test";
    HGLOBAL hglbCopy;

    if(OpenClipboard())
    {
    EmptyClipboard();
    TCHAR *wcBuffer = 0;
    hglbCopy = GlobalAlloc( GMEM_MOVEABLE, (csText.GetLength() + 1) * sizeof (TCHAR));

    wcBuffer = (TCHAR*)GlobalLock(hglbCopy);
    _tcscpy(wcBuffer, csText);

    GlobalUnlock(hglbCopy);
    SetClipboardData(CF_TEXT, hglbCopy);
    CloseClipboard();
    }
    }

    // And then:

    POINT pt;
    GetCursorPos(&pt);
    CWnd* editControl;
    editControl = WindowFromPoint(pt);
    editControl->GetFocus();
    editControl->SendMessage(WM_PASTE, 0, 0);


    It does work for notepad but it does not work for other apps ( for example - Word and a textbox in a browser window)


    I've also tried to programatically click Ctrl+V and cause a paste like this:
    editControl->SendMessage(WM_CHAR, 22, 0x56);

    But it seems to have the same problem - only works in some places (notepad...)


    Any idea how can I make this work for any input control ?
    (just like Ctrl+V works for every control)
    Last edited by Yovav; April 5th, 2009 at 12:00 PM.
    Best Regards - Yovav

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Pasting to any window

    Try to use SendInput API rather than sending WM_CHAR message.
    Victor Nijegorodov

  3. #3
    Join Date
    Apr 2001
    Location
    Las Vegas
    Posts
    539

    Re: Pasting to any window

    Still the same results - works on notepad but not in Word or in browser's textboxes

    This is what I've used:
    (should I try to change anything ?)


    INPUT *key;
    key = new INPUT;
    key->type = INPUT_KEYBOARD;
    key->ki.wVk = VK_CONTROL; // virt key code
    key->ki.dwFlags = 0; // flags—see doc
    key->ki.time = 0; // time stamp, 0 = dflt
    key->ki.wScan = 0; // hw scan code
    key->ki.dwExtraInfo = 0; // app-defined
    SendInput(1,key,sizeof(INPUT));
    //key->ki.dwExtraInfo = KEYEVENTF_KEYUP;
    //SendInput(1,key,sizeof(INPUT));

    key = new INPUT;
    key->type = INPUT_KEYBOARD;
    key->ki.wVk = 'V'; // virt key code
    key->ki.dwFlags = 0; // flags—see doc
    key->ki.time = 0; // time stamp, 0 = dflt
    key->ki.wScan = 0; // hw scan code
    key->ki.dwExtraInfo = 0; // app-defined
    SendInput(1,key,sizeof(INPUT));
    //key->ki.dwExtraInfo = KEYEVENTF_KEYUP;
    //SendInput(1,key,sizeof(INPUT));
    Best Regards - Yovav

  4. #4
    Join Date
    Apr 2001
    Location
    Las Vegas
    Posts
    539

    Re: Pasting to any window

    Also, FYI: I'm running Vista 64bit
    Best Regards - Yovav

  5. #5
    Join Date
    Jun 2004
    Posts
    1,352

    Re: Pasting to any window

    Quote Originally Posted by VictorN View Post
    Try to use SendInput API rather than sending WM_CHAR message.




    I do something like that in my Apps: However I go to ClassWizard and created a 'Member Control Variabale of the Edit box of CEdit type':


    Code:
    	CString source;
    	//put your text in source
    
    	source = "Line 1   :\r\n"       
    	                "Line2    :\r\n"       
    		 "...etc,   :\r\n"       
    		".....";				 
    			 
    			 
    	if(OpenClipboard())
    	{
    		HGLOBAL clipbuffer;
    		char * buffer;
    		EmptyClipboard();
    		clipbuffer = GlobalAlloc(GMEM_DDESHARE, source.GetLength()+1);
    		buffer = (char*)GlobalLock(clipbuffer);
    		strcpy(buffer, LPCSTR(source));
    		GlobalUnlock(clipbuffer);
    		SetClipboardData(CF_TEXT,clipbuffer);
    		CloseClipboard();
    	}
    	
    	this->m_CntrlNotes.Paste();      //m_nCntrNotes is a CEdit Control Variable assigned in ClassWizard
    	this->UpdateData(); 
    	AfxMessageBox(m_Notes); //m_Notes is a varaiable assigned to the edit box in ClassWizard
    This will copy a string to the clipboard and into an Edit control assigned to an edit box via Classwizard.
    Last edited by ADSOFT; April 5th, 2009 at 03:47 PM.
    Rate this post if it helped you.

  6. #6
    Join Date
    Apr 2001
    Location
    Las Vegas
    Posts
    539

    Re: Pasting to any window

    Still the same results - works on notepad but not in Word or in browser's textboxes

    I've added this:

    ((CEdit*)editControl)->Paste();

    instead of editControl->SendMessage(WM_PASTE, 0, 0L);


    I wonder if there is some kind of Vista built-in security feature that is preventing me from doing this...
    Best Regards - Yovav

  7. #7
    Join Date
    Jun 2004
    Posts
    1,352

    Re: Pasting to any window

    Are you trying to paste directly into MS WORD?

    Also right into Html code? or into the address bar of the Browser?
    Rate this post if it helped you.

  8. #8
    Join Date
    Apr 2001
    Location
    Las Vegas
    Posts
    539

    Re: Pasting to any window

    Yes I was trying to paste into Word,

    When I hit a hot key my function do copy the data into clipboard, but on Word it does not do the paste,
    however, if I hit Ctrl+V it does copy my value from clipboard,

    Also, it does work for the address line of Internet Explorer,
    but not for TextBox inside web sites - for example: the search box at the google web page,

    Thanks again.
    Best Regards - Yovav

  9. #9
    Join Date
    Jun 2004
    Posts
    1,352

    Re: Pasting to any window

    Well the Edit box in a browser in NOT a CEdit control, the browser has over written the Ctrl-Paste function. ... I doubt you have access to that via, MFC.


    MSWORD is another story. The client area probobly not a CEdit box.


    I was doing some stuff a few month's ago where I had to get the address line of the brower, or a specif instance of the Browser. ... i.e, several sites were open.

    I had to use FindWindow, and get into the use of that function. It gets pretty messy.

    Regarding getting data into MSWORD you might have more success with that. What I posted only works with CEdit Boxes.

    Regarding geting data in the edit boxes of the browers client area. I'm not sure if we have access to that via MFC. ..... Like I said I don't do much Internet stuff, so I could be wrong, I just know that those boxes in the Client area are not CEdit boxes.

    One way of getting data into the Google Box would be to download the webpage into a file edit it and call that html file via the browser, basically scrub the file.
    Rate this post if it helped you.

  10. #10
    Join Date
    Jun 2004
    Posts
    1,352

    Re: Pasting to any window

    Well here you go, I think this get access to the Client area in the Browser.

    http://www.codeguru.com/cpp/i-n/iepr...le.php/c13065/
    Rate this post if it helped you.

  11. #11
    Join Date
    Dec 2008
    Posts
    114

    Re: Pasting to any window

    Quote Originally Posted by Yovav View Post
    Yes I was trying to paste into Word,
    Simply use COM

  12. #12
    Join Date
    Jul 2005
    Posts
    266

    Re: Pasting to any window

    Your SendInput sequence is not entirely correct. Use this one it should work ( works for any of my applications )
    Code:
    void SendPaste()
    {
    	INPUT Input[4];
    	memset(Input, 0, sizeof(Input));
    	Input[0].type = INPUT_KEYBOARD;
    	Input[0].ki.wVk = VK_CONTROL;
    	Input[0].ki.dwFlags = KEYEVENTF_EXTENDEDKEY;
    	Input[1].type = INPUT_KEYBOARD;
    	Input[1].ki.wVk = 'V';
    	Input[2].type = INPUT_KEYBOARD;
    	Input[2].ki.wVk = 'V';
    	Input[2].ki.dwFlags = KEYEVENTF_KEYUP;
    	Input[3].type = INPUT_KEYBOARD;
    	Input[3].ki.wVk = VK_CONTROL;
    	Input[3].ki.dwFlags = KEYEVENTF_KEYUP | KEYEVENTF_EXTENDEDKEY;
    	SendInput(4, Input, sizeof(Input[0]));
    }

  13. #13
    Join Date
    Apr 2001
    Location
    Las Vegas
    Posts
    539

    Re: Pasting to any window

    I like your SendInput sequence better, but I'm getting the same results,

    if you say its working for you then I really start to think that it is something related to my Vista 64bit

    I will try it in some other Windows emulators with Virtual PC and see,

    Thanks.
    Best Regards - Yovav

  14. #14
    Join Date
    Jul 2005
    Posts
    266

    Re: Pasting to any window

    Can you show us the exact code you are using at the moment (not only the SendInput sequence ) but all the parts . maybe if you could attach a sample project we could test on our pcs it would help debug the issue.

  15. #15
    Join Date
    Jun 2004
    Posts
    1,352

    Re: Pasting to any window

    I gave him some links that expllain why he can't post information to Word (think about it, do you really think that Word is just one bigcCEdit Class, .. maybe a Richedit control, but most likely something derivied from CWND!, I havn't read the article but I'm sure he addresses it) .

    Regarding posting information into forms: He has to get the content of the web page and parse the contents via CHtmlView->GetHtmlDocuement(); It's not a trivial task, it requires somes knowledge of COM and IHtmlDocument2 with one of the last SDK's if you are using V6 (which I' am and are having problems getting the SDK created in 2003 for V6, to work) or compiler newere than V6, since accessing a HTML Form requires having access to IHTMLelementPTR.


    In a nutshell those are not CEdit Classes he is trying to communicate with, sendig a message via CEdit is not going to get you anywhere with those two apps he is trying to communicate with: .... it like the old saying, "Don't send a BOY to do a mans job". Your going to have to roll up your sleeves to get information to WORD and a HTML Form!!!
    Rate this post if it helped you.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured