天天看點

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(); ///列印

}

上一篇: VC小結 

繼續閱讀