📄 cell_tooltip.shtml.htm
字号:
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Author" CONTENT="Zafir Anjum">
<META NAME="GENERATOR" CONTENT="Mozilla/4.0 [en] (WinNT; I) [Netscape]">
<TITLE>Tooltip for individual cells</TITLE>
</HEAD>
<body background="../fancyhome/back.gif" tppabs="http://www.codeguru.com/fancyhome/back.gif" bgcolor="#FFFFFF" link="#B50029" vlink="#8E2323" alink="#FF0000" bgproperties="fixed">
<table WIDTH="100%">
<tr WIDTH="100%">
<td><td>
</tr>
</table>
<CENTER>
<H3>
<FONT COLOR="#AOAO99">Tooltip for individual cells</FONT></H3></CENTER>
<HR WIDTH="100%">Tooltips are very useful when column widths are limited
due to limited screen size. They could also be used to expand the text
of abreviated columns. For this task, we will use the tooltip support provided
by MFC. The code below displays the text of the cell in a tooltip, but
it can be easily modified to display something other that what抯 already
displayed in the cell.
<P>Adding tooltips for individual cells is quite easy. However, the documentation
was not very helpful and there are few details that you should be aware
of. The list view control on Windows 95 and Windows NT 4.0 have two very
significant differences. First, the list view control ( and the tooltip
control ) is an ANSI control on Windows 95. What this means is that on
Windows 95, the control notifications and messages are the ANSI versions.
Do not rely on the project setting to automatically translate the plain,
undecorated form of a message constant (e.i. without the A or W suffix)
to the correct message value. For example, if you were developing a UNICODE
application, then TTN_NEEDTEXT would translate to TTN_NEEDTEXTW, but on
Windows 95 the actual message would be TTN_NEEDTEXTA. This also applies
to structures and strings. On Window 95, all strings passed to a control
should be ANSI strings. On NT 4.0, the controls are UNICODE controls.
<P>Second, on NT 4.0, the list view control automatically creates a tooltip
control. This built in tooltip control automatically sends the TTN_NEEDTEXTW
notification whenever the mouse is over the list view control and does
not move for a certian duration. The code below ignores notification from
this built in tooltip control.
<BR>
<P>Override PreSubclassWindow() and after calling the base class version
call EnableToolTips(TRUE). EnableToolTips() is a member function of the
CWnd class and thus available to all windows and controls.
<PRE><TT><FONT COLOR="#990000">void CMyListCtrl::PreSubclassWindow()
{
CListCtrl::PreSubclassWindow();
// Add initialization code
EnableToolTips(TRUE);
}</FONT></TT></PRE>
Override the OnToolHitTest() function. OnToolHitTest() is a virtual function
defined in CWnd class and is called by the framework to determine whether
a point is over any of the tools. A tool can be a control window or even
a rectangular area within a window. For our purpose, we would want the
area of each cell to be treated as a tool.
<P>The documentation of the OnToolHitTest() implies that you should return
1 if a tool is found and -1 if no tool was found. The truth, however, is
that, the framework uses the return value to determine if the tool has
changed. The framework updates the tooltip only when the tool changes,
therefore, OnToolHitTest() should return a different value whenever the
cell at the given point changes.
<PRE><TT><FONT COLOR="#990000">int CMyListCtrl::OnToolHitTest(CPoint point, TOOLINFO * pTI) const
{
int row, col;
RECT cellrect;
row = CellRectFromPoint(point, &cellrect, &col );
if ( row == -1 )
return -1;
pTI->hwnd = m_hWnd;
pTI->uId = (UINT)((row<<10)+(col&0x3ff)+1);
pTI->lpszText = LPSTR_TEXTCALLBACK;
pTI->rect = cellrect;
return pTI->uId;
}</FONT></TT></PRE>
The function first calls the CellRectFromPoint() function to determine
the row, column and the bounding rectangle of the cell. We cover the CellRectFromPoint()
below. The function then sets up the TOOLINFO structure. The 憉Id
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -