문제

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.

도움이 되었습니까?

해결책

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;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top