本人在学activex,我想在一个窗口显示word文档,编写了一个程序,但运行不正常
我的程序如下,但窗口内没显示word文档(我想在窗口内显示word文档)。请问该改哪里?
//displaywordinwindow.cpp
#define UNICODE
#include <windows.h>
#include "interfacehead.h"
HWND hwndContain;//容器窗口句柄
WCHAR szFileName[MAX_PATH];//文件名称,c:\path\A.doc
IStorage * lpIStorage=NULL;
HRESULT hr;
CLSID clsid;
IOleObject* pIOleObject= NULL;
IOleInPlaceActiveObject * pIOleInPlaceActiveObject=NULL;
IPersistStorage* pIPersistStorage=NULL;
RECT rcView;
CMyCom * MyCom;
IOleClientSite * pIOleClientSite;
HWND DocWin;
//回调函数的申明
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
//入口主函数
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow)
{
MyCom=new CMyCom;
WCHAR szAppName[]=L"显示Word内容";
WCHAR szClassName[]=L"DisplayWordClass";
WNDCLASSEX wndclassex;
MSG msg;
//取得当前路径
WCHAR path[]=L"\\a.doc";
GetCurrentDirectory(MAX_PATH,szFileName);
wcscat(szFileName,path);
//开始给窗口类赋值
wndclassex.cbSize=sizeof(WNDCLASSEX);
wndclassex.style=CS_VREDRAW | CS_HREDRAW | CS_DBLCLKS;
wndclassex.lpfnWndProc=WndProc;
wndclassex.cbClsExtra=0;
wndclassex.cbWndExtra=0;
wndclassex.hInstance=hInstance;
wndclassex.hIcon=NULL;
wndclassex.hCursor=LoadCursor (NULL, IDC_ARROW) ;
wndclassex.hbrBackground=(HBRUSH)COLOR_APPWORKSPACE;
wndclassex.lpszMenuName=NULL;
wndclassex.hIconSm=NULL;
wndclassex.lpszClassName=szClassName;
//注册窗口
if (!RegisterClassEx (&wndclassex))
{
MessageBox (NULL, L"程序要求windows 2000或xp兼容的版本!",szAppName, MB_ICONERROR) ;
return 0 ;
}
//建立窗口
hwndContain=CreateWindowEx(0,
szClassName,szAppName,
WS_SYSMENU|WS_OVERLAPPED | WS_VISIBLE,
0,
0,
800,
600,
NULL,
NULL,
hInstance,
NULL);
ShowWindow (hwndContain, iCmdShow);
UpdateWindow (hwndContain) ;
//循环程序
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
//主窗口的回调函数
LRESULT CALLBACK WndProc (HWND hwndmain, UINT message, WPARAM wParam, LPARAM lParam){
switch (message){
case WM_CREATE:
CoInitialize(NULL);
hr=StgIsStorageFile(szFileName);
if(FAILED(hr)){
MessageBox(hwndmain,L"不是复合文件",L"ok",0);
CoUninitialize();
return 0;
}
hr=StgOpenStorage(szFileName,NULL,STGM_READ|STGM_SHARE_DENY_WRITE,NULL,0,&lpIStorage);
if(FAILED(hr)){
MessageBox(hwndmain,L"建立存储对象失败",L"ok",0);
CoUninitialize();
return 0;
}
hr=ReadClassStg(lpIStorage, &clsid);
if(FAILED(hr)||(clsid == GUID_NULL))
{
MessageBox(hwndmain,L"clsid为空",L"ok",0);
lpIStorage->Release();
CoUninitialize();
return 0;
}
hr=CoCreateInstance(clsid, NULL, CLSCTX_INPROC, IID_IOleObject, (void**)&pIOleObject);
if(FAILED(hr))
{
MessageBox(hwndmain,L"建立实例失败",L"ok",0);
lpIStorage->Release();
CoUninitialize();
return 0;
}
hr = pIOleObject->QueryInterface(IID_IPersistStorage, (void**)&pIPersistStorage);
if(FAILED(hr))
{
MessageBox(hwndmain,L"取得IPersistStorage接口失败",L"ok",0);
lpIStorage->Release();
CoUninitialize();
return 0;
}
hr=pIPersistStorage->Load(lpIStorage);
pIPersistStorage->Release();
return 0;
case WM_SHOWWINDOW:
GetClientRect(hwndmain, &rcView);
hr=MyCom->QueryInterface(IID_IOleClientSite,(void**)&pIOleClientSite);
pIOleObject->SetClientSite(pIOleClientSite);
hr = pIOleObject->DoVerb(OLEIVERB_SHOW, NULL, pIOleClientSite, 0, hwndmain, &rcView);
if(FAILED(hr))
{
MessageBox(hwndmain,L"DoVerb失败",L"ok",0);//每次执行都会有该提示
return 0;
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0 ;
}
return DefWindowProc (hwndmain, message, wParam, lParam);
}