如何在IE内部嵌入word,不是链接打开的那种(高分150求解,up有分)

bluefox168 2004-09-05 10:57:38
如题,就是如何利用c#编写一个OCX控件,把word嵌入到IE中。

http://www.goldgrid.com/Products/Demo/iWebOfficeSign/Ocxasp/DocumentList.asp
里演示的那样嵌入到IE里面,我知道对word的控制怎么实现,但怎么嵌入呢?

欢迎大家讨论,积极建议者都有分
...全文
922 61 打赏 收藏 转发到动态 举报
写回复
用AI写文章
61 条回复
切换为时间正序
请发表友善的回复…
发表回复
googoo5100 2004-10-26
  • 打赏
  • 举报
回复
ding
xdwlb 2004-10-19
  • 打赏
  • 举报
回复
关注
DataBind 2004-09-10
  • 打赏
  • 举报
回复
STDMETHODIMP_(BOOL) CFramerDocument::SaveDocObject(BOOL fPrompt)
{
TRACE2("CFramerDocument[%d]::SaveDocObject(fPrompt=%d)\n", _dwDocId, fPrompt);
if ((!_poleobj) || (_fIsHTMLFile)) return FALSE;

BSTR bstrFileName = NULL;
LPOLESTR pwszDisplayName = NULL;

HRESULT hr = E_FAIL;

// If object was opened from a file, get the full path and file name...
if (_pmkfile)
{
hr = _pmkfile->GetDisplayName(_pbctx, NULL, &pwszDisplayName);
if (FAILED(hr)) return FALSE;
}

// If the user pressed SaveAs, or if there is no associated file with
// the embedded object, then prompt for a file name & location...
if ((fPrompt) || (!_pmkfile))
{

OPENFILENAME ofn;
CHAR szFileName[MAX_PATH];

memset(&ofn, 0, sizeof(OPENFILENAME));
szFileName[0] = 0;

LPSTR psz = ConvertToANSI(pwszDisplayName);
if (psz){ lstrcpy(szFileName, psz); delete [] psz;}

ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = _hwndDoc;
ofn.lpstrFilter = g_szFileFilter;
ofn.nFilterIndex = 1L;
ofn.lpstrTitle = NULL;
ofn.lpstrFile = szFileName;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrDefExt = "*";
ofn.Flags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_EXPLORER |
OFN_NOREADONLYRETURN | OFN_PATHMUSTEXIST;

if (GetSaveFileName(&ofn))
bstrFileName = ConvertToBSTR(szFileName);
}
else
{
bstrFileName = SysAllocString(pwszDisplayName);
}

// Save the file to disk using IPersistFile...
if (bstrFileName)
{
IPersistFile* ppf = 0;
hr = _poleobj->QueryInterface(IID_IPersistFile, (void**)&ppf);
if (SUCCEEDED(hr))
{
hr = ppf->Save(bstrFileName, FALSE);
if (SUCCEEDED(hr) && (NULL == _pmkfile))
{
// If we don't have one already, create a moniker so we don't
// need to ask for the file name again during simple save...
CreateFileMoniker(bstrFileName, &_pmkfile);
SaveDocTitle(bstrFileName);
UpdateTitleBar();
}
ppf->Release();
}
SysFreeString(bstrFileName);
}

if (pwszDisplayName)
CoTaskMemFree(pwszDisplayName);

return SUCCEEDED(hr);
}


ULONG _cRef; // Reference count
HWND _hwndDoc; // MDI child window
HWND _hwndMDI; // MDI client window (parent)
DWORD _dwDocId; // ID for this document (debug)
DWORD _dwAdviseTok; // Advise sink token

// File interfaces
IBindCtx* _pbctx;
IMoniker* _pmkfile; // Moniker to file opened
IStorage* _pstgfile; // Temp storage to hold OLE object

// Object interfaces
IOleObject* _poleobj; // Our embedded OLE object
IOleInPlaceObject* _pipobj; // IP pointer (when active)
IOleDocumentView* _pdocv; // DocView pointer (when active)
IOleCommandTarget* _pcmdt; // CommandTarget (for Print)

HBITMAP _hbmDeactive; // Bitmap image of document (when deactive)
HMENU _hmenuHidden; // Merged menu that should be hidden when in Print Preview
BSTR _bstrDocName; // Name of the file opened

// Bit Flags
BOOL _fDisconnected:1;
BOOL _fVisible:1;
BOOL _fSaved:1;
BOOL _fInPrintPreview:1;
BOOL _fShouldRestore:1;
BOOL _fIsHTMLFile:1;


extern const GUID CLSID_EXCELWORKBOOK = {0x00020820,0x0000,0x0000,{0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x46}};
extern const GUID CLSID_EXCELCHART = {0x00020821,0x0000,0x0000,{0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x46}};
extern const GUID CLSID_PPTPRESENTATION = {0x64818D10,0x4F9B,0x11CF,{0x86,0xEA,0x00,0xAA,0x00,0xB9,0x29,0xE8}};
extern const GUID CLSID_WORDDOCUMENT = {0x00020906,0x0000,0x0000,{0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x46}};
extern const GUID CLSID_HTMLDOCUMENT = {0x25336920,0x03F9,0x11CF,{0x8F,0xD0,0x00,0xAA,0x00,0x68,0x6F,0x13}};

// Inplace Print Preview GUIDs...
extern const GUID IID_IPreviewCallback = {0xB722BCD5,0x4E68,0x101B,{0xA2,0xBC,0x00,0xAA,0x00,0x40,0x47,0x70}};
extern const GUID IID_IInplacePrintPreview = {0xB722BCD4,0x4E68,0x101B,{0xA2,0xBC,0x00,0xAA,0x00,0x40,0x47,0x70}};




这是一个MDI的程序,你把MDI的hwnd 改称你的控件的main hwnd就行了。
我只贴出了部分代码,其它的要靠自己哦。得到了_poleobj,你害怕什么?想要什么都可以做了。


用C#,可能要很费劲啊!把这些WinApi非托管函数调用都重新的声明一遍吧,好累哦!
估计你根本从C#得不到任何的帮助,而且也解决不了引用计数、句柄遗失造成的内存泄漏问题。com需要自己AddRef 、Release 哦。


DataBind 2004-09-10
  • 打赏
  • 举报
回复
STDMETHODIMP_(BOOL) CFramerDocument::OpenFileAsDocObject(BSTR* pbstrFile)
{
TRACE1("CFramerDocument[%d]::OpenFileAsDocObject() -- Bind to file\n", _dwDocId);
CLSID clsid;
IPersistStorage* pipstg = NULL;

if ((NULL == pbstrFile) || (SysStringLen(*pbstrFile) == 0))
return E_INVALIDARG;

// Take the file path and create a moniker for binding...
HRESULT hr = CreateFileMoniker(*pbstrFile, &_pmkfile);
if (FAILED(hr)) return FALSE;

// If the file is not a Stg file, assume it is HTML document
// and just display it as read-only web page...
if (StgIsStorageFile(*pbstrFile) != S_OK)
{
_fIsHTMLFile = TRUE;
SaveDocTitle(*pbstrFile);
return CreateNewDocObject(CLSID_HTMLDOCUMENT);
}

// Bind to the file and copy the data from the file into a storage as if
// the document is an embedded OLE object. (This is needed since EXCEL won't
// let you ip activate anything loaded with IPersistFile.)
hr = _pmkfile->BindToObject(_pbctx, NULL, IID_IPersistStorage, (void**)&pipstg);
if (SUCCEEDED(hr))
{
hr = pipstg->GetClassID(&clsid);
if (SUCCEEDED(hr))
hr = pipstg->Save(_pstgfile, FALSE);
pipstg->Release();
}

// If the copy worked and we know the CLSID for the server, we can
// proceed to ip activation. Otherwise, bail out now...
if (FAILED(hr)) return FALSE;

// We'll save the name of the file (minus the path) for display...
SaveDocTitle(*pbstrFile);

// Call CreateNewDocObject to actually start the inplace session and
// load from the data saved...
return CreateNewDocObject(clsid);


}

STDMETHODIMP_(BOOL) CFramerDocument::CreateNewDocObject(REFCLSID rclsid)
{
TRACE1("CFramerDocument[%d]::CreateNewDocObject()\n", _dwDocId);
IUnknown* punk = NULL;
IPersistStorage* pipstg = NULL;

// Don't load if an object has already been loaded...
if ((!_pstgfile) || (_poleobj)) return FALSE;

// Create an inproc handler and load the data from IStorage...
HRESULT hr = CoCreateInstance(rclsid, NULL, CLSCTX_ALL, IID_IUnknown, (void**)&punk);
if (FAILED(hr) || (NULL == punk)) return FALSE;

// Load the data using IPersistStorage...
hr = punk->QueryInterface(IID_IPersistStorage, (void**)&pipstg);
if (SUCCEEDED(hr))
{
// If we opened a file and have moniker, load from exisitng data...
if (_pmkfile)
hr = pipstg->Load(_pstgfile);
else
hr = pipstg->InitNew(_pstgfile);

pipstg->Release();
}
else
{
IPersistFile* pipf = NULL;
LPOLESTR pwszDisplayName = NULL;

hr = _pmkfile->GetDisplayName(_pbctx, NULL, &pwszDisplayName);
if (SUCCEEDED(hr))
{
hr = punk->QueryInterface(IID_IPersistFile, (void**)&pipf);
if (SUCCEEDED(hr))
{
pipf->Load(pwszDisplayName, STGM_READ);
pipf->Release();
}
CoTaskMemFree(pwszDisplayName);
}
}

// Check if the object could not be initialized...
if (FAILED(hr)) {punk->Release(); return FALSE;}

// We need IOleObject so ask. If object doesn't support it we should fail...
hr = punk->QueryInterface(IID_IOleObject, (void **)&_poleobj);
punk->Release(); // Release punk here just in case...
if (FAILED(hr)) return FALSE;

// Fudge factor -- if you make the following calls too quickly, PPT will hang...
Sleep(250);

// Run the object (object must support IRunnableObject)...
OleRun(_poleobj);

// SetClientSite is critical for DocObjects. If this fails the
// object doesn't support inplace activation!!
hr = _poleobj->SetClientSite(static_cast<IOleClientSite*>(this));
if (FAILED(hr))
{
_poleobj->Release();
_poleobj = NULL;
return FALSE;
}

// Lock the object in memory and make a few housekeeping calls...
OleLockRunning(_poleobj, TRUE, FALSE);

_poleobj->SetHostNames(L"Framer Ex", _bstrDocName);
_poleobj->Advise(static_cast<IAdviseSink*>(this), &_dwAdviseTok);

return TRUE;
}

DataBind 2004-09-10
  • 打赏
  • 举报
回复
核心代码:
STDMETHODIMP_(BOOL) CFramerApp::OpenFile()
{
OPENFILENAME ofn;
CHAR szFile[MAX_PATH];
BSTR bstrFile = NULL;

memset(&ofn, 0, sizeof(OPENFILENAME));
szFile[0] = 0;

ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = _hwndApp;
ofn.lpstrFilter = g_szFileFilter;
ofn.nFilterIndex = 1L;
ofn.lpstrTitle = NULL;
ofn.lpstrFile = szFile;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrDefExt = "*";
ofn.Flags = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST;

if ((GetOpenFileName(&ofn)) &&
(NULL != (bstrFile = ConvertToBSTR(szFile))))
{
CFramerDocument* pv = new CFramerDocument(_dwNumDocs++);
if (pv == NULL) return FALSE;

if ((pv->CreateDocWindow(_hwndMDI)) &&
(pv->OpenFileAsDocObject(&bstrFile)))
{
pv->ShowDocWindow();
SysFreeString(bstrFile);
return TRUE;
}
pv->CloseDocWindow();
SysFreeString(bstrFile);
MessageBox(_hwndApp, "Cannot open the specfied file!", "Open", MB_ICONHAND);
}

return FALSE;
}

STDMETHODIMP_(BOOL) CFramerDocument::CreateDocWindow(HWND hwndMDIClient)
{
TRACE1("CFramerDocument[%d]::CreateDocWindow()\n", _dwDocId);

MDICREATESTRUCT mdic; ZeroMemory(&mdic, sizeof(MDICREATESTRUCT));
CHAR szDocName[50];
wsprintf(szDocName, "Document%d", _dwDocId);

HRESULT hr = CreateBindCtx(0, &_pbctx);
if (FAILED(hr)) return FALSE;

hr = StgCreateDocfile(NULL, STGM_TRANSACTED | STGM_READWRITE | STGM_SHARE_EXCLUSIVE |
STGM_CREATE | STGM_DELETEONRELEASE, 0, &_pstgfile);

if (FAILED(hr)){_pbctx->Release(); return FALSE;}

mdic.szClass = "FramerExDocWnd";
mdic.szTitle = szDocName;
mdic.hOwner = g_hInstance;
mdic.lParam = (LPARAM)this;

_hwndMDI = hwndMDIClient;

_hwndDoc = (HWND)SendMessage(_hwndMDI, WM_MDICREATE, 0, (LPARAM)(LPMDICREATESTRUCT)&mdic);
if (NULL == _hwndDoc) return FALSE;

ShowWindow(_hwndDoc, SW_HIDE);

_bstrDocName = ConvertToBSTR(szDocName);

return TRUE;
}
melonlee 2004-09-10
  • 打赏
  • 举报
回复
UPUP
就冲你UP有分!!!
Mark
yzg100 2004-09-10
  • 打赏
  • 举报
回复
UP
jacky4214300 2004-09-10
  • 打赏
  • 举报
回复
up!
就冲你UP有分!!!
DataBind 2004-09-10
  • 打赏
  • 举报
回复
微软有一个例子,到微软的站点找找。用VC写的。
huohuaxu 2004-09-09
  • 打赏
  • 举报
回复
接分
bluefox168 2004-09-09
  • 打赏
  • 举报
回复
高手回答呀?
bluefox168 2004-09-08
  • 打赏
  • 举报
回复
没有人知道吗?up
tangyy 2004-09-08
  • 打赏
  • 举报
回复
好东西,要了,哈哈
kevinboy 2004-09-08
  • 打赏
  • 举报
回复
up
nm3000 2004-09-08
  • 打赏
  • 举报
回复
up
bluefox168 2004-09-07
  • 打赏
  • 举报
回复
用什么语言写没关系,主要是该怎么写?希望知道的提供参考,我知道很多人想知道。分可以开贴再加!
DataBind 2004-09-07
  • 打赏
  • 举报
回复
改用VC++吧,别用C#.dot net framework不是所有客户端都有的啊。
我写了一个了,有问题就问我吧。
主要是设制子窗体。
控件是父窗体,word的窗体为子窗体。
wangxy0919 2004-09-07
  • 打赏
  • 举报
回复
up
也想实现这个功能,学习
gxboy 2004-09-07
  • 打赏
  • 举报
回复
编写ocx??三言两句说不清 UP GZ
bluefox168 2004-09-07
  • 打赏
  • 举报
回复
我想问的就是怎样编写那个ocx控件
加载更多回复(41)

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧