Thema: Clipboard
Einzelnen Beitrag anzeigen
Alt 11.03.2009, 11:39   #1
Clipboard
Bluesteel Bluesteel ist offline 11.03.2009, 11:39

Text in die Windows-Zwischenablage schreiben und daraus lesen

Ist ne ganz praktische sache denke ich xD

so hier nun der code

Code:
/*

Read/write text from/to the Windows clipboard

Clear clipboard with EmptyClipboard() function



Dependencies: CString




*/



CString GetClipboardText()

{

    HANDLE  String ;

    char    ret_text[4096];



    // open clipboard for task

    if (OpenClipboard(0))

    {

        // try reading if successful

        if ((String  = GetClipboardData(CF_TEXT))!=0)

        {

            strncpy(ret_text, (char *) String , 4096);

            ret_text[4095] = '\0';

            CloseClipboard();

            return ret_text ;

        }

        else

        {

            CloseClipboard();

            return "";

        }

    }

    return "";

}



bool CopyToClipboard(CString strText)

{

    bool bRet = true;



    if (strText.GetLength() > 0)

    {

        HGLOBAL  hGlobalMemory;

        LPVOID    lpGlobalMemory;



        // allocate global memory

        hGlobalMemory = GlobalAlloc(GHND, strText.GetLength() + 1);

        if (hGlobalMemory)

        {

            // lock block to get a pointer to it

            lpGlobalMemory = GlobalLock(hGlobalMemory);

            // copy String  to the block

            lpGlobalMemory = lstrcpy((char *) lpGlobalMemory, strText.GetBuffer(strText.GetLength()));

            // free memory

            GlobalUnlock(hGlobalMemory);



            // open clipboard for task

            if (OpenClipboard(0))

            {

                EmptyClipboard();

                // try writing if successful

                if (!SetClipboardData(CF_TEXT, hGlobalMemory))

                {

                    // error writing to clipboard

                    bRet = false;

                }

                CloseClipboard();

            }

        }

    }

    return bRet;

}
__________________




 
Benutzerbild von Bluesteel
Bluesteel
Super-Moderator
Letzte Erfolge
Registriert seit: 27.05.2008
Ort: zu hause
Beitr?ge: 2.029
Abgegebene Danke: 510
Erhielt 302 Danke für 52 Beiträge
Downloads: 48
Uploads: 2
Nachrichten: 1565
Hits: 9726
Mit Zitat antworten
Folgende 2 Benutzer sagen Danke zu Bluesteel für den nützlichen Beitrag:
DJJeGgA (15.03.2009), LOL (11.03.2009)