ツールチップを有効にする |
int CHogeListCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CListCtrl::OnCreate(lpCreateStruct) == -1) return -1; //ツールチップを有効にします。 EnableToolTips(TRUE); return 0; } |
メッセージのマップ(.h) |
//{{AFX_MSG(CHogeListCtrl) ・・・ afx_msg BOOL OnToolTipText( UINT id, NMHDR * pNMHDR, LRESULT * pResult ); //}}AFX_MSG DECLARE_MESSAGE_MAP() |
メッセージのマップ(.cpp) |
BEGIN_MESSAGE_MAP(CHogeListCtrl, CListCtrl) //{{AFX_MSG_MAP(CTreeListCtrl) ・・・ //}}AFX_MSG_MAP //ON_NOTIFY_EX( TTN_NEEDTEXT, 0, OnToolTipText ) ON_NOTIFY_EX(TTN_NEEDTEXT, 0, OnToolTipText) ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTA, 0, 0xFFFF, OnToolTipText) ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTW, 0, 0xFFFF, OnToolTipText) END_MESSAGE_MAP() |
OnToolTipTextの実装 |
BOOL CHogeListCtrl::OnToolTipText( UINT id, NMHDR * pNMHDR, LRESULT * pResult ){ TOOLTIPTEXTA* pTTTA = (TOOLTIPTEXTA*)pNMHDR; TOOLTIPTEXTW* pTTTW = (TOOLTIPTEXTW*)pNMHDR; CString strTipText; UINT nID = pNMHDR->idFrom; if( nID == 0 ) // Notification in NT from automatically return FALSE; // created tooltip int row = ((nID-1) %gt;> 10) & 0x3fffff ; int col = (nID-1) & 0x3ff; strTipText = GetItemText( row, col ); #ifndef _UNICODE if (pNMHDR->code == TTN_NEEDTEXTA) lstrcpyn(pTTTA->szText, strTipText, 80); else _mbstowcsz(pTTTW->szText, strTipText, 80); #else if (pNMHDR->code == TTN_NEEDTEXTA) _wcstombsz(pTTTA->szText, strTipText, 80); else lstrcpyn(pTTTW->szText, strTipText, 80); #endif *pResult = 0; return TRUE; // message was handled } |
OnToolHitTest()関数の実装(.h) |
// オーバーライド // ClassWizard は仮想関数のオーバーライドを生成します。 //{{AFX_VIRTUAL(CTreeListCtrl) ・・・ //}}AFX_VIRTUAL virtual int OnToolHitTest( CPoint point, TOOLINFO* pTI ) const; |
OnToolHitTest()関数の実装(.cpp) |
int CHogeListCtrl::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; } |
CellRectFromPoint()関数の実装 |
int CHogeListCtrl::CellRectFromPoint(CPoint & point, RECT * cellrect, int * col) const{ int colnum; // LVS_REPORTスタイルでない場合は無効にする if( (GetWindowLong(m_hWnd, GWL_STYLE) & LVS_TYPEMASK) != LVS_REPORT ) return -1; // 表示されているトップとボトムのアイテムの数を取得する int row = GetTopIndex(); int bottom = row + GetCountPerPage(); if( bottom > GetItemCount() ) bottom = GetItemCount(); //カラムの数を取得する CHeaderCtrl* pHeader = (CHeaderCtrl*)GetDlgItem(0); int nColumnCount = pHeader->GetItemCount(); for( ;row <=bottom;row++){ CRect rect; GetItemRect( row, &rect, LVIR_BOUNDS ); if( rect.PtInRect(point) ){ // Now find the column for( colnum = 0; colnum < nColumnCount; colnum++ ){ int colwidth = GetColumnWidth(colnum); if( point.x >= rect.left && point.x <= (rect.left + colwidth ) ){ RECT rectClient; GetClientRect( &rectClient ); if( col ) *col = colnum; rect.right = rect.left + colwidth; // Make sure that the right extent does not exceed // the client area if( rect.right > rectClient.right ){ rect.right = rectClient.right; } *cellrect = rect; return row; } rect.left += colwidth; } } } return -1; } |