Question

I have an application which has a button on Click of button am trying to paste the text already available to Notepad. My application gathers the text first and puts it on the clipboard(this is working perfectly fine), I am facing problem with the Paste part. Here is the code, Please let me know where am i going wrong.

CWnd *pCwnd = FindWindow(NULL, _T("Untitled - Notepad"));
    HWND handle = pCwnd->GetSafeHwnd();
    pCwnd->PostMessageA(WM_PASTE,0,0);

I am using Notepad to test it so the name is ("Untitled - Notepad"). Please help me. Thanks in advance.

Était-ce utile?

La solution

I don't use MFC, but you can probably translate to what you need. The issue is you need to send the message to the edit control, not the main window.

#include <Windows.h>
#include <string>
#include <cstdlib>

int main()
{
    const std::string data("This is some text from the clipboard.");
    HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, data.size() + 1);
    std::memcpy(GlobalLock(hMem), data.c_str(), data.size() + 1);
    GlobalUnlock(hMem);
    OpenClipboard(NULL);
    EmptyClipboard();
    SetClipboardData(CF_TEXT, hMem);
    CloseClipboard();

    HWND mainWindow = FindWindow(NULL, "Untitled - Notepad");
    HWND editWindow = FindWindowEx(mainWindow, NULL, "edit", NULL);
    PostMessage(editWindow, WM_PASTE, 0, 0);
    return 0;
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top