首页 > 代码库 > 积累的VC编程小技巧之打印相关

积累的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(); ///打印

}