MFC自绘列表网格线的问题

baidu_28543119 2015-09-23 08:19:35
我是将列表设置为没有默认网格线的属性,然后在DrawItem中对网格线进行绘制的,但是当我点击竖向滚动条的向下或者向上按钮时,列表网格线的绘制发生错位,效果如下

但是当我用滚轮滚动或者拖拽滚动条时,都不会出现这种情况,代码如下:
void CMyListCtrl::DrawListGridLine(CDC* pDC)
{
const MSG *pMsg = GetCurrentMessage();
DefWindowProc( pMsg->message, pMsg->wParam, pMsg->lParam );
//Draw the lines only for LVS_REPORT mode
if( (GetStyle() & LVS_TYPEMASK) == LVS_REPORT )
{
// Get the number of columns
CHeaderCtrl* pHeader = GetHeaderCtrl();
int nColumnCount = pHeader->GetItemCount();
// The bottom of the header corresponds to the top of the line
CRect rectHeader;
pHeader->GetClientRect( &rectHeader );
int nTop = rectHeader.bottom;
// Now get the client rect so we know the line length and
// when to stop
CRect rectClient;
GetClientRect( &rectClient );
CPen newPen, *oldPen;
newPen.CreatePen(PS_SOLID, THIN_PEN_SIZE, m_clrLine);
oldPen = pDC->SelectObject(&newPen);
// 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 >= rectClient.right ) break;
// Draw the Vertical line.
pDC->MoveTo( borderx , nTop);
pDC->LineTo( borderx , rectClient.bottom );
}
// Draw the horizontal grid lines
// First get the height
CRect rectItem;
if( !GetItemRect( ZeroIndex, &rectItem, LVIR_BOUNDS ))
return;
int nHeight = rectItem.bottom - rectItem.top;
GetClientRect(&rectClient);
int nLength = rectClient.right;
SCROLLINFO sSCrollInfo;
sSCrollInfo.cbSize = sizeof(SCROLLINFO);
GetScrollInfo(SB_VERT,&sSCrollInfo);
for( int i = 0; i <= GetCountPerPage(); i++ )
{
int k = nTop + nHeight * i;
pDC->MoveTo( 0, nTop + nHeight*i);
pDC->LineTo(nLength, nTop + nHeight*i);
}
//删除画笔
pDC->SelectObject(oldPen);
newPen.DeleteObject();
}
}
这是什么原因呢
...全文
417 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
信阳毛尖 2015-09-23
  • 打赏
  • 举报
回复
代码看着没啥问题,不调试是没谱儿的.... 你的SB_TOP 和 SB_BOTTOM怎么处理?
笨笨仔 2015-09-23
  • 打赏
  • 举报
回复
跟踪一下,自绘中可能还要处理滚动条事件

15,980

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧