接口:::rtl::OUString SAL_CALL SwXTextDocument::InsertOLERetNameSize(const ::rtl::OUString& aPathName, const float fOleWidth, const float fOleHeight)throw (::com::sun::star::uno::RuntimeException)
1.当aPathName的值为NULL时,插入的是空白的OLE,插入方法为:
(1)首先获取插入对象的ID, 然后直接调用CreateEmbeddedObject();
svt::EmbeddedObjectRef xObj; uno::Reference < embed::XStorage > xStor = comphelper::OStorageHelper::GetTemporaryStorage(); comphelper::EmbeddedObjectContainer aCnt(xStor); ::rtl::OUString aName = aCnt.CreateUniqueObjectName(); bool bInsert = false; uno::Reference < embed::XEmbeddedObject > m_xObj; //获取插入对象的ID String aServerName = String( ::rtl::OStringToOUString( ::rtl::OString("病历通 3.0 绘图"),RTL_TEXTENCODING_GBK ) ); SvObjectServerList aServerList; aServerList.FillInsertObjects(); aServerList.Remove( SwDocShell::Factory().GetClassId() ); const SvObjectServer* pS = aServerList.Get( aServerName ); //生成一个对象 m_xObj = aCnt.CreateEmbeddedObject( pS->GetClassName().GetByteSequence(), aName ); if ( m_xObj.is() ) { xObj.Assign( m_xObj, embed::Aspects::MSOLE_CONTENT ); bInsert = rSh.InsertOleObject( xObj ); }
(2) 获取插入对象的ID,然后通过embed::XEmbedObjectCreator对象来生成embed::XEmbeddedObject;
svt::EmbeddedObjectRef xObj; uno::Reference < embed::XStorage > xStor = comphelper::OStorageHelper::GetTemporaryStorage(); comphelper::EmbeddedObjectContainer aCnt(xStor); ::rtl::OUString aName = aCnt.CreateUniqueObjectName(); bool bInsert = false; //获取插入对象的ID String aServerName = String( ::rtl::OStringToOUString( ::rtl::OString("病历通 3.0 绘图"),RTL_TEXTENCODING_GBK ) ); SvObjectServerList aServerList; aServerList.FillInsertObjects(); aServerList.Remove( SwDocShell::Factory().GetClassId() ); const SvObjectServer* pS = aServerList.Get( aServerName ); uno::Sequence < sal_Int8 > aClass( pS->GetClassName().GetByteSequence() ); //生成对象 uno::Reference < embed::XEmbedObjectCreator > xFactory( ::comphelper::getProcessServiceFactory()->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.embed.EmbeddedObjectCreator")) ), uno::UNO_QUERY ); uno::Reference < embed::XEmbeddedObject > m_xObj = uno::Reference < embed::XEmbeddedObject >( xFactory->createInstanceInitNew( aClass, ::rtl::OUString(), xStor, aName, uno::Sequence < beans::PropertyValue >() ), uno::UNO_QUERY ); if ( m_xObj.is() ) { xObj.Assign( m_xObj, embed::Aspects::MSOLE_CONTENT ); bInsert = rSh.InsertOleObject( xObj ); }
总结:其实,通过断点调试可以看出,第一种方法还是通过第二种方法实现的,只是把第二种方法封装了一下。
2. 调整插入OLE的大小
(1) 调用sw\source\filter\xml\xmltexti.cxx中的函数: void lcl_setObjectVisualArea( const uno::Reference< embed::XEmbeddedObject >& xObj,
sal_Int64 nAspect, const Size& aVisSize, const MapUnit& aUnit )
(2)m_xObj->setVisualAreaSize( ...)
(3)xObj->setVisualAreaSize( ...)
总结:说到底,以上三个调用函数最终调用的还是OCommonEmbeddedObject类 中的函数
virtual void SAL_CALL setVisualAreaSize( sal_Int64 nAspect, const ::com::sun::star::awt::Size& aSize )
throw ( ::com::sun::star::lang::IllegalArgumentException,
::com::sun::star::embed::WrongStateException,
::com::sun::star::uno::Exception,
::com::sun::star::uno::RuntimeException );