2007年1月17日星期三

Paste custom data from clipboard

Follow the below steps can read a new clipboard format data from clipboard to your application:

  1. Use RegisterClipboardFormat function to register a new clipboard format
  2. Use IsClipboardFormatAvailable function to determines whether the clipboard contains data in the specified format or not
  3. Use OpenClipboard to open the clipboard
  4. Use GetClipboardData to retrieves data from the clipboard
  5. Use GlobalSize to retrieves the current size, in bytes, of the data
  6. Use GlobalLock function to get a pointer to the first byte of the object's memory block, and then use memcpy to copy data from this pointer
  7. Use GlobalUnlock function to decrement the lock count associated with a memory object
  8. Use CloseClipboard function to close the clipboard
Below is an example

char* cData = NULL;
// Register a new clipboard format named "cf/new-cf-name"
UINT cfType = RegisterClipboardFormat("cf/new-cf-name");

if (!cfType && IsClipboardFormatAvailable(cfType)) {
    return;
}

// Open Clipboard
if (OpenClipboard() )
{
    // Get data from clipboard
    HANDLE hData = GetClipboardData(cfType);
    if (hData)
    {
        DWORD size = GlobalSize(hData);
        cData = new char[size];
        char* sData = (char*)GlobalLock(hData);

        if (sData) {
            memcpy(cData, sData, size);
        }
        GlobalUnlock(hData);
    }
    CloseClipboard();
}

if (cData) {
    // Handle the data...
}

没有评论: