mfc ListControl 重绘网格线

来源:互联网 发布:物理名师课堂软件 编辑:程序博客网 时间:2024/06/06 08:47

.h文件

 //网格线设置 [yal 2013.10.08]
 int        m_nGridStyle;      //风格 PS_DASH,PS_SOLID,。。。
 COLORREF   m_GridColor;       //颜色
 int        m_nGridWeigth;     //粗度1,2,...

 void  setGridStyle(int nStyle);
 void  setGridWeight(int nWeight);
 void  setGridColor(COLORREF cr);

.cpp文件

CListCtrlCl::CListCtrlCl()
:m_nGridStyle(PS_SOLID)
,m_nGridWeigth(1)
,m_GridColor(RGB(255,255,255))
{
}
void CListCtrlCl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{

 // TODO:  添加您的代码以绘制指定项

 // 重绘网格线 [7/12/2013 dell]
 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 );
  //yal 2013.10.08画有颜色的线
  CPen pen(m_nGridStyle,m_nGridWeigth,m_GridColor);
  CPen* oldpen=dc.SelectObject(&pen);

  CHeaderCtrl* pHeader = (CHeaderCtrl*)GetDlgItem(0);
  int nColumnCount = pHeader->GetItemCount();

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

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

  if( !GetItemRect( 1, &rectCol, LVIR_BOUNDS ))
   return;

  int height1 = rectCol.bottom - rectCol.top;

  // 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/*top*/);
   dc.LineTo( borderx-1, height1+top+lpDrawItemStruct->itemID*height1/*nColumnCount*height1 + top*/);
  }

  // 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(unsigned int i = 1; i <= lpDrawItemStruct->itemID+1; i++ )
  {
   dc.MoveTo( 0, top + height*i);
   dc.LineTo( width, top + height*i );
  }

   dc.SelectObject(oldpen);
 }
}

原创粉丝点击