Drawing horizontal and vertical gridlines for CListCtrl

Rating: none

 Zafir Anjum (view profile)August 6, 1998

To draw horizontal and vertical gridlines we essentially use the same method used for the vertical gridline or column border used in the previous section. After drawing the vertical lines, we use GetItemRect() to get the item height and then we draw the horizontal grid lines based on this height. One implication of using GetItemRect() is that it fails when the list does not have any items in it and no horizontal line is drawn. Here is the complete code of the overridden OnPaint() function. Also note that the next version of the list view control will support the LVS_EX_GRIDLINES style and should make our code redundant.
(continued)

 CMyListCtrl::OnPaint() {   MSG *msg = GetCurrentMessage(); DefWindowProc( msg->message, msg->wParam, msg->lParam );  ( (GetStyle() & LVS_TYPEMASK) == LVS_REPORT ) {  CClientDC dc( ); CHeaderCtrl* pHeader = (CHeaderCtrl*)GetDlgItem(0);  nColumnCount = pHeader->GetItemCount();  RECT rect; pHeader->GetClientRect( &rect );  top = rect.bottom;   GetClientRect( &rect );   borderx = 0 - GetScrollPos( SB_HORZ ); (  i = 0; i < nColumnCount; i++ ) {  borderx += GetColumnWidth( i );  ( borderx >= rect.right ) ;  dc.MoveTo( borderx-1, top); dc.LineTo( borderx-1, rect.bottom ); }   ( !GetItemRect( 0, &rect, LVIR_BOUNDS )) ;  height = rect.bottom - rect.top; GetClientRect( &rect );  width = rect.right; ( i = 1; i <= GetCountPerPage(); i++ ) { dc.MoveTo( 0, top + height*i); dc.LineTo( width, top + height*i ); } }  }

The vertical grid line is actually drawn one pixel left of the column border. This aligns it better with the column header. It also introduces a bug. When you increase a column width, the column area below the last visible item is not updated, thus leaving traces of the previous line. There are two approaches you can take to resolve this. First, draw the line exactly on the column border (e.i. do not subtract 1 from borderx). The second approach is to handle the HDN_TRACK notification from the header control and invalidate the exposed area so that it gets redrawn.

BTW, Paul Gerhart has also implemented this using an owner-drawn CListCtrl. He has made the source code available with a sample app. You can find it at http://www.voicenet.com/~pgerhart/_shware.html

• 本文已收录于以下专栏：

举报原因： 您举报文章：Drawing horizontal and vertical gridlines for CListCtrl 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)