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
分享到:
相关推荐
剪贴板数据获取是计算机编程中的一个常见任务,特别是在多应用程序交互或用户界面设计中。剪贴板提供了在不同程序间传递信息的便捷途径。在本文中,我们将深入探讨如何在不同的编程语言中获取剪贴板的数据,并了解...
例如,在Windows系统中,可以使用WinAPI函数`OpenClipboard`、`EmptyClipboard`、`GetClipboardData`等来获取和处理剪切板内容。对于跨平台的应用,可能还需要使用如Qt、wxWidgets等库提供的功能。 在开发这类应用...
2. **Windows API调用**:在Windows平台上,剪切板操作通常涉及到对系统API的调用,如`OpenClipboard`、`GetClipboardData`、`EmptyClipboard`等,来获取、设置和清空剪切板数据。 3. **数据类型处理**:剪切板不仅...
3. **放入剪贴板**:将节点数据存入系统剪贴板,这通常通过调用适当的API函数来完成,如在wxWidgets库中使用`wxClipboard`类。 粘贴操作则包括: 1. **检查剪贴板**:确保剪贴板中有可供粘贴的数据。 2. **确定插入...
例如,在Qt中,可以使用`QApplication::clipboard()`获取剪贴板对象,然后调用`setText()`或`setImage()`等方法来设置数据。 4. **Web浏览器环境**: 在Web应用中,由于安全原因,JavaScript不能直接操作剪贴板。...
复制操作将当前选区文本放入剪贴板,粘贴则从剪贴板取出数据,剪切则在复制后删除原文本。 7. 事件处理: - **信号和槽**:Qt或wxWidgets等库提供了信号和槽机制,用于处理用户交互事件。比如,当用户点击“打开”...
4. **粘贴**:粘贴功能将剪贴板中的内容插入到指定位置,这可以是来自同一记事本的复制或剪切操作,也可以是从其他应用程序中复制的数据。 5. **插入当前时间**:这是一个便捷的功能,特别是在编写日志或记录时间...
8. **多选和剪切粘贴**:高级的资源管理器还支持多选功能,允许用户同时选择多个文件或目录,以及剪切、复制和粘贴操作,这需要实现相关的数据结构和算法来跟踪选中状态,并处理剪贴板操作。 9. **权限管理**:在...