積累的VC編程小技巧之打印相關
1.修改打印預覽的ToolBar
為AFX_IDD_PREVIEW_TOOLBAR這個ID創建一個DialogBar。則系統就會用新創建的DialogBar代替系統默認的那個
2.關于打印
1.要打印哪個視就
((CMainFrame*)AfxGetMainWnd())->m_wndSplitter.SetActivePane(...)
//要打印的那個視對應的Pane
2.有一個單文檔工程,文檔窗口被切分:左視圖由CTreeView 的派生類管理,右視圖由CListView 的派生類CMyListView(其為風格為LVS_REPORT)管理,我想為右視圖添加打印和打印預覽,我在MyListView.cpp中添加了
ON_COMMAND(ID_FILE_PRINT,CListView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW,CListView::OnFilePrintPreview)還有
BOOL CMyListView::OnPreparePrinting(CPrintInfo* pInfo)
{
// TODO: call DoPreparePrinting to invoke the Print dialog box
// return CListView::OnPreparePrinting(pInfo);
pInfo->SetMaxPage(2);
BOOL bret=DoPreparePrinting(pInfo);
pInfo->m_nNumPreviewPages=2;
return bret;
}
3. 下面是從MSDN中摘出來的一段,是用來改變消息路由的。用了這段代碼之后,CView中的消息(菜單,控件,子窗口)將先被CMyShape類來處理。不知道你要的是不是這樣的效果。
// This example illustrates extending the framework's standard command
// route from the view to objects managed by the view. This example
// is from an object-oriented drawing application, similar to the
// DRAWCLI sample application, which draws and edits "shapes".
BOOL CMyView::OnCmdMsg(UINT nID, int nCode, void* pExtra,
AFX_CMDHANDLERINFO* pHandlerInfo)
{
// Extend the framework's command route from the view to
// the application-specific CMyShape that is currently selected
// in the view. m_pActiveShape is NULL if no shape object
// is currently selected in the view.
if ((m_pActiveShape != NULL)
&& m_pActiveShape->OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))
return TRUE;
// If the object(s) in the extended command route don't handle
// the command, then let the base class OnCmdMsg handle it.
return CView::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
}
// The command handler for ID_SHAPE_COLOR (menu command to change
// the color of the currently selected shape) was added to
// the message map of CMyShape (note, not CMyView) using ClassWizard.
// The menu item will be automatically enabled or disabled, depending
// on whether a CMyShape is currently selected in the view, that is,
// depending on whether CMyView::m_pActiveView is NULL. It is not
// necessary to implement an ON_UPDATE_COMMAND_UI handler to enable
// or disable the menu item.
BEGIN_MESSAGE_MAP(CMyShape, CCmdTarget)
//{{AFX_MSG_MAP(CMyShape)
ON_COMMAND(ID_SHAPE_COLOR, OnShapeColor)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
如果你只是想調用OnFilePrint( )函數,可以試一試下面的代碼,就和調用其它類中的函數一樣。
CMDIFrameWnd *pFrame =
(CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
// Get the active MDI child window.
CMDIChildWnd *pChild =
(CMDIChildWnd *) pFrame->GetActiveFrame();
// or CMDIChildWnd *pChild = pFrame->MDIGetActive();
// Get the active view attached to the active MDI child
// window.
CMyView *pView = (CMyView *) pChild->GetActiveView();
pView->OnFilePrint( );
4.
void CMyReportView::OnFileOpen()
{
char Filter[] = "Crystal Report files(*.rpt)|*.rpt|All files(*.*)|*.*||";
CRect rect;
CFileDialog OpenDlg(TRUE,0,0,OFN_HIDEREADONLY|OFN_FILEMUSTEXIST,(LPCTSTR)Filter,NULL);
if(OpenDlg.DoModal()!=IDOK) ///顯示文件對話框
return;
CString m_fName=OpenDlg.GetPathName(); ///取得文件名
if(m_CrystalReport)
m_CrystalReport.DestroyWindow();
GetClientRect(rect);
///////////////////創建控件///////////////
if (!m_CrystalReport.Create(AfxRegisterWndClass(0, AfxGetApp()->LoadStandardCursor(IDC_ARROW)),WS_CHILD|WS_VISIBLE,rect,this,IDC_CRYSTALREPORT1))
{
AfxMessageBox("控件創建失敗!");
return ;
}
m_CrystalReport.SetWindowParentHandle((long)(this->m_hWnd));///設置父窗口
m_CrystalReport.SetWindowBorderStyle(0); ///設置為沒有邊框
m_CrystalReport.SetWindowLeft(0); ///左空間
m_CrystalReport.SetWindowTop(0); ///頂部空間
m_CrystalReport.SetWindowControls(FALSE); ///不顯示工具條
m_CrystalReport.SetReportFileName(m_fName); ///設置報表文件
m_CrystalReport.SetWindowWidth(rect.Width()); ///設置窗口寬度
m_CrystalReport.SetWindowHeight(rect.Height()); ///設置窗口高度
m_CrystalReport.SetFormulas(0, "Company=\"VC知識庫\""); ///將報表中的Company變量的值設置為VC知識庫
m_CrystalReport.SetDestination(0); ///設置輸出對象是屏幕
m_CrystalReport.PrintReport(); ///顯示報表
}
void CMyReportView::OnFilePrint()
{
if(m_CrystalReport && m_CrystalReport.GetReportFileName() != "")
{
m_CrystalReport.SetDestination(1); ///設置輸出對象是打印機
m_CrystalReport.PrintReport(); ///打印
}
RFID管理系統集成商 RFID中間件 條碼系統中間層 物聯網軟件集成