www.gusucode.com > 几个VC++打印小例子-源码程序 > 几个VC++打印小例子-源码程序/code/Dlg_PrintTest/Dlg_PrintTestDlg.cpp
//Download by http://www.NewXing.com // Dlg_PrintTestDlg.cpp : implementation file // #include "stdafx.h" #include "Dlg_PrintTest.h" #include "Dlg_PrintTestDlg.h" #include "PrintView.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About class CAboutDlg : public CDialog { public: CAboutDlg(); // Dialog Data //{{AFX_DATA(CAboutDlg) enum { IDD = IDD_ABOUTBOX }; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: //{{AFX_MSG(CAboutDlg) //}}AFX_MSG DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) // No message handlers //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CDlg_PrintTestDlg dialog CDlg_PrintTestDlg::CDlg_PrintTestDlg(CWnd* pParent /*=NULL*/) : CDialog(CDlg_PrintTestDlg::IDD, pParent) { //{{AFX_DATA_INIT(CDlg_PrintTestDlg) m_strTest = _T(""); //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CDlg_PrintTestDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CDlg_PrintTestDlg) DDX_Text(pDX, IDC_EDIT_PRINTTEST, m_strTest); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CDlg_PrintTestDlg, CDialog) //{{AFX_MSG_MAP(CDlg_PrintTestDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_PRINT_BTN, OnPrintBtn) ON_BN_CLICKED(IDC_PRINT_PREVIEW_BTN, OnPrintPreviewBtn) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CDlg_PrintTestDlg message handlers BOOL CDlg_PrintTestDlg::OnInitDialog() { CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here return TRUE; // return TRUE unless you set the focus to a control } void CDlg_PrintTestDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); } } // If you add a minimize button to your dialog, you will need the code below // to draw the icon. For MFC applications using the document/view model, // this is automatically done for you by the framework. void CDlg_PrintTestDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); } } // The system calls this to obtain the cursor to display while the user drags // the minimized window. HCURSOR CDlg_PrintTestDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CDlg_PrintTestDlg::OnPrintBtn() { // TODO: Add your control notification handler code here UpdateData(TRUE); //在打印过程中,“打印”按钮禁用 GetDlgItem(IDC_PRINT_BTN)->EnableWindow(FALSE); //保存当前主窗口,否则打印结束后主窗口会自动关闭 CFrameWnd *pOldFrameWnd = (CFrameWnd*)AfxGetThread()->m_pMainWnd; //定义一个单文档(SDI)或多文档(MDI)类型指针,因为它们具有打印功能 //此处采用的是多文档类型 CMultiDocTemplate *pMulDocTemp; pMulDocTemp = new CMultiDocTemplate( IDR_PRINT_MENU,//menu id:菜单地址 NULL,//*pDocClass:文档类对象指针 RUNTIME_CLASS(CFrameWnd),//*pFrameClass:窗口类对象指针 RUNTIME_CLASS(CPrintView)//*pViewClass:视口类对象指针 ); //获得文档支持,此处用到的是打印支持 AfxGetApp()->AddDocTemplate(pMulDocTemp); //创建新的窗口:打印窗口 CFrameWnd *pFrameWnd=pMulDocTemp->CreateNewFrame(NULL,NULL); //初始化该窗口 pMulDocTemp->InitialUpdateFrame(pFrameWnd,NULL); //设置当前窗口为打印窗口 AfxGetApp()->m_pMainWnd = pFrameWnd; //设置打印窗口 pFrameWnd->SetWindowText(_T("PrintTest")); //打印之前隐藏该打印窗口 pFrameWnd->ShowWindow(SW_HIDE); //定义CPrintView类的一个对象指针 CPrintView *pPrintView = (CPrintView*)pFrameWnd->GetActiveView(); //CPrintView类的窗口指针指向当前主窗口 pPrintView->m_pDlg=this; //执行CPrintView的打印函数 pPrintView->OnFilePrint(); //重新获得原来的主窗口 AfxGetApp()->m_pMainWnd = pOldFrameWnd; //打印结束后关闭打印窗口 pFrameWnd->DestroyWindow(); //恢复“打印”按钮 GetDlgItem(IDC_PRINT_BTN)->EnableWindow(TRUE); } void CDlg_PrintTestDlg::OnPrintPreviewBtn() { // TODO: Add your control notification handler code here UpdateData(TRUE); //保存当前主窗口,否则打印结束后主窗口会自动关闭 CFrameWnd *pOldFrame = (CFrameWnd*)AfxGetThread()->m_pMainWnd; //定义一个单文档(SDI)或多文档(MDI)类型指针,因为它们具有打印预览功能 //此处采用的是单文档类型 CSingleDocTemplate *pSigDocTemp; pSigDocTemp = new CSingleDocTemplate( IDR_PRINT_MENU,//menu id:菜单地址 NULL,//*pDocClass:文档类对象指针 RUNTIME_CLASS(CFrameWnd),//*pFrameClass:窗口类对象指针 RUNTIME_CLASS(CPrintView)//*pViewClass:视口类对象指针 ); //获得文档支持,此处用到的是打印预览支持 AfxGetApp()->AddDocTemplate(pSigDocTemp); //创建新的窗口:打印预览窗口 CFrameWnd *pFrameWnd=pSigDocTemp->CreateNewFrame(NULL,NULL); //初始化该窗口 pSigDocTemp->InitialUpdateFrame(pFrameWnd,NULL); //设置当前窗口为打印预览窗口 AfxGetApp()->m_pMainWnd = pFrameWnd; //设置打印预览窗口 pFrameWnd->SetWindowText(_T("PreviewTest")); //打印预览之前隐藏该打印预览窗口 pFrameWnd->ShowWindow(SW_HIDE); //设置打印预览窗口最大化 pFrameWnd->ShowWindow(SW_SHOWMAXIMIZED); //定义CPrintView类的一个对象指针 CPrintView *pPrintView = (CPrintView*)pFrameWnd->GetActiveView(); //CPrintView类的对话框指针指向主对话框 pPrintView->m_pDlg=this; //CPrintView类的窗口指针指向原来主窗口 pPrintView->m_pFrameWnd = pOldFrame; pPrintView->m_pDlg=this; //执行CPrintView的打印预览函数 pPrintView->OnFilePrintPreview(); }