最近一段时间真是事情太多了,前不久项目中一个嵌入office软件的问题,由于没有时间研究,且项目的需求是浏览word文档,偷了一个懒,把word文档转换成pdf文档,然后嵌入libcef浏览器给打开了
现在回过头来,重新研究了这方面的直接办法,在网上查了多方面的资料,终于决定要使用ocx这个方案来完成此项功能,可限于资源问题,ocx方面的资料 没怎么接触过,所以就盲目的在晚上看别人的东西,不过皇天不负有心人,最终还是让我成功的搞定了这个问题,然后觉着,这个ocx也不过如此么,总的来说说 还是比较简单
我当前机器环境:windwos7 64位 dsoframer 1.3 qt5.5.1 vs2013
下边我说下我自己的步骤:
1、下载dsoframer.ocx,然后使用命令注册regsvr32.exe dsoframer.ocx
2、运行regedit.exe 在HKEY_LOCAL_MACHINE目录下搜索dsoframer,获取CLSID的值,这个是用作ocx的唯一标示,如果没有搜索到,可以下载其他的dsoframer,重新注册,或者给我留言
3、接下来直接上代码
QAxWidget * _Word = new QAxWidget(this); _Word->setControl("{00460182-9E5E-11d5-B7C8-B8269041DD57}"); _Word->setProperty("Visible", true); QString filePath = QFileDialog::getOpenFileName(, "Open doc file", QString(), "*.doc"); if (filePath.isEmpty()) return; _Word->dynamicCall("Open(string)", filePath); _Word->dynamicCall("SetCaption(string)", "ocx test"); _Word->dynamicCall("SetMenuAccelerators(bool)", false); _Word->dynamicCall("SetMenubar(bool)", false); QGridLayout * ply = new QGridLayout(this); ply->addWidget(_Word, , , , ); ply->setContentsMargins(, , , ); ui.widget->setLayout(ply);
效果图如下:
dsoframer的相关接口,不过常用的也就那么几个
BeforeDocumentClosed(IDispatch*,bool&) Document,Cancel
BeforeDocumentSaved(IDispatch*,QString,bool&) Document,Location,Cancel
OnActivationChange(bool) fGoingActive
OnDocumentClosed()
OnDocumentOpened(QString,IDispatch*) File,Document
OnFileCommand(dsoFileCommandType,bool&) Item,Cancel
OnPrintPreviewExit()
OnSaveCompleted(IDispatch*,QString,QString) Document,DocName,FullFileLocation
exception(int,QString,QString,QString) code,source,disc,help
propertyChanged(QString) name
signal(QString,int,void*) name,argc,argv
Activate()
Close()
CreateNew(QString) ProgIdOrTemplate
EnableFileCommand(dsoFileCommandType) Item bool
ExecOleCommand(int) OLECMDID
ExecOleCommand(int,QVariant) OLECMDID,Options
ExecOleCommand(int,QVariant,QVariant&) OLECMDID,Options,vInParam
ExecOleCommand(int,QVariant,QVariant&,QVariant&) OLECMDID,Options,vInParam,vInOutParam
GetDataObjectContent(QVariant) ClipFormatNameOrNumber QVariant
Open(QVariant) Document
Open(QVariant,QVariant) Document,ReadOnly
Open(QVariant,QVariant,QVariant) Document,ReadOnly,ProgId
Open(QVariant,QVariant,QVariant,QVariant)Document,ReadOnly,ProgId,WebUsername
Open(QVariant,QVariant,QVariant,QVariant,QVariant) Document,ReadOnly,ProgId,WebUsername,WebPassword
PrintOut()
PrintOut(QVariant) PromptUser
PrintOut(QVariant,QVariant) PromptUser,PrinterName
PrintOut(QVariant,QVariant,QVariant) PromptUser,PrinterName,Copies
PrintOut(QVariant,QVariant,QVariant,QVariant)
PromptUser,PrinterName,Copies,FromPage
PrintOut(QVariant,QVariant,QVariant,QVariant,QVariant) PromptUser,PrinterName,Copies,FromPage,ToPage
PrintOut(QVariant,QVariant,QVariant,QVariant,QVariant,QVariant) PromptUser,PrinterName,Copies,FromPage,ToPage,OutputFile
PrintPreview()
PrintPreviewExit()
Save()
Save(QVariant) SaveAsDocument
Save(QVariant,QVariant) SaveAsDocument,OverwriteExisting
Save(QVariant,QVariant,QVariant)
SaveAsDocument,OverwriteExisting,WebUsername
Save(QVariant,QVariant,QVariant,QVariant) SaveAsDocument,OverwriteExisting,WebUsername,WebPassword
SetActivationPolicy(dsoActivationPolicy) ActivationPolicy
SetBackColor(QColor) BackColor
SetBorderColor(QColor) BorderColor
SetBorderStyle(dsoBorderStyle) BorderStyle
SetCaption(QString) Caption
SetDataObjectContent(QVariant,QVariant) ClipFormatNameOrNumber,DataByteArray
SetEnableFileCommand(dsoFileCommandType,bool) Item,rhs
SetEventsEnabled(bool) EventsEnabled
SetForeColor(QColor) ForeColor
SetFrameHookPolicy(dsoFrameHookPolicy) FrameHookPolicy
SetHostName(QString)HostName
SetLockServer(bool) LockServer
SetMenuAccelerators(bool) MenuAccelerators
SetMenubar(bool) Menubar SetModalState(bool) ModalState
SetTitlebar(bool) Titlebar
SetTitlebarColor(QColor) TitlebarColor
SetTitlebarTextColor(QColor) TitlebarTextColor
SetToolbars(bool) Toolbars
ShowDialog(dsoShowDialogType) DlgType
_PrintOutOld()
_PrintOutOld(QVariant) PromptToSelectPrinter
如果想看哪种方法调用失败或者是异常,请参考C++ gui程序附加dos输出窗口
注:需要完整事例的可以给我留言