读书人

关于listctrl 网格线的有关问题

发布时间: 2012-06-30 17:20:12 作者: rapoo

关于listctrl 网格线的问题
我用listctrl控件的时候设置属性LVS_EX_GRIDLINES,发现网格线是灰色的,在我的界面上很难看。

后来我在网上找到了一段代码

void CListCtrlCl::OnPaint()
{
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 );
}

}
}

这段代码可以模拟出黑色的线,但是当我向右拖动列时,在没有内容的地方网格线会出现重叠现象,也就是说好像我在拖动列宽的时候没有刷新。

如果我把DefWindowProc( msg->message, msg->wParam, msg->lParam );这句话隐掉的话,以上现象就没了,但是内容不见了?

请问怎么回事?或者还有其他方法改变列表控件的网格线的颜色吗?

[解决办法]
http://www.pudn.com/downloads80/sourcecode/windows/control/listview/detail310667.html
刚刚忘记贴了
[解决办法]
加 OnNotify 虚函数

C/C++ code
BOOL CListCtrlCl::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult) {    // TODO: Add your specialized code here and/or call the base class    switch(   ((NMHDR*)lParam)->code   )       {                 case HDN_ITEMCHANGINGW:        Invalidate();        break;    }       return CListCtrl::OnNotify(wParam, lParam, pResult);}
[解决办法]
为了保险,HDN_ITEMCHANGINGA工程为 ASIC ,HDN_ITEMCHANGINGW工程为UNICODE

C/C++ code
BOOL CListCtrlCl::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult) {    // TODO: Add your specialized code here and/or call the base class    switch(   ((NMHDR*)lParam)->code   )       {          case HDN_ITEMCHANGINGA:         case HDN_ITEMCHANGINGW:        Invalidate();        break;    }       return CListCtrl::OnNotify(wParam, lParam, pResult);} 

读书人网 >VC/MFC

热点推荐