Click here to Skip to main content
15,895,746 members
Articles / Desktop Programming / WTL

A fast and lightweight cell control

Rate me:
Please Sign up or sign in to vote.
4.42/5 (31 votes)
11 Mar 2008CPOL1 min read 91K   4.5K   81  
A fast and lightweight cell control for displaying tabular data. The cell is a custom control derived from ATL::CWindow.
// MfcTestView.cpp : implementation of the CMfcTestView class
//

#include "stdafx.h"
#include "MfcTest.h"

#include "MfcTestDoc.h"
#include "MfcTestView.h"
#include "../include/msg.h"
using namespace mycell;

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CMfcTestView

IMPLEMENT_DYNCREATE(CMfcTestView, CView)

BEGIN_MESSAGE_MAP(CMfcTestView, CView)
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
	ON_NOTIFY(OCN_GETCELLTEXT,100,_OnGetCellText)
	ON_WM_CREATE()
	ON_WM_SIZE()
END_MESSAGE_MAP()

// CMfcTestView construction/destruction

CMfcTestView::CMfcTestView()
{
	// TODO: add construction code here
	m_pGrid.CoCreateInstance(CLSID_Grid);
	_ASSERT(m_pGrid!=NULL);
}

CMfcTestView::~CMfcTestView()
{
}

BOOL CMfcTestView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
	
	return CView::PreCreateWindow(cs);
}
void CMfcTestView::_OnGetCellText(NMHDR* lpnm, LRESULT*)
{
	NM_GRIDVIEW* lp=(NM_GRIDVIEW*)lpnm;
	LPTSTR lpszText=(LPTSTR)lp->lParam;
	OnGetCellText(lp->hdr.hwndFrom,lp->row,lp->col,lpszText);
}
void CMfcTestView::OnGetCellText(HWND hWndFrom,int row,int col,LPTSTR lpszText)
{
	CComBSTR label;
	m_pGrid->GetColLabel(col,&label);
	CString str(label);
	wsprintf(lpszText,_T("%s%d"),str,row+1);       
}
void CMfcTestView::init_grid()
{											 
	int const cols=200;
	m_pGrid->put_cols(cols);	
	m_pGrid->put_rows(99999);
	TCHAR buf[50];													   
	for(int i=0;i<cols;++i){
		int const col=i+1;
		if(col<27){					//A~Z
			buf[0]=(col+0x40);
			buf[1]=0;
		}else if(col<703){				//AA~ZZ
			buf[1]=(col-1)%26+'A';//0x41;
			buf[0]=(col-1)/26-1+'A';//0x40;
			buf[2]=0;
		}
		m_pGrid->SetColLabel(i,CComBSTR(buf));
	}
}

int CMfcTestView::OnCreate(LPCREATESTRUCT lpcs)
{
	init_grid();
	RECT rc={0,0,800,600};
	m_pGrid->Create((OLE_HANDLE)m_hWnd,&rc,NULL,WS_CHILD|WS_VISIBLE|WS_CLIPSIBLINGS|WS_CLIPCHILDREN,/*WS_EX_STATICEDGE*/0,100,0);
	m_pGrid->put_listener((OLE_HANDLE)m_hWnd);
	return 0;
}
void CMfcTestView::OnSize(UINT, int width, int height)
{
	if(width>0 && height>0 && m_pGrid.p)
	{
		OLE_HANDLE nWnd;
		m_pGrid->GetHWnd(&nWnd);
		::MoveWindow((HWND)nWnd,0,0,width,height,TRUE);
	}
}
// CMfcTestView drawing

void CMfcTestView::OnDraw(CDC* /*pDC*/)
{
	CMfcTestDoc* pDoc = GetDocument();
	//ASSERT_VALID(pDoc);
	if (!pDoc)
		return;

	// TODO: add draw code for native data here
}


// CMfcTestView printing

BOOL CMfcTestView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CMfcTestView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CMfcTestView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}


// CMfcTestView diagnostics

#ifdef _DEBUG
void CMfcTestView::AssertValid() const
{
	CView::AssertValid();
}

void CMfcTestView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CMfcTestDoc* CMfcTestView::GetDocument() const // non-debug version is inline
{
	//ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMfcTestDoc)));
	return (CMfcTestDoc*)m_pDocument;
}
#endif //_DEBUG


// CMfcTestView message handlers

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
China China
My name is Yanxueming,i live in Chengdu China.Graduated from UESTC in 1999.

Comments and Discussions