Click here to Skip to main content
15,881,172 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.8K   4.5K   81  
A fast and lightweight cell control for displaying tabular data. The cell is a custom control derived from ATL::CWindow.
/* -------------------------------------------------------------------------
//MyCell Library - MyCell version 1.0
//
// This file is a part of the MyCell Library.
// The use and distribution terms for this software are covered by the
// Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
// which can be found in the file CPL.txt at this distribution. By using
// this software in any fashion, you are agreeing to be bound by the terms
// of this license. You must not remove this notice, or any other, from
// this software.
//
// Creator: yanxueming
// Email: xueming.yan@gmail.com
// -----------------------------------------------------------------------*/
#include "stdafx.h"
#include <atltypes.h>
//#include "../include/ToolTipImp.h"
//#include "../include/Wokd.h"
#include "../include/Workbook.h"
//using mycell::global::g_tooltip_text_buf;
namespace mycell{
	/*
	ToolTipImpl::ToolTipImpl(Worksheet* pSheet):pSheet_(pSheet),tip_(*pSheet->GetTooltipCtrl())
	{}

	void ToolTipImpl::Create(HWND hWndParent)
	{
	DWORD dwStyle = WS_POPUP;// | TTS_NOPREFIX | TTS_ALWAYSTIP;
	tip_.Create(hWndParent, CWindow::rcDefault, NULL, dwStyle);

	memset(&toolInfo_, 0, sizeof(TOOLINFO));
	toolInfo_.cbSize = sizeof(TOOLINFO);
	toolInfo_.uFlags = TTF_IDISHWND | TTF_TRACK | TTF_ABSOLUTE;
	toolInfo_.uId=(UINT)tip_.m_hWnd;
	toolInfo_.hwnd=hWndParent;
	toolInfo_.hinst = _Module.GetResourceInstance();
	tip_.AddTool(&toolInfo_);
	tip_.TrackActivate(&toolInfo_,FALSE);
	tip_.SetTipBkColor(RGB(0xcc,0xcc,0xff));
	//tip_.Activate(TRUE);
	//tip_.SetMaxTipWidth(20);
	}
	*/
	//LRESULT ToolTipImpl::OnKillFocus(UINT,WPARAM,LPARAM,BOOL& bHandled)
	//{
	//	bHandled=FALSE;
	//	//if(tip_.IsWindow())
	//	{
	//		tip_.TrackActivate(/*&toolInfo_,*/FALSE);
	//		tip_.cell.set(-100,-100);
	//	}
	//	return 0;
	//}
	//LRESULT ToolTipImpl::OnDestroy(UINT,WPARAM,LPARAM,BOOL& bHandled)
	//{
	//	bHandled=FALSE;
	//	tip_.DestroyWindow();
	//	return 0;
	//}
	//LRESULT ToolTipImpl::OnMouseMove(UINT,WPARAM,LPARAM lParam,BOOL& nHandled)
	//{
	//	return 0;
	//}
	//TCHAR g_tooltip_text_buf[MAX_CELLTEXT];
	void Worksheet::Tooltip_ChangeCellToolTip(const CELLHITTESTINFO& chi)
	{
		//nHandled=FALSE;
		pair<TOOLINFO,CToolTipCtrl>& tooltip=tooltip_;
		//CCell& tooltipCell_=tooltipCell_;
		//LPTSTR tooltipText_=pWorkbook_->Tooltip_GetTooltipText();
		bool bHideTooltip=false;
		POINT pt=chi.pt;//{GET_X_LPARAM(lParam),GET_Y_LPARAM(lParam)};
		CToolTipCtrl& tip=tooltip.second;
		TOOLINFO& toolInfo=tooltip.first;
		CRect rcCli;
		GetClientRect(rcCli);
		if(!rcCli.PtInRect(pt)){
			tip.TrackActivate(&toolInfo,FALSE);
			tooltipCell_.set(-100,-100);
			if(GetCapture()==m_hWnd)
				ReleaseCapture();
			return;
		}
		//ECellHitTestConstants hct;
		const CCellRange* pMerge=GetMergeCells(chi.cell);
		const CCell cid=pMerge?pMerge->TopLeft():chi.cell;
		//g_tooltip_cell=chi.cell;
		if(cid.row>=0 && cid.col>=0 && cid.row<get_RowHeader().get_rows() && cid.col<get_ColHeader().get_cols())
		{
			if(tip.IsWindowVisible() && cid==tooltipCell_)
				return ;

			GetCellText(cid,Workbook::tooltipText_,EGCVD_TOOLTIP);

			CClientDC dc(m_hWnd);
			const StyleDesc& style=Style_GetCellStyle(cid);
			CFont font(style.Font_GetHFont(dc));
			HFONT hOldFont=dc.SelectFont(font);
			CRect rc;
			if(pMerge){
				rc=get_RangeRect(*pMerge);
			}else
				rc=raw_GetCellRect(cid);
			CRect rcCell;
			dc.DrawText(Workbook::tooltipText_,lstrlen(Workbook::tooltipText_),&rcCell,DT_CALCRECT);

			if(rcCell.Width()>rc.Width()){			
				toolInfo.lpszText=Workbook::tooltipText_;
				tip.UpdateTipText(&toolInfo);
				ClientToScreen(&pt);
				pt.x+=10;
				pt.y+=20;
				int const cxScreen=GetSystemMetrics(SM_CXFULLSCREEN);
				int const cyScreen=GetSystemMetrics(SM_CYFULLSCREEN);
				if(pt.x+rcCell.Width()>cxScreen){
					pt.x=cxScreen-rcCell.Width()-10;
				}
				if(pt.y+rcCell.Height()>cyScreen){
					pt.y=cyScreen-rcCell.Height()-10;
				}

				tip.TrackPosition(pt.x,pt.y);
				if(!tip.GetCurrentTool(&toolInfo)){
					tip.TrackActivate(&toolInfo,TRUE);
#ifdef _DEBUG
					//CString msg;
					//msg.Format(_T(""::GetTickCount();
					AtlTrace(_T("\nTrackActivate-%d"),GetTickCount());
#endif
				}
				tooltipCell_=cid;
			}else{
				bHideTooltip=true;
			}
			dc.SelectFont(hOldFont);
		}else{
			bHideTooltip=true;
		}
		if(bHideTooltip){
			tip.TrackActivate(&toolInfo,FALSE);
			tooltipCell_.set(-100,-100);
			if(GetCapture()==m_hWnd)
				ReleaseCapture();
		}else{
			tooltipCell_=cid;
			SetCapture();
		}
	}
}//namespace mycell

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