Paste custom data from clipboard
Follow the below steps can read a new clipboard format data from clipboard to your application:
- Use RegisterClipboardFormat function to register a new clipboard format
- Use IsClipboardFormatAvailable function to determines whether the clipboard contains data in the specified format or not
- Use OpenClipboard to open the clipboard
- Use GetClipboardData to retrieves data from the clipboard
- Use GlobalSize to retrieves the current size, in bytes, of the data
- 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
- Use GlobalUnlock function to decrement the lock count associated with a memory object
- Use CloseClipboard function to close the clipboard
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...
}
没有评论:
发表评论