VC 剪贴板操作

 

 VC 剪贴板操作

 


    程序里用到了操作剪贴板的函数,而一直有个问题未能解决。有时会出现莫名其妙的异常。比如我把文本拷贝到剪贴板,之后我的编辑框竟然不能输入文本,即使可以输入,也是一推乱码。今天终于找到根源了。就是多了一句代码GlobalFree 害得我调试惨了。唉!感叹!忏悔一身的业障,才招来今日的遇事不顺!阿弥陀佛!

 

MSDN里面说得很清楚了:

After SetClipboardData is called, the system owns the object identified by the hMem parameter. The application can read the data, but must not free the handle or leave it locked. If the hMem parameter identifies a memory object, the object must have been allocated using the GlobalAlloc function with the GMEM_MOVEABLE and GMEM_DDESHARE flags.

 

我自作聪明调用了GlobalFree所以出现了意想不到的bug。


 

 一、设置剪贴板文本

/**************************************************************************** 设置剪贴板文本 ****************************************************************************/ bool SetClipBoardText(const TCHAR* text,HWND hWnd) { ASSERT(hWnd); //打开剪贴板 if ( !::OpenClipboard(hWnd) ) return false; //empties the clipboard and frees handles to data in the clipboard if ( !EmptyClipboard() ) { CloseClipboard(); return false; } //get text length int len=_tcslen(text); //After SetClipboardData is called, the system owns the object identified by the hMem parameter. //The application can read the data, but must not free the handle or leave it locked. If the //hMem parameter identifies a memory object, the object must have been allocated using the //GlobalAlloc function with the GMEM_MOVEABLE and GMEM_DDESHARE flags. HANDLE hClip=GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE,(len+1)*sizeof(TCHAR)); if (hClip==NULL) { CloseClipboard(); return false; } //locks a global memory object and returns a pointer to the first byte of the object's memory block TCHAR* pBuf=(TCHAR*)GlobalLock(hClip); if (pBuf==NULL) { GlobalFree(hClip); CloseClipboard(); return fals

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值