怎样实现作为ole对象嵌入的word文档的打印和打印预览功能啊?
为什么我这段代码不起作用呀? hr = pItem->m_lpObject->QueryInterface(IID_IOleDocument,(void**)&pDocument);
hr = pDocument->QueryInterface(IID_IOleCommandTarget,(void**)&pCmd);
hr = pCmd->Exec(NULL,OLECMDID_PRINTPREVIEW,OLECMDEXECOPT_DODEFAULT,NULL,NULL);
if(SUCCEEDED(hr))
AfxMessageBox("OK");
pCmd->Release();
pDocument->Release();
hr 返回了成功,但是程序没有反应,请多指教!!!!
为什么我这段代码不起作用呀? hr = pItem->m_lpObject->QueryInterface(IID_IOleDocument,(void**)&pDocument);
hr = pDocument->QueryInterface(IID_IOleCommandTarget,(void**)&pCmd);
hr = pCmd->Exec(NULL,OLECMDID_PRINTPREVIEW,OLECMDEXECOPT_DODEFAULT,NULL,NULL);
if(SUCCEEDED(hr))
AfxMessageBox("OK");
pCmd->Release();
pDocument->Release();
hr 返回了成功,但是程序没有反应,请多指教!!!!
你在视图类中重载OnPreparePrinting(CPrintInfo* pInfo)不就可以打印Word了吗?
我手头没有代码。
但是没有反应,不知道为什么?用 Word 中的方法 doc.PrintPreview() 提示 视图无效。
请问还有什么方法呢,是什么地方的问题?
void CHelloCtrl::DoHello()
{
// 创建打印线程
m_pPrintThread = (CPrintThread*)
AfxBeginThread(RUNTIME_CLASS(CPrintThread),
THREAD_PRIORITY_NORMAL, CREATE_SUSPENDED, NULL);
m_pPrintThread- >ResumeThread();
}
在PrintThread.h中添加新成员变量
CPrintDoc* m_pPrintDoc和CPrintFrame* m_pPrintFrame,
并在构造函数和析构函数中完成对它们的初始设置和清除,
确认在PrintThread.h中已加入语句#include
"PrintDoc.h"和#include "PrintFrame.h";
CPrintThread::CPrintThread()
{
m_pPrintDoc=NULL;
m_pPrintFrame=NULL;
}CPrintThread::~CPrintThread()
{
if (m_pPrintDoc!=NULL)
delete m_pPrintFrame;
if (m_pPrintFrame!=NULL)
delete m_pPrintDoc;
}
在PrintThread.cpp的CPrintThread::InitInstance中,进行创建窗体CPrintFrame,确认在PrintThread.cpp中已加入语句#include "PrintFrame.h";
BOOL CPrintThread::InitInstance()
{
// 创建文档/视图框架
CPrintFrame* pFrame = new CPrintFrame;
m_pMainWnd = pFrame;
m_pPrintFrame=pFrame;m_pPrintDoc=new CPrintDoc;CCreateContext context;
context.m_pCurrentDoc = m_pPrintDoc;
context.m_pNewViewClass = RUNTIME_CLASS(CPrintView);
pFrame- >Create(NULL,"打印主窗体",
WS_OVERLAPPEDWINDOW,CRect(0,0,100,100),
NULL,NULL,0,&context);
pFrame- >InitialUpdateFrame(m_pPrintDoc, TRUE);return TRUE;
}在PrintView.h的CPrintView中,添加成员函数CPrintDoc* GetDocument(),确认在PrintView.h中已加入语句#include "PrintDoc.h";
CPrintDoc* CPrintView::GetDocument()
{
ASSERT(m_pDocument- >IsKindOf
(RUNTIME_CLASS(CPrintDoc)));
return (CPrintDoc*)m_pDocument;
}
---- 四、实现ActiveX打印预览技术 ---- 下面介绍利用上面的技术成果来实现ActiveX的打印预览技术,实现步骤如下: 在上面工作的基础上,用ClassWizard对CPrintView类实现OnPreparePrinting函数,如下:
BOOL CPrintView::OnPreparePrinting(CPrintInfo* pInfo)
{
// 准备打印
return DoPreparePrinting(pInfo);
}
用ClassWizard在Automation页中为CHelloCtrl添加一方法void DoPreview(),外部名亦为DoPreview;
void CHelloCtrl::DoPreview()
{
// 进行打印预览
::PostMessage(m_pPrintThread- >m_pPrintFrame- >
GetActiveView()- >m_hWnd,WM_USER_PREVIEW,0,0);
}在PrintView.h中添加#define WM_USER_PREVIEW WM_USER+10在PrintView.cpp中的消息映射中添加ON_MESSAGE(WM_USER_PREVIEW, DoPreview),形成如下:
BEGIN_MESSAGE_MAP(CPrintView, CView)
ON_MESSAGE(WM_USER_PREVIEW, DoPreview)
//{{AFX_MSG_MAP(CPrintView)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()为类CPrintView添加成员函数LRESULT DoPreview(WPARAM wParam, LPARAM lParam)实现CPrintView::DoPreview如下:
LRESULT CPrintView::DoPreview
(WPARAM wParam, LPARAM lParam)
{
// 进入打印预览
OnFilePrintPreview();return 0;
}
为CPrintView添加public成员变量COleControl* m_pControlPreview,并初始化如下:
CPrintView::CPrintView()
{
m_pControlPreview=NULL; //
初始化要预览的ActiveX控件类为空
}在CPrintView::OnDraw中对控件内容进行显示
void CPrintView::OnDraw(CDC* pDC)
{
if (m_pControlPreview==NULL)
pDC->TextOut(0,0,"No Preview View");
else {
CRect controlRect;
m_pControlPreview- >GetClientRect(&controlRect);
CRect previewRect(0,0,controlRect.
Width(),controlRect.Height());
m_pControlPreview- >OnDraw
(pDC,controlRect,controlRect);
}
}
用ClassWizard在Automation页中为CHelloCtrl添加一方法void SetPreviewControl(),外部名亦为SetPreviewControl,对其实现如下:
void CHelloCtrl::SetPreviewControl()
{
// 设置要预览的View
CView* pView=m_pPrintThread- >
m_pPrintFrame- >GetActiveView();
CPrintView* pPrintView=(CPrintView*)pView;
pPrintView- >m_pControlPreview=this;
}---- 在ActiveX Control Test Container测试,激活方法次序为DoHello、SetPreviewControl、DoPreview。
我的打印预览的视图是空白的,在OnDraw函数中这样写也没有用?
CView* pView = pItem->GetActiveView();
pView->OnDraw(pDC);
这样做就死机了,不能找到绘制item的那个视图
{
CWebOfficeDoc* pDoc = GetDocument();
// TODO: 在此添加绘制代码
if(m_pSelection==NULL)
{
POSITION m_Pos=pDoc->GetStartPosition();
m_pSelection=(CWebOfficeOleClientItem*)pDoc->GetNextClientItem(m_Pos);
}
if(m_pSelection!=NULL)
{
LPDISPATCH lpDisp;
lpDisp=m_pSelection->GetIDispatch();
_Application_Word m_WordApp;
_Document_Word m_WordDoc;
m_WordDoc.AttachDispatch(lpDisp,TRUE);
m_WordApp=m_WordDoc.GetApplication();
m_WordApp.SetScreenUpdating(FALSE); CRect m_Rect;
m_pSelection->OnGetItemPosition(m_Rect);
m_pSelection->Draw(pDC,m_Rect);
m_WordApp.SetScreenUpdating(TRUE);
m_WordDoc.ReleaseDispatch();
m_WordApp.ReleaseDispatch();
}
}