www.pudn.com > EasyReport.rar > EasyReportView.cpp


// EasyReportView.cpp : implementation of the CEasyReportView class 
// 
 
#include "stdafx.h" 
#include "EasyReport.h" 
 
#include "EasyReportDoc.h" 
#include "EasyReportView.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CEasyReportView 
 
IMPLEMENT_DYNCREATE(CEasyReportView, CScrollView) 
 
BEGIN_MESSAGE_MAP(CEasyReportView, CScrollView) 
	//{{AFX_MSG_MAP(CEasyReportView) 
	ON_WM_SIZE() 
	ON_COMMAND(ID_DELETECOL, OnDeletecol) 
	ON_COMMAND(ID_DELETEROW, OnDeleterow) 
	ON_COMMAND(ID_INSCOL, OnInscol) 
	ON_COMMAND(ID_INSROW, OnInsrow) 
	ON_COMMAND(ID_MERGE, OnMerge) 
	ON_COMMAND(ID_UNMERGE, OnUnmerge) 
	//}}AFX_MSG_MAP 
	// Standard printing commands 
	ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint) 
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint) 
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview) 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CEasyReportView construction/destruction 
 
CEasyReportView::CEasyReportView() 
{ 
	m_pGridCtrl = NULL; 
} 
 
CEasyReportView::~CEasyReportView() 
{ 
	if (m_pGridCtrl) 
		delete m_pGridCtrl; 
} 
 
BOOL CEasyReportView::PreCreateWindow(CREATESTRUCT& cs) 
{ 
	// TODO: Modify the Window class or styles here by modifying 
	//  the CREATESTRUCT cs 
 
	return CScrollView::PreCreateWindow(cs); 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CEasyReportView drawing 
 
void CEasyReportView::OnDraw(CDC* pDC) 
{ 
	CEasyReportDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc); 
	// TODO: add draw code for native data here 
} 
 
 
///////////////////////////////////////////////////////////////////////////// 
// CEasyReportView printing 
 
BOOL CEasyReportView::OnPreparePrinting(CPrintInfo* pInfo) 
{ 
	// default preparation 
	return DoPreparePrinting(pInfo); 
} 
 
void CEasyReportView::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo) 
{ 
	if (m_pGridCtrl) 
		m_pGridCtrl->OnBeginPrinting(pDC, pInfo); 
} 
void CEasyReportView::OnPrint(CDC* pDC, CPrintInfo* pInfo)  
{ 
	if (m_pGridCtrl) 
		m_pGridCtrl->OnPrint(pDC, pInfo); 
} 
void CEasyReportView::OnEndPrinting(CDC* pDC, CPrintInfo* pInfo) 
{ 
	if (m_pGridCtrl) 
		m_pGridCtrl->OnEndPrinting(pDC, pInfo); 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CEasyReportView diagnostics 
 
#ifdef _DEBUG 
void CEasyReportView::AssertValid() const 
{ 
	CScrollView::AssertValid(); 
} 
 
void CEasyReportView::Dump(CDumpContext& dc) const 
{ 
	CScrollView::Dump(dc); 
} 
 
CEasyReportDoc* CEasyReportView::GetDocument() // non-debug version is inline 
{ 
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CEasyReportDoc))); 
	return (CEasyReportDoc*)m_pDocument; 
} 
#endif //_DEBUG 
 
///////////////////////////////////////////////////////////////////////////// 
// CEasyReportView message handlers 
 
BOOL CEasyReportView::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)  
{ 
	if (m_pGridCtrl && IsWindow(m_pGridCtrl->m_hWnd)) 
        if (m_pGridCtrl->OnCmdMsg(nID, nCode, pExtra, pHandlerInfo)) 
            return TRUE; 
	return CScrollView::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo); 
} 
 
void CEasyReportView::OnSize(UINT nType, int cx, int cy)  
{ 
	CScrollView::OnSize(nType, cx, cy); 
	 
	if (m_pGridCtrl->GetSafeHwnd()) 
	{ 
		CRect rect; 
		GetClientRect(rect); 
		m_pGridCtrl->MoveWindow(rect); 
	}	 
	 
} 
void CEasyReportView::OnInitialUpdate() 
{ 
	CScrollView::OnInitialUpdate(); 
 
	CSize sizeTotal; 
	if (m_pGridCtrl == NULL) 
	{ 
		// Create the Gridctrl object 
		m_pGridCtrl = new CGridCtrl; 
		if (!m_pGridCtrl) return; 
 
		// Create the Gridctrl window 
		CRect rect; 
		GetClientRect(rect); 
		m_pGridCtrl->Create(rect, this,100); 
 
		// fill it up with stuff 
		m_pGridCtrl->SetEditable(TRUE); 
		m_pGridCtrl->EnableDragAndDrop(TRUE); 
 
		try { 
			m_pGridCtrl->SetRowCount(50); 
			m_pGridCtrl->SetColumnCount(15); 
			m_pGridCtrl->SetFixedRowCount(1); 
			m_pGridCtrl->SetFixedColumnCount(1); 
		} 
		catch (CMemoryException* e) 
		{ 
			e->ReportError(); 
			e->Delete(); 
			return; 
		} 
		CFont mFont; 
		mFont.CreateFont(50, 20,270,270, 200, TRUE,0,0, DEFAULT_CHARSET, 
                             OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS, DEFAULT_QUALITY, 
                             DEFAULT_PITCH | FF_DONTCARE, "Arial"); 
		/*BOOL CreateFont(  
                 int nHeight,           //字体的高度 
                 int nWidth,            //字体的宽度 
                 int nEscapement,       //字体显示的角度 
                 int nOrientation,      //字体的角度 
                 int nWeight,           //字体的磅数 
                 BYTE bItalic,          //斜体字体 
                 BYTE bUnderline,       //带下划线的字体 
                 BYTE cStrikeOut,       //带删除线的字体 
                 BYTE nCharSet,         //所需的字符集 
                 BYTE nOutPrecision,    //输出的精度 
                 BYTE nClipPrecision,   //裁减的精度  
                 BYTE nQuality,         //逻辑字体与输出设备的实际 
                                        //字体之间的精度 
                 BYTE nPitchAndFamily,  //字体间距和字体集 
                 LPCTSTR lpszFacename   //字体名称 
               );*/ 
		// fill rows/cols with text 
		for (int row = 0; row < m_pGridCtrl->GetRowCount(); row++) 
		{ 
			for (int col = 0; col < m_pGridCtrl->GetColumnCount(); col++) 
			{  
				GV_ITEM Item; 
				Item.mask = GVIF_TEXT|GVIF_FORMAT; 
				Item.row = row; 
				Item.col = col; 
				if (row < 1)  
				{ 
					if(col==0) 
					{ 
					Item.nFormat = DT_LEFT|DT_WORDBREAK; 
					Item.strText.Format(_T("")); 
					} 
					else 
					{ 
						Item.nFormat = DT_CENTER|DT_WORDBREAK; 
						CString str=""; 
						int k=col%26,s=col/26; 
						if(s>0&&col!=26) 
							if(k==0) 
								str.Format(_T("%c"),s+64-1); 
							else 
								str.Format(_T("%c"),s+64); 
						 
						if(k==0) 
							k=26; 
						CString st; 
						st.Format(_T("%c"),k+64); 
						str=str+st; 
						Item.strText.Format(_T(str)); 
						mFont.GetLogFont(&Item.lfFont); 
					} 
 
				}  
				else  
					if (col < 1) 
					{ 
					Item.nFormat = DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_END_ELLIPSIS|DT_NOPREFIX; 
					Item.strText.Format(_T("%d"),row); 
					} else  
					{ 
						Item.nFormat = DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_END_ELLIPSIS|DT_NOPREFIX; 
						Item.strText.Format(_T("")); 
					} 
				m_pGridCtrl->SetItem(&Item); 
				if(col==0) 
					m_pGridCtrl->SetColumnWidth(col,40); 
				else 
					m_pGridCtrl->SetColumnWidth(col,75); 
			} 
			m_pGridCtrl->SetRowHeight(row,25); 
		 
		} 
			 
	} 
	sizeTotal.cx = sizeTotal.cy = 100; 
	SetScrollSizes(MM_TEXT, sizeTotal); 
} 
 
void CEasyReportView::SetGridHead() 
{ 
	/*CString str1,str2; 
	int Int,mod,IntTem; 
	for(int col=1;colGetColumnCount();col++) 
	{ 
		Int=int(col/27); 
		mod=col%27; 
		if(Int>0) 
		{ 
			IntTem =Int+65; 
			str1=IntTem; 
		} 
		mod += 64; 
		str2=mod; 
		if(Int>0) 
			str2 += str1; 
		m_pGridCtrl->SetItemText(0,col,str2);	 
	}*/ 
} 
void CEasyReportView::OnDeletecol()  
{ 
	CCellID idcell; 
	idcell=m_pGridCtrl->GetFocusCell(); 
	if(m_pGridCtrl->GetColumnCount()>2) 
		m_pGridCtrl->DeleteColumn(idcell.col); 
	m_pGridCtrl->Refresh(); 
	 
} 
 
void CEasyReportView::OnDeleterow()  
{ 
	CCellID idcell; 
	idcell=m_pGridCtrl->GetFocusCell(); 
	if( (idcell.row!=m_pGridCtrl->GetRowCount()-1) && m_pGridCtrl->GetRowCount()>2) 
		m_pGridCtrl->DeleteRow(idcell.row); 
	m_pGridCtrl->Refresh();	 
	 
} 
 
void CEasyReportView::OnInscol()  
{ 
	CCellID idcell; 
	idcell=m_pGridCtrl->GetFocusCell(); 
	if(idcell.col>0) 
		m_pGridCtrl->InsertColumn("head",DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_END_ELLIPSIS|DT_NOPREFIX,idcell.col); 
	SetGridHead(); 
	m_pGridCtrl->Refresh(); 
	 
} 
 
void CEasyReportView::OnInsrow()  
{ 
	CCellID idcell; 
	idcell=m_pGridCtrl->GetFocusCell(); 
	if(idcell.row>0) 
		m_pGridCtrl->InsertRow("",idcell.row); 
	m_pGridCtrl->Refresh(); 
	 
} 
 
void CEasyReportView::OnMerge()  
{ 
	 
	CCellRange rang; 
	int maxrow=rang.GetMaxRow(); 
	if(maxrow!=m_pGridCtrl->GetRowCount()-1) 
		m_pGridCtrl->MergeSelectedCells(); 
	m_pGridCtrl->Refresh(); 
	 
} 
 
void CEasyReportView::OnUnmerge()  
{ 
	m_pGridCtrl->UnMergeSelectedCells(); 
	m_pGridCtrl->Refresh(); 
	 
}