[转]LibreOffice-SDK 开发实战:嵌入MFC-View 和 C# Winform

简介: 转自:http://www.aqcoder.com/blog/detail/id/1579bb39-9bcd-4c0f-9b02-67a851148196/ 前面片文章中我简要介绍了下 LibreOffice SDK 的环境配置,以及 cpp 中一个例子的编译。

转自:http://www.aqcoder.com/blog/detail/id/1579bb39-9bcd-4c0f-9b02-67a851148196/

前面片文章中我简要介绍了下 LibreOffice SDK 的环境配置,以及 cpp 中一个例子的编译。 接下来我们来看一下如何将 LibreOffice 嵌入到 MFC 的 View 中和 C# 的 Winform 中。先上两张效果图: MFC View:这里写图片描述

C# Winform:这里写图片描述

MFC View

这里我主要讲解 LibreOffice 的相关部分,MFC 相关的部分就要带过,特别注意环境设置(ps: 玩MFC 的童鞋都不用多说,不要闲我啰嗦 ^_^) 1.建立一个当文档程序

2.设置头文件包含目录

C:\LibreOffice4\sdk\include
C:\LibreOffice4\sdk\includecpp

注意第二个目录是编译 cpp 例子得到的,详见http://blog.csdn.net/my___dream/article/details/45176921

3.设置库目录 C:\LibreOffice4\sdk\lib

4.添加环境变量 PATH: C:\LibreOffice4\URE\bin

5.show you the code: 包含头文件以及命名空间

#include "sal/config.h"
#include <sal/main.h> #include <rtl/ustring.hxx> #include <osl/diagnose.h> #include <cppuhelper/bootstrap.hxx> #include <com/sun/star/bridge/XUnoUrlResolver.hpp> #include <com/sun/star/registry/XSimpleRegistry.hpp> #include <com/sun/star/lang/XMultiComponentFactory.hpp> #include <com/sun/star/frame/XDesktop.hpp> #include <com/sun/star/uno/Reference.h> #include <com/sun/star/lang/XComponent.hpp> #include <com/sun/star/awt/XSystemChildFactory.hpp> #include <com/sun/star/util/URL.hpp> #include <com/sun/star/beans/XPropertySet.hpp> #include <com/sun/star/container/XNameAccess.hpp> #include <com/sun/star/container/XEnumerationAccess.hpp> #include <com/sun/star/frame/XComponentLoader.hpp> #include <com/sun/star/frame/XTitle.hpp> #include <com/sun/star/util/URL.hpp> #include <com/sun/star/util/XCloseable.hpp> #include <com/sun/star/util/XCloseListener.hpp> #include <com/sun/star/util/CloseVetoException.hpp> #include <com/sun/star/uno/RuntimeException.hpp> #include <osl/file.hxx> #include <osl/process.h> #include <rtl/ustrbuf.hxx> #include <com/sun/star/awt/XSystemChildFactory.hpp> #include <com/sun/star/awt/XDialog2.hpp> #include <com/sun/star/awt/XControlModel.hpp> #include <com/sun/star/lang/SystemDependent.hpp> #include <com/sun/star/lang/XMultiServiceFactory.hpp> #include <com/sun/star/awt/XSystemDependentWindowPeer.hpp> #include <com/sun/star/util/XURLTransformer.hpp> #include <list> using namespace cppu; using namespace com::sun::star::uno; using namespace com::sun::star::lang; using namespace com::sun::star::beans; using namespace com::sun::star::frame; using ::rtl::OUString; using ::rtl::OUStringToOString; using namespace com::sun::star; using namespace container; using namespace osl; using namespace rtl; using namespace util; using namespace awt;

在 View 中重写 OnInitialUpdate 函数,键入如下代码:

void CSDITestView::OnInitialUpdate() { CView::OnInitialUpdate(); CString strFileName = GetDocument()->GetPathName(); if (strFileName.IsEmpty()) return; using com::sun::star::lang::SystemDependent::SYSTEM_WIN32; // init XComponentContext & XMultiComponentFactory & XMultiServiceFactory Reference< XComponentContext > xContext(::cppu::bootstrap()); Reference< XMultiComponentFactory > xMultiComponetFactory = xContext->getServiceManager(); Reference<XMultiServiceFactory> xMultiServiceFactory(xMultiComponetFactory, UNO_QUERY); // create XComponentLoader Reference< XComponentLoader > xComponentLoader = Reference<XComponentLoader>( xMultiComponetFactory->createInstanceWithContext(OUString("com.sun.star.frame.Desktop"), xContext), UNO_QUERY); // create a XWindow & XFrame Adn the XWindow contain the XFrame Reference<XSystemChildFactory> xSystemChildFactory = Reference<XSystemChildFactory>( xMultiComponetFactory->createInstanceWithContext(OUString("com.sun.star.awt.Toolkit"), xContext), UNO_QUERY); Reference<XWindowPeer> xPeer = Reference<XWindowPeer>(xSystemChildFactory->createSystemChild( Any(reinterpret_cast<long>(m_hWnd)), Sequence<sal_Int8>(4), SYSTEM_WIN32), UNO_QUERY); if (xPeer.is()) { Reference<XWindow> xFrameContainerWindow = Reference<XWindow>(xPeer, UNO_QUERY); Reference<XFrame> xFrame = Reference<XFrame>(xMultiComponetFactory ->createInstanceWithContext(OUString("com.sun.star.frame.Frame"), xContext), UNO_QUERY); if (xFrameContainerWindow.is() && xFrame.is()) { xFrame->initialize(xFrameContainerWindow); Reference<XFrames> xChildContainer = Reference<XFramesSupplier>(xComponentLoader,UNO_QUERY)->getFrames(); xChildContainer->append(xFrame); xFrame->setName(OUString("myframe")); // to identify frame } Reference<XSystemDependentWindowPeer> xPeer(xFrameContainerWindow, UNO_QUERY); if(xPeer.is()) { xFrameContainerWindow->setVisible(true); long lh = 0; xPeer->getWindowHandle(Sequence<sal_Int8>(4), SYSTEM_WIN32) >>= lh; m_hPlatformContainerWindow = reinterpret_cast<HWND>(lh); } } // load component Sequence<PropertyValue> properties; properties.realloc(2); properties[0] = PropertyValue(OUString("ReadOnly"), 0, Any(false), PropertyState_DIRECT_VALUE); properties[1].Name = OUString(OUString("passwd")); properties[1].Value = Any(OUString("abc")); URL url; OUString ousUrl, ousWorkingDir, ousPathUrl; osl_getProcessWorkingDir(&ousWorkingDir.pData); osl::FileBase::getFileURLFromSystemPath(strFileName.GetBuffer(), ousPathUrl); osl::FileBase::getAbsoluteFileURL(ousWorkingDir, ousPathUrl, ousUrl); url.Complete = OUString(ousUrl); Reference<XURLTransformer> urltf = Reference<XURLTransformer>(xMultiComponetFactory ->createInstanceWithContext(OUString("com.sun.star.util.URLTransformer"), xContext), UNO_QUERY); urltf->parseStrict(url); Reference<XComponent> xComponent = xComponentLoader->loadComponentFromURL(url.Complete, OUString("myframe"),4, properties);// move windowCWnd* pWnd =CWnd::FromHandle(m_hPlatformContainerWindow);if(pWnd){CRect rect;CWnd::FromHandle(m_hWnd)->GetClientRect(&rect); pWnd->MoveWindow(rect, TRUE);}// save to close m_xComponet = xComponent; m_xDesktop =Reference<XDesktop>(xComponentLoader, UNO_QUERY);}

代码解释(按注释行解释): 1. 首先是判断有没打开文档,没有就返回,这个不需要解释了,一定要判断哦。。。

  1. // init XComponentContext & XMultiComponentFactory & XMultiServiceFactory 初始化组件上下文、组件工厂(顺带提一下,LibreOffice 需要 UNO 编程的知识, 没接触过 UNO 的童鞋看看这个 中文:https://wiki.openoffice.org/wiki/Zh/Documentation/DevGuide/OpenOffice.org_Developers_Guide) 英文:https://wiki.openoffice.org/wiki/Documentation/DevGuide/OpenOffice.org_Developers_Guide

  2. // create XComponentLoader 创建 com.sun.star.frame.Desktop 对象得到 XComponentLoader 接口

  3. // create a XWindow & XFrame Adn the XWindow contain the XFrame 这里英文注释写的不是很准确,鄙人英文不是很好就这么写了 T_T。这部分是为了得到 XWindows 接口用于承载 XFrame 后面加载的组件都在 XFrame 容器当中。 首先,创建一个 com.sun.star.awt.Toolkit 对象得到 XSystemChildFactory,调用 XSystemChildFactory.createSystemChild() 方法得到 XWindowPeer 接口,注意这个方法传入了父窗口句柄,也就是 View 的句柄。 接着,创建一个 com.sun.star.frame.Frame 对象得到 XFrame 接口,并进行初始化工作,然后得到 XFrames 接口并调用 XFrames.getFrames() 方法加入到 Frame 集合当中。 最后,设置 com.sun.star.frame.Frame 对象的名称,这个很关键,以为后面要根据名称加载组件。 最最后,得到 XSystemDependentWindowPeer 接口,调用 XSystemDependentWindowPeer.getWindowHandle() 方法得到容器的窗口句柄。

  4. // load component 加载组件,这里特别要注意 URL 对象,因为从 GetPathName() 方法得到文件路径是 Window 的路径格式(也就是反斜杠),我们要把他转化为 Unix 系统下的路径格式(也就是正斜杠,到底谁反人类了 T_T) 然后调用 XComponentLoader 接口的 XComponentLoader.loadComponentFromURL() 方法加载组件。注意第二个参数一定要和步骤 4 中设置的 com.sun.star.frame.Frame 对象的名字相同,你得告诉人家加载到那个容器里嘛~~

  5. // move window 这段代码也没什么好解释的了,就是窗口的定位。

  6. 最后几个是成员变量是为了关闭文档,注意:如果不关闭文档后面再打开程序,打开同一个文档是会挂掉,因为 UNO 对象是跨进访问的,你的程序关了 soffice.exe 和 soffice.bin 两个进程都还在,而你打开的文档信息在进程中,所以挂掉。。。 这里只是做个示例和试验,所以代码没有考虑结构和严谨性,容易出 BUG ,请不要吐槽 ->_->

所以,在析构函数中加入如下代码:

CSDITestView::~CSDITestView() { Reference< XCloseable > xClaseable = Reference< XCloseable >(m_xComponet, UNO_QUERY); xClaseable->close(false); m_xDesktop.clear(); }

以及成员变量

HWND m_hPlatformContainerWindow;
com::sun::star::uno::Reference< com::sun::star::frame::XDesktop > m_xDesktop; com::sun::star::uno::Reference< com::sun::star::lang::XComponent > m_xCompone

OK, MFC 的示例就酱完了,刚开始看 UNO 的代码可能有点累,都是模板和接口,建议先收悉一下 UNO 编程的相关知识,上面给的中文连接还有没翻译完,建议看英文的。。。英文不好的程序员不是好程序员 ->_->

C# Winform

Winform 的代码步骤是一样的: 初始化 --> 创建容器 --> 加载组件 --> 移动窗口。 1. 创建工程 2. 添加引用: \LibreOffice4\sdk\cli\ 目录下有 cli_basetypes.dll、cli_cppuhelper.dll、cli_oootypes.dll、cli_ure.dll、cli_uretypes.dll 5 个程序集,都引用上 3. 上代码:

[DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)] public static extern int MoveWindow(IntPtr hWnd, int x, int y, int nWidth, int nHeight, bool bRePaint); public Form1() { InitializeComponent(); m_xContext = uno.util.Bootstrap.bootstrap(); mxMSFactory = (XMultiServiceFactory)m_xContext.getServiceManager(); XComponentLoader aLoader = (XComponentLoader) mxMSFactory.createInstance("com.sun.star.frame.Desktop"); XSystemChildFactory xSystemChildFactory = (XSystemChildFactory)mxMSFactory.createInstance("com.sun.star.awt.Toolkit"); XWindowPeer xWindowPeer = xSystemChildFactory.createSystemChild(new Any(this.Handle.ToInt32()), new byte[4], 1); XWindow xWindow = (XWindow)xWindowPeer; XFrame xFrame = (XFrame)mxMSFactory.createInstance("com.sun.star.frame.Frame"); xFrame.initialize(xWindow); XFramesSupplier xFrameSupplier = (XFramesSupplier)aLoader; xFrameSupplier.getFrames().append(xFrame); xFrame.setName("myframe"); XSystemDependentWindowPeer xSDWindowPeer = (XSystemDependentWindowPeer)xWindow; xWindow.setVisible(true); object hHandle = xSDWindowPeer.getWindowHandle(new byte[4], 1).Value; XComponent xComponent = aLoader.loadComponentFromURL("file:///C:/test.odt", "myframe", 4, new unoidl.com.sun.star.beans.PropertyValue[0]); MoveWindow(new IntPtr((int)hHandle), 0, 0, this.Width, this.Height, true); }

这个实例在主窗口初始化的时候就加载了,所以加载的文档是写死的,注意是正斜杠哦。。。 代码基本和 C++ 的一样就不解释了,提一下就是 MoveWindows() 用到了 Win32API 不知道 C# 如何调用 Win32API 的童鞋自己 Google 吧 ^_^ 好了,我也刚开始学 LO,这种方法还没在实际项目中验证,如有错漏之处还请大神请教,共勉。。。

相关文章
|
19天前
|
数据采集 存储 C#
C# 爬虫技术:京东视频内容抓取的实战案例分析
C# 爬虫技术:京东视频内容抓取的实战案例分析
|
10天前
|
设计模式 C# 开发者
C#设计模式入门实战教程
C#设计模式入门实战教程
|
19天前
|
数据采集 开发工具 Python
海康威视工业相机SDK+Python+PyQt开发数据采集系统(支持软件触发、编码器触发)
该系统基于海康威视工业相机SDK,使用Python与PyQt开发,支持Gige与USB相机设备的搜索及双相机同时显示。系统提供软件触发与编码器触发模式,并可在数据采集过程中实时保存图像。此外,用户可以调节曝光时间和增益,并进行信息输入,这些信息将被保存至配置文件以便下次自动加载。参数调节与实时预览等功能进一步增强了系统的实用性。
42 1
|
20天前
|
JSON C# 开发者
💡探索C#语言进化论:揭秘.NET开发效率飙升的秘密武器💼
【8月更文挑战第28天】C#语言凭借其强大的功能与易用性深受开发者喜爱。伴随.NET平台演进,C#持续引入新特性,如C# 7.0的模式匹配,让处理复杂数据结构更直观简洁;C# 8.0的异步流则使异步编程更灵活高效,无需一次性加载全部数据至内存。通过示例展示了模式匹配简化JSON解析及异步流实现文件逐行读取的应用。此外,C# 8.0还提供了默认接口成员和可空引用类型等特性,进一步提高.NET开发效率与代码可维护性。随着C#的发展,未来的.NET开发将更加高效便捷。
35 1
|
1月前
|
关系型数据库 Java MySQL
C#winform中使用SQLite数据库
C#winform中使用SQLite数据库
39 3
C#winform中使用SQLite数据库
|
2月前
|
算法 C#
C#实战 | 求解《九章算术》盈不足之共买物
【7月更文挑战第8天】中国古代数学成就显著,《九章算术》展示了先进的算法,如分数运算和方程解法,领先世界数百年。项目示例通过控制台应用,运用for循环和if条件语句,模拟解决书中盈不足问题,展示了解决数学问题的编程方法。
31 6
C#实战 | 求解《九章算术》盈不足之共买物
|
17天前
|
Android开发 iOS开发 C#
Xamarin:用C#打造跨平台移动应用的终极利器——从零开始构建你的第一个iOS与Android通用App,体验前所未有的高效与便捷开发之旅
【8月更文挑战第31天】Xamarin 是一个强大的框架,允许开发者使用单一的 C# 代码库构建高性能的原生移动应用,支持 iOS、Android 和 Windows 平台。作为微软的一部分,Xamarin 充分利用了 .NET 框架的强大功能,提供了丰富的 API 和工具集,简化了跨平台移动应用开发。本文通过一个简单的示例应用介绍了如何使用 Xamarin.Forms 快速创建跨平台应用,包括设置开发环境、定义用户界面和实现按钮点击事件处理逻辑。这个示例展示了 Xamarin.Forms 的基本功能,帮助开发者提高开发效率并实现一致的用户体验。
36 0
|
17天前
|
数据安全/隐私保护 C# UED
利用 Xamarin 开展企业级移动应用开发:从用户登录到客户管理,全面演示C#与Xamarin.Forms构建跨平台CRM应用的实战技巧与代码示例
【8月更文挑战第31天】利用 Xamarin 进行企业级移动应用开发能显著提升效率并确保高质量和高性能。Xamarin 的跨平台特性使得开发者可以通过单一的 C# 代码库构建 iOS、Android 和 Windows 应用,帮助企业快速推出产品并保持一致的用户体验。本文通过一个简单的 CRM 示例应用演示 Xamarin 的使用方法,并提供了具体的代码示例。该应用包括用户登录、客户列表显示和添加新客户等功能。此外,还介绍了如何增强应用的安全性、数据持久化、性能优化及可扩展性,从而构建出功能全面且体验良好的移动应用。
24 0
|
17天前
|
开发者 iOS开发 C#
Uno Platform 入门超详细指南:从零开始教你打造兼容 Web、Windows、iOS 和 Android 的跨平台应用,轻松掌握 XAML 与 C# 开发技巧,快速上手示例代码助你迈出第一步
【8月更文挑战第31天】Uno Platform 是一个基于 Microsoft .NET 的开源框架,支持使用 C# 和 XAML 构建跨平台应用,适用于 Web(WebAssembly)、Windows、Linux、macOS、iOS 和 Android。它允许开发者共享几乎全部的业务逻辑和 UI 代码,同时保持原生性能。选择 Uno Platform 可以统一开发体验,减少代码重复,降低开发成本。安装时需先配置好 Visual Studio 或 Visual Studio for Mac,并通过 NuGet 或官网下载工具包。
24 0
|
17天前
|
前端开发 开发者 Apache
揭秘Apache Wicket项目结构:如何打造Web应用的钢铁长城,告别混乱代码!
【8月更文挑战第31天】Apache Wicket凭借其组件化设计深受Java Web开发者青睐。本文详细解析了Wicket项目结构,帮助你构建可维护的大型Web应用。通过示例展示了如何使用Maven管理依赖,并组织页面、组件及业务逻辑,确保代码清晰易懂。Wicket提供的页面继承、组件重用等功能进一步增强了项目的可维护性和扩展性。掌握这些技巧,能够显著提升开发效率,构建更稳定的Web应用。
40 0

热门文章

最新文章