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
分享到:
相关推荐
第·1·章简要介绍了·wxWidgets·的发展历史和框架结构,第2章介绍了wxWidgets的程序框架及其实现,第3章深入剖析了wxWidgets的事件处理机制,第4~11章详细讲述了wxWidgets的图形用户界面、数据结构及wxWidgets在...
这是一部学习wxWidgets的基础文档,能够让你快速的学习
wxWidgets是一个跨平台的程序员的开发工具包,用来开发...本编程手册从浅入深的介绍wxWidgets,详细介绍了窗口、事件、控件、图像、剪贴析、数据结构、文件和流、内存管理、国际化程序、多线程、网络还有高级应用话题。
wxwidgets开发入门 API查询 深入学习wxwidgets
wxWidgets(/wɪksˈwɪdʒɪts/,原名wxWindows[1])是一個開放原始碼且跨平台的物件工具集(widget toolkit),其函式庫可用來建立基本的圖形使用者介面(GUI)。wxWidgets由Julian Smart於1992年首先開發。 ...
wxWidgets跨平台界面开发,一次开发,全平编译运行。
程序可能会有bug,如果不能直接执行,请先找找是不是自己原因,或者给我发消息(不...另外这个是用wxwidgets写得,不知道能不能跨平台使用,有兴趣的可以下在windows平台编译一下,只是头文件什么的肯定是要更换的。
wxWidgets 下的皮肤组件,非常好用
WIN版本的wxWidgets-3.1.3.zip wxWidgets源码 来自官网 下载链接 https://github.com/wxWidgets/wxWidgets/releases/download/v3.1.3/wxWidgets-3.1.3.zip 不想花积分的可以直接下载。
wxWidgets和MFC混合编程 讲述在一个程序中wxWidgets和MFC的窗口如何一起工作。
wxWidgets官方帮助文档wxWidgets-2.8.7-CHM
wxWidgets自绘标题栏,项目源码分享,请使用vs2015编辑器
wxWidgets Demo source codes
wxWidgets安装+教程wxWidgets安装+教程wxWidgets安装+教程wxWidgets安装+教程wxWidgets安装+教程wxWidgets安装+教程wxWidgets安装+教程wxWidgets安装+教程wxWidgets安装+教程
wxWidgets的类型转换 char int wxString
CodeBlocks编译wxWidgets设置.pdf
(1)使用GNU 编译器,在命令行下使用wxWidgets类库,学习wxWidgets简单数据结构(wxString, wxFile, wxDataTime)。 (2)尝试使用codebloks 中wxwidgets程序模板,编写最基本wxwidgets程序,理解程序架构并掌握程序运行...
wxWidgetsGUI《Cross-Platform GUI Programming with wxWidgets》的清晰中文版
wxWidgets最新版本的帮助文档,如果使用wxWidgets写程序不容错过
包含了《wxWidgets跨平台GUI编程》的中、英文两个版本。另外还有wxWidgets的类库参考指南。