wxWidgets从剪切板获取数据

wxWidgets的剪切板实现windows下优先使用OLE接口,但是在使用OLE接口的时候对剪切板里面的数据处理中对部分格式采取了默认处理,有可能造成数据转换的错误,这个时候我们自己要写代码重新从剪切板里面读取数据,采用正确的格式转换数据处理!
下面是一个示例函数:
bool GetOleClipboardData(wxDataObject& data)
{
#if wxUSE_OLE && !defined(__WXWINCE__)
IDataObject *pDataObject = NULL;
HRESULT hr = OleGetClipboard(&pDataObject);
if ( FAILED(hr) || !pDataObject )
{
wxLogSysError(hr, _("Failed to get data from the clipboard"));
return false;
}
// build the list of supported formats
size_t nFormats = data.GetFormatCount(wxDataObject::Set);
wxDataFormat format;
wxDataFormat *formats;
if ( nFormats == 1 )
{
// the most common case
formats = &format;
}
else
{
// bad luck, need to alloc mem
formats = new wxDataFormat[nFormats];
}
data.GetAllFormats(formats, wxDataObject::Set);
// get the data for the given formats
FORMATETC formatEtc;
CLIPFORMAT cf;
bool result = false;
// enumerate all explicit formats on the clipboard.
// note that this does not include implicit / synthetic (automatically
// converted) formats.
#ifdef __WXDEBUG__
// get the format enumerator
IEnumFORMATETC *pEnumFormatEtc = NULL;
hr = pDataObject->EnumFormatEtc(DATADIR_GET, &pEnumFormatEtc);
if ( FAILED(hr) || !pEnumFormatEtc )
{
wxLogSysError(hr,
_("Failed to retrieve the supported clipboard formats"));
}
else
{
// ask for the supported formats and see if there are any we support
for ( ;; )
{
ULONG nCount;
hr = pEnumFormatEtc->Next(1, &formatEtc, &nCount);
// don't use FAILED() because S_FALSE would pass it
if ( hr != S_OK )
{
// no more formats
break;
}
cf = formatEtc.cfFormat;
wxLogTrace(wxTRACE_OleCalls,
wxT("Object on the clipboard supports format %s."),
wxDataObject::GetFormatName(cf));
}
pEnumFormatEtc->Release();
}
#endif // Debug
STGMEDIUM medium;
// stop at the first valid format found on the clipboard
for ( size_t n = 0; !result && (n < nFormats); n++ )
{
// convert to NativeFormat Id
cf = formats[n].GetFormatId();
// if the format is not available, try the next one
// this test includes implicit / sythetic formats
if ( !::IsClipboardFormatAvailable(cf) )
continue;
formatEtc.cfFormat = cf;
formatEtc.ptd = NULL;
formatEtc.dwAspect = DVASPECT_CONTENT;
formatEtc.lindex = -1;
// use the appropriate tymed
switch ( formatEtc.cfFormat )
{
case CF_DIB://与位图一样处理
case CF_BITMAP:
formatEtc.tymed = TYMED_GDI;
break;
#ifndef __WXWINCE__
case CF_METAFILEPICT:
formatEtc.tymed = TYMED_MFPICT;
break;
case CF_ENHMETAFILE:
formatEtc.tymed = TYMED_ENHMF;
break;
#endif
default:
formatEtc.tymed = TYMED_HGLOBAL;
}
// try to get data
hr = pDataObject->GetData(&formatEtc, &medium);
if ( FAILED(hr) )
{
// try other tymed for GDI objects
if ( formatEtc.cfFormat == CF_BITMAP )
{
formatEtc.tymed = TYMED_HGLOBAL;
hr = pDataObject->GetData(&formatEtc, &medium);
}
}
if ( SUCCEEDED(hr) )
{
// pass the data to the data object
hr = data.GetInterface()->SetData(&formatEtc, &medium, true);
if ( FAILED(hr) )
{
wxLogDebug(wxT("Failed to set data in wxIDataObject"));
// IDataObject only takes the ownership of data if it
// successfully got it - which is not the case here
ReleaseStgMedium(&medium);
}
else
{
result = true;
}
}
//else: unsupported tymed?
}
if ( formats != &format )
{
delete [] formats;
}
//else: we didn't allocate any memory
// clean up and return
pDataObject->Release();
return result;
#else
return false;
#endif
}

转载于:https://my.oschina.net/u/2332347/blog/637819

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值