打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Drawing horizontal and vertical gridlines
http://www.codeguru.com/Cpp/controls/listview/gridlines/article.php/c963/


To draw horizontal and vertical gridlines we essentiallyuse the same method used for the vertical gridline or column border usedin the previous section. After drawing the vertical lines, we use GetItemRect()to get the item height and then we draw the horizontal grid lines basedon this height. One implication of using GetItemRect() is that it failswhen 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 notethat the next version of the list view control will support the LVS_EX_GRIDLINESstyle and should make our code redundant.


void CMyListCtrl::OnPaint()
{
// First let the control do its default drawing.
const MSG *msg = GetCurrentMessage();
DefWindowProc( msg->message, msg->wParam, msg->lParam );

// Draw the lines only for LVS_REPORT mode
if( (GetStyle() & LVS_TYPEMASK) == LVS_REPORT )
{
// Get the number of columns
CClientDC dc(this );
CHeaderCtrl* pHeader = (CHeaderCtrl*)GetDlgItem(0);
int nColumnCount = pHeader->GetItemCount();

// The bottom of the header corresponds to the top of the line
RECT rect;
pHeader->GetClientRect( &rect );
int top = rect.bottom;

// Now get the client rect so we know the line length and
// when to stop
GetClientRect( &rect );

// The border of the column is offset by the horz scroll
int borderx = 0 - GetScrollPos( SB_HORZ );
for( int i = 0; i < nColumnCount; i++ )
{
// Get the next border
borderx += GetColumnWidth( i );

// if next border is outside client area, break out
if( borderx >= rect.right ) break;

// Draw the line.
dc.MoveTo( borderx-1, top);
dc.LineTo( borderx-1, rect.bottom );
}

// Draw the horizontal grid lines

// First get the height
if( !GetItemRect( 0, &rect, LVIR_BOUNDS ))
return;

int height = rect.bottom - rect.top;

GetClientRect( &rect );
int width = rect.right;

for( i = 1; i <= GetCountPerPage(); i++ )
{
dc.MoveTo( 0, top + height*i);
dc.LineTo( width, top + height*i );
}
}

// Do not call CListCtrl::OnPaint() for painting messages
}

The vertical grid line is actually drawn one pixel left of the column border. Thisaligns it better with the column header. It also introduces a bug. Whenyou increase a column width, the column area below the last visible itemis not updated, thus leaving traces of the previous line. There are twoapproaches you can take to resolve this. First, draw the line exactly onthe column border (e.i. do not subtract 1 from borderx). The second approachis to handle the HDN_TRACK notification from the header controland 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 itat http://www.voicenet.com/~pgerhart/_shware.html


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
用OpenCV和OCR识别图片中的表格数据 !OpenCV简直太强大了!
pdfplumber说明文档翻译
\vspace vs. \vskip
一个python的2048简单实现
18、简单的UI布局。
Unity零基础到进阶 ☀️| UGUI布局 之Content Size Fitter组件介绍 和 使用示例
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服