最近一段時間真是事情太多了,前不久項目中一個嵌入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、接下來直接上代碼

1 QAxWidget * _Word = new QAxWidget(this); 2 3 _Word->setControl("{00460182-9E5E-11d5-B7C8-B8269041DD57}"); 4 5 _Word->setProperty("Visible", true); 6 7 QString filePath = QFileDialog::getOpenFileName(0, "Open doc file", QString(), "*.doc"); 8 9 if (filePath.isEmpty()) 10 11 return; 12 13 _Word->dynamicCall("Open(string)", filePath); 14 15 _Word->dynamicCall("SetCaption(string)", "ocx test"); 16 17 _Word->dynamicCall("SetMenuAccelerators(bool)", false); 18 19 _Word->dynamicCall("SetMenubar(bool)", false); 20 21 QGridLayout * ply = new QGridLayout(this); 22 23 ply->addWidget(_Word, 0, 0, 1, 1); 24 25 ply->setContentsMargins(0, 0, 0, 0); 26 27 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輸出窗口
注:需要完整事例的可以給我留言