关于在剪贴板上复制数据 [英] About Copying data on Clipboard
问题描述
大家好,
我有一个将数据放入剪贴板的示例.
Hello All,
I have an example on putting data in clipboard.
const char output[] = "TEST";
const size_t len = strlen(output) + 1;
hMem = GlobalAlloc(GMEM_MOVEABLE, len);
memcpy(GlobalLock(hMem), output, len);
GlobalUnlock(hMem);
OpenClipboard(0);
EmptyClipboard();
SetClipboardData(CF_TEXT, hMem);
CloseClipboard();
但是似乎我无法释放hMem.如果我释放hMem,那么我正在丢失数据.现在我很困惑.那我该如何管理剪贴板?
说,我已将大约1M的数据复制到内存中.然后我要关闭我的申请.但是我不想丢失复制的1 MB数据.
如何更好地将数据放在剪贴板上并确保即使我的应用程序不再运行也可以释放内存.
如果我丢失数据,那不是世界末日.我只想知道
谢谢
But it seems that I cannot free hMem. If I free hMem then I am loosing data. Now I am confused. How would I manage clipboard then?
Say, I have copied around 1M data in to memory. Then I am closing my application. But I dont want to loose copied 1 Megabyte data.
How will be a better way to put data on clipboard and make sure the memory is free even though my application is not running anymore.
If I loose data its not the end of the world. I just wanna know
Thank you
推荐答案
调用SetClipboardData()
时,会将内存的所有权传递给剪贴板.不要向后释放它!
如果要在关闭应用程序后释放内存,则可以再次调用EmptyClipboard()
释放内存.但随后,该数据将无法再被其他应用程序粘贴.
通常的做法是将数据放在剪贴板上.通过任何应用程序对SetClipboard()
的下一次调用,将释放内存,然后将新数据放入剪贴板.
When callingSetClipboardData()
, you pass the ownership of the memory to the clipboard. Don''t free it aftwerwards!
If you want to free the memory upon closing your app, you may callEmptyClipboard()
again to release the memory. But then, the data can''t be pasted by other applications anymore.
It''s common practice to let the data on the clipboard. With the next call toSetClipboard()
by any application, the memory is freed before the new data are put to the clipboard.
这篇关于关于在剪贴板上复制数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!