Click here to Skip to main content
15,889,116 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 90.9K   4.5K   81  
A fast and lightweight cell control for displaying tabular data. The cell is a custom control derived from ATL::CWindow.
// MainFrm.cpp : implmentation of the CMainFrame class
//
/////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "resource.h"

#include "aboutdlg.h"
#include "MDITestView.h"
#include "ChildFrm.h"
#include "MainFrm.h"
	BOOL IsFileExist(LPCTSTR path)
	{
		WIN32_FIND_DATA FindFileData;
		HANDLE hFind = ::FindFirstFile(path, &FindFileData);
		if(hFind!=INVALID_HANDLE_VALUE){
			::FindClose(hFind);
			return TRUE;
		}
		return FALSE;
	}

LRESULT CExampleListView::OnDblClk(WORD,WORD,HWND,BOOL&)
{
	TCHAR strTmp[MAX_PATH];
	::GetModuleFileName(_Module.GetResourceInstance(),strTmp,MAX_PATH);
	for(int i=lstrlen(strTmp);i>=0;i--)
	{
		if(strTmp[i]=='/' || strTmp[i]=='\\'){
			strTmp[i+1]=0;
			//lstrcat(strTmp,FN_SQLLOG);
			break;
		}
	}
	int idx=lv_.GetCurSel();
	switch(idx)
	{
	case 0:
		{
			lstrcat(strTmp,_T("example1.xml"));
		}break;
	}
	if(IsFileExist(strTmp))
		pMF_->OpenFile(strTmp);
	else{
		CString err(_T("File "));
		err+=strTmp;
		err+=_T(" does not exist!");
		MessageBox(err);
	}
	return 0;
}

BOOL CMainFrame::PreTranslateMessage(MSG* pMsg)
{
	VARIANT_BOOL bHandled=VARIANT_FALSE;
	pDockMgr_->PreTranslateMessage((int*)pMsg,&bHandled);
	if(bHandled)
		return TRUE;

	if(CMDIFrameWindowImpl<CMainFrame>::PreTranslateMessage(pMsg))
		return TRUE;

	HWND hWnd = MDIGetActive();
	if(hWnd != NULL)
		return (BOOL)::SendMessage(hWnd, WM_FORWARDMSG, 0, (LPARAM)pMsg);

	return FALSE;
}

BOOL CMainFrame::OnIdle()
{
	UIUpdateToolBar();
	return FALSE;
}
#define APP_REG_KEY _T("SoftWare\\OkSoft\\MyCell_MDITest")
void CMainFrame::LoadState()
{
	CRegKey reg;
	DWORD err = reg.Open(HKEY_CURRENT_USER,APP_REG_KEY, KEY_READ);
	WINDOWPLACEMENT wp;
	if(err == ERROR_SUCCESS)
	{
		DWORD dwType = NULL;
		DWORD dwSize = sizeof(wp);
		err = RegQueryValueEx(reg.m_hKey,_T("MainFrameWindowPlacement"), NULL, &dwType,
			(LPBYTE)&wp, &dwSize);
	}
	if(ERROR_SUCCESS==err){
		SetWindowPlacement(&wp);
	}else{
		WINDOWPLACEMENT wp;
		GetWindowPlacement(&wp);
		wp.showCmd=SW_SHOWMAXIMIZED;
		SetWindowPlacement(&wp);
	}
}

void CMainFrame::SaveState()
{
	CRegKey reg;
	DWORD err = reg.Create(HKEY_CURRENT_USER, APP_REG_KEY);
	if(err == ERROR_SUCCESS)
	{
		//DockData* pDockData=(DockData*)&dockWnd_;
		//BYTE arr[sizeof(WINDOWPLACEMENT)+sizeof(DockData)];
		WINDOWPLACEMENT wp;
		wp.length=sizeof(WINDOWPLACEMENT);
		GetWindowPlacement(&wp);
		err = RegSetValueEx(reg.m_hKey, _T("MainFrameWindowPlacement"), NULL, REG_BINARY,
			(LPBYTE)&wp, sizeof(WINDOWPLACEMENT));
	}
}
void CMainFrame::CreateDockWindows()
{
	CComPtr<IDockItem> pDI;
	pDockMgr_->CreateDockItem(L"Examples",&pDI);
	pDI->SetDockSize(300);
	pDI->Create(m_hWnd,rcDefault,WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,0);
	HWND hWnd;
	pDI->GetHWnd(&hWnd);
	wndExample_.Create(hWnd, rcDefault, NULL, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 0/*WS_EX_CLIENTEDGE*/);
	pDockMgr_->DockTo(pDI,NULL,EPT_LEFTMOST);
	pDI->SetChild(wndExample_.m_hWnd);
}
LRESULT CMainFrame::OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{

#ifdef _USE_DLL
#endif
	::ShowWindow(m_hWnd,SW_HIDE);
	// create command bar window
	HWND hWndCmdBar = m_CmdBar.Create(m_hWnd, rcDefault, NULL, ATL_SIMPLE_CMDBAR_PANE_STYLE);
	// attach menu
	m_CmdBar.AttachMenu(GetMenu());
	// load command bar images
	m_CmdBar.LoadImages(IDR_MAINFRAME);
	// remove old menu
	SetMenu(NULL);

	HWND hWndToolBar = CreateSimpleToolBarCtrl(m_hWnd, IDR_MAINFRAME, FALSE, ATL_SIMPLE_TOOLBAR_PANE_STYLE);

	CreateSimpleReBar(ATL_SIMPLE_REBAR_NOBORDER_STYLE);
	AddSimpleReBarBand(hWndCmdBar);
	AddSimpleReBarBand(hWndToolBar, NULL, TRUE);

	CreateSimpleStatusBar();

	CreateMDIClient();
	m_CmdBar.SetMDIClient(m_hWndMDIClient);
	createDockMgr();
	CreateDockWindows();

	UIAddToolBar(hWndToolBar);
	UISetCheck(ID_VIEW_TOOLBAR, 1);
	UISetCheck(ID_VIEW_STATUS_BAR, 1);

	// register object for message filtering and idle updates
	CMessageLoop* pLoop = _Module.GetMessageLoop();
	ATLASSERT(pLoop != NULL);
	pLoop->AddMessageFilter(this);
	pLoop->AddIdleHandler(this);

	//LoadState();
	PostMessage(WM_USER+100);
	return 0;
}

LRESULT CMainFrame::OnDestroy(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled)
{
	SaveState();
		m_CmdBar.AttachMenu(NULL);

	// unregister message filtering and idle updates
	CMessageLoop* pLoop = _Module.GetMessageLoop();
	ATLASSERT(pLoop != NULL);
	pLoop->RemoveMessageFilter(this);
	pLoop->RemoveIdleHandler(this);

	bHandled = FALSE;
	return 1;
}

LRESULT CMainFrame::OnFileExit(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	PostMessage(WM_CLOSE);
	return 0;
}

LRESULT CMainFrame::OnFileNew(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	CChildFrame* pChild = new CChildFrame;
#ifdef _USE_DLL
	CString err;
	if(!pChild->m_view.FinalConstruct(err)){
		delete pChild;
		MessageBox(err,_T("error"),MB_OK|MB_ICONERROR);
		return 0;
	}
	//pChild->m_view.Init(pWorkbook_);
#else
	pChild->m_view.workbook_.put_SelectionMode(ESM_MULTISELECTION);
#endif
	pChild->CreateEx(m_hWndClient);
	// TODO: add code to initialize document

	return 0;
}
void CMainFrame::OpenFile(LPCSTR lpFileName)
{
	CChildFrame* pChild=new CChildFrame;

	TCHAR buf[MAX_PATH]={0};
#ifdef _USE_DLL
	CString err;
	if(!pChild->m_view.FinalConstruct(err)){
		delete pChild;
		MessageBox(err,_T("error"),MB_OK|MB_ICONERROR);
		return ;
	}
	CComBSTR bstrFile;
	pChild->m_view.pWorkbook_->ImportFromXml(CComBSTR(lpFileName),NULL);
	if(bstrFile.Length()>0)
		lstrcpy(buf,CString(bstrFile));
	pChild->CreateEx(m_hWndClient);
	GetShortPathName(buf,buf,MAX_PATH);
	CString strTitle(_T("mycell-"));
	strTitle+=buf;
	pChild->SetWindowText(strTitle);
	DrawMenuBar();
#endif
}
LRESULT CMainFrame::OnFileOpen(WORD,WORD,HWND,BOOL&)
{
	CChildFrame* pChild=new CChildFrame;

	TCHAR buf[MAX_PATH]={0};
#ifdef _USE_DLL
	CString err;
	if(!pChild->m_view.FinalConstruct(err)){
		delete pChild;
		MessageBox(err,_T("error"),MB_OK|MB_ICONERROR);
		return 0;
	}
	CComBSTR bstrFile;
	pChild->m_view.pWorkbook_->ImportFromXml(NULL,&bstrFile);
	if(bstrFile.Length()>0)
		lstrcpy(buf,CString(bstrFile));
	else{
		delete pChild;
		return 0;
	}
	pChild->CreateEx(m_hWndClient);
	GetShortPathName(buf,buf,MAX_PATH);
	CString strTitle(_T("mycell-"));
	strTitle+=buf;
	pChild->SetWindowText(strTitle);
	DrawMenuBar();
#else
	pChild->m_view.workbook_.ImportFromXml(EXT_UNKNOWN,NULL,buf);
	if(buf[0]){
		pChild->m_view.workbook_.put_SelectionMode(ESM_MULTISELECTION);
		GetShortPathName(buf,buf,MAX_PATH);
		CString strTitle(_T("mycell-"));
		strTitle+=buf;
		pChild->CreateEx(m_hWndClient);
		pChild->SetWindowText(strTitle);
		DrawMenuBar();
	}else{
		delete pChild;
		//MessageBox(_T("�޷���ָ�����ļ���"));
	}
#endif
	return 0;
}

LRESULT CMainFrame::OnViewToolBar(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	static BOOL bVisible = TRUE;	// initially visible
	bVisible = !bVisible;
	CReBarCtrl rebar = m_hWndToolBar;
	int nBandIndex = rebar.IdToIndex(ATL_IDW_BAND_FIRST + 1);	// toolbar is 2nd added band
	rebar.ShowBand(nBandIndex, bVisible);
	UISetCheck(ID_VIEW_TOOLBAR, bVisible);
	UpdateLayout();
	return 0;
}

LRESULT CMainFrame::OnViewStatusBar(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	BOOL bVisible = !::IsWindowVisible(m_hWndStatusBar);
	::ShowWindow(m_hWndStatusBar, bVisible ? SW_SHOWNOACTIVATE : SW_HIDE);
	UISetCheck(ID_VIEW_STATUS_BAR, bVisible);
	UpdateLayout();
	return 0;
}

LRESULT CMainFrame::OnAppAbout(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	CAboutDlg dlg;
	dlg.DoModal();
	return 0;
}

LRESULT CMainFrame::OnWindowCascade(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	MDICascade();
	return 0;
}

LRESULT CMainFrame::OnWindowTile(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	MDITile();
	return 0;
}

LRESULT CMainFrame::OnWindowArrangeIcons(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	MDIIconArrange();
	return 0;
}

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