⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 designerviewerdlg.cpp

📁 一个很好用的报表控件
💻 CPP
字号:
// DesignerViewerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "DesignerViewer.h"
#include "DesignerViewerDlg.h"
static const char BASED_CODE mFilter[] = "Crystal Reports|*.rpt||";
#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()

/////////////////////////////////////////////////////////////////////////////
// CDesignerViewerDlg dialog

CDesignerViewerDlg::CDesignerViewerDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDesignerViewerDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDesignerViewerDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CDesignerViewerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDesignerViewerDlg)
	DDX_Control(pDX, IDC_BTNDESIGNREPORT, m_DesignerReportBtn);
	DDX_Control(pDX, IDC_BTNPREVIEWREPORT, m_PreviewReportBtn);
	DDX_Control(pDX, IDC_BTNSAVEREPORT, m_SaveReportBtn);
	DDX_Control(pDX, IDC_BTNOPENREPORT, m_OpenReportBtn);
	DDX_Control(pDX, IDC_BTNNEWREPORT, m_NewReportBtn);
	DDX_Control(pDX, IDC_EMBEDDABLECRYSTALREPORTSDESIGNERCTRL1, m_ReportDesigner);
	DDX_Control(pDX, IDC_CRVIEWER1, m_ReportViewer);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDesignerViewerDlg, CDialog)
	//{{AFX_MSG_MAP(CDesignerViewerDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTNNEWREPORT, OnBtnnewreport)
	ON_BN_CLICKED(IDC_BTNOPENREPORT, OnBtnopenreport)
	ON_BN_CLICKED(IDC_BTNSAVEREPORT, OnBtnsavereport)
	ON_BN_CLICKED(IDC_BTNPREVIEWREPORT, OnBtnpreviewreport)
	ON_BN_CLICKED(IDC_BTNDESIGNREPORT, OnBtndesignreport)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDesignerViewerDlg message handlers

BOOL CDesignerViewerDlg::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
	m_ReportViewer.ShowWindow(SW_HIDE);
	m_ReportDesigner.ShowWindow(SW_SHOW); 
	m_ReportViewer.SetEnableExportButton(TRUE);
    m_PreviewReportBtn.ShowWindow(SW_HIDE);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CDesignerViewerDlg::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 CDesignerViewerDlg::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 CDesignerViewerDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CDesignerViewerDlg::OnBtnnewreport() 
{
	// TODO: Add your control notification handler code here
	InitReport(TRUE);	
    m_PreviewReportBtn.ShowWindow(SW_SHOW);
}

void CDesignerViewerDlg::OnBtnopenreport() 
{
	// TODO: Add your control notification handler code here
	InitReport(FALSE);		
    m_PreviewReportBtn.ShowWindow(SW_SHOW);
}

void CDesignerViewerDlg::OnBtnsavereport() 
{
	// TODO: Add your control notification handler code here
	CFileDialog SaveFileDlg(FALSE,"*.rpt",NULL,OFN_EXPLORER, mFilter); 
    int iResult = SaveFileDlg.DoModal();
    if(iResult == IDOK)
    {
        _bstr_t SaveFileName(SaveFileDlg.GetPathName().AllocSysString());
        m_ReportDesigner.SaveReport(SaveFileName);
    }
    else if(iResult == IDCANCEL) 
    {        
        return;
    }	
}

void CDesignerViewerDlg::OnBtnpreviewreport() 
{
	// TODO: Add your control notification handler code here
	m_ReportViewer.RefreshEx(FALSE);
    m_PreviewReportBtn.ShowWindow(SW_HIDE);
    m_ReportDesigner.ShowWindow(SW_HIDE);
    m_DesignerReportBtn.ShowWindow(SW_SHOW);
    m_ReportViewer.ShowWindow(SW_SHOW);    	
}

void CDesignerViewerDlg::OnBtndesignreport() 
{
	// TODO: Add your control notification handler code here
	m_DesignerReportBtn.ShowWindow(SW_HIDE);
    m_ReportViewer.ShowWindow(SW_HIDE);    
    m_PreviewReportBtn.ShowWindow(SW_SHOW);
    m_ReportDesigner.ShowWindow(SW_SHOW);
	
}

void CDesignerViewerDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	
	CDialog::OnCancel();
}

void CDesignerViewerDlg::InitReport(BOOL bNewOrOpen)
{
    if(bNewOrOpen)
    {
		//如果该值为"true",则建立一个新的报表
        m_Application.CreateInstance("CrystalDesignRuntime.Application");
        m_Report = m_Application->NewReport();
    }
    else
    {
		//打开一个报表以修改和预览
        CFileDialog FileOpenDlg(TRUE,"*.rpt",NULL, OFN_EXPLORER, mFilter);
        int iResult = FileOpenDlg.DoModal();        
        if(iResult == IDOK)
        {
            //取得所选择的文件名
			_bstr_t OpenFileName(FileOpenDlg.GetPathName().AllocSysString());
            //建立应用程序实例           
            m_Application.CreateInstance("CrystalDesignRuntime.Application");
            //打开报表
            m_Report = m_Application->OpenReport(OpenFileName);
        }
        else if (iResult == IDCANCEL)
        {
            return;
        }
    }
    //设置报表对象到报表设计控件中
    m_ReportDesigner.SetReportObject(m_Report);
    //设置预览的报表数据源
    m_ReportViewer.SetReportSource(m_Report);
	//设置报表预览的放大比例
    m_ReportViewer.Zoom(1);
    //在Viewer控件中先显示报表
    m_ReportViewer.ViewReport();
    BOOL btnEnabled;
    btnEnabled = m_ReportDesigner.IsWindowEnabled();
    if( btnEnabled == FALSE)
    {
		//设置相关按钮的状态
        m_SaveReportBtn.EnableWindow(TRUE);
        m_PreviewReportBtn.EnableWindow(TRUE);
    }
    if(!m_ReportDesigner.IsWindowVisible())
    {
        m_DesignerReportBtn.ShowWindow(SW_HIDE);
        m_PreviewReportBtn.ShowWindow(SW_SHOW);
        m_ReportViewer.ShowWindow(FALSE);
        m_ReportDesigner.ShowWindow(TRUE);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -