MFC CGridCtrl 表格控件,如何去除黑色背景,让表格占满

zlxi 2020-02-12 12:01:48
MFC CGridCtrl 表格控件,如何去除黑色背景,让表格占满


但如果把水平和垂直滚动条去掉,双击就没有黑色背景色。
pGridLog->ShowScrollBar(SB_BOTH, FALSE);//关闭水平滚动条




void CLogArea::DrawTable(CAibotFrameDlg* pdialog)
{

int _1scale = CAibotFrameDlg::GetScaleFactor()->Get01WPix();
int _5scale = CAibotFrameDlg::GetScaleFactor()->Get10WPix() * 5;

int _01hscale = CAibotFrameDlg::GetScaleFactor()->Get01HPix();
int _10hscale = CAibotFrameDlg::GetScaleFactor()->Get10HPix();
int _30hscale = _10hscale * 3;

// m_AloneListRect 绘制表格的矩阵大小
m_AloneListRect.TopLeft() = CPoint(2, m_Rect.top + _30hscale);
m_AloneListRect.BottomRight() = CPoint(m_Rect.right-2, m_Rect.bottom);

//获取表格控件
CGridCtrl *pGridLog = (CGridCtrl *)pdialog->GetDlgItem(IDC_CUSTOM_TABLOG);
pGridLog->ModifyStyleEx(WS_EX_CLIENTEDGE, NULL, SWP_DRAWFRAME); //关闭下陷效果
pGridLog->SetEditable(true);
pGridLog->SetTextBkColor(RGB(248, 255, 255));

pGridLog->SetGridBkColor(RGB(0, 0, 0));
pGridLog->SetGridLineColor(RGB(0, 0, 0));

int rowhight = abs((m_lfHeight - 1) - _10hscale); // 设置行高
int rows = m_AloneListRect.Height() / rowhight; // 计算矩阵中能存放的行数
m_AloneListRect.bottom = m_AloneListRect.top + rows *rowhight; //重新设置矩阵

//将表格控件移动到指定矩阵区域
pGridLog->MoveWindow(m_AloneListRect);




CRect rect; //获取客户区大小
pGridLog->GetClientRect(rect);
int cols = IDS_STRING_LG_RMARK + 1 - IDS_STRING_LG_NO; //获取列数 12列
pGridLog->SetRowCount(rows+1); //设置行数
pGridLog->SetColumnCount(cols); //设置列数

pGridLog->SetFixedRowCount(1); //表头为第一行
//pGridLog->SetFixedColumnCount(1); //表头为一列

int width = rect.Width() / cols; //前面11列列宽
int total = 0;
pGridLog->SetRowHeight(0, rowhight); //设置标题行高
//绘制表格头
for (int icol = 0, itext = IDS_STRING_LG_NO; icol < cols; ++icol,++itext)
{
GV_ITEM Item;
Item.mask = GVIF_TEXT | GVIF_FORMAT;
Item.row = 0;
Item.col = icol;
if (icol == pGridLog->GetColumnCount() - 1)
{
int len = rect.Width() - (cols-1) * width;
pGridLog->SetColumnWidth(icol, len); //设置各列宽
total += len;
}
else
{
pGridLog->SetColumnWidth(icol, width); //设置各列宽
total += width;
}
Item.nFormat = DT_CENTER | DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS;

CString str;
str.LoadString(itext);
Item.strText.Format(str, icol);
pGridLog->SetItem(&Item);
}

pGridLog->ShowWindow(SW_SHOW); //显示表格

pGridLog->ShowScrollBar(SB_HORZ, FALSE);//关闭水平滚动条

}




出现问题如下图


...全文
673 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
zlxi 2020-02-17
  • 打赏
  • 举报
回复
引用 13 楼 zgl7903 的回复:
最大的可能性是没有考虑滚动条

#define GRIDCONTROL_GRIDLINEEXTENDTO_NOUSER //显示格扩展到非客户区

void CGridCtrl::OnDraw(CDC* pDC)
{
  if (!m_bAllowDraw)
    return;

  CRect clipRect;
  if (pDC->GetClipBox(&clipRect) == ERROR)
    return;

  EraseBkgnd(pDC);            // OnEraseBkgnd does nothing, so erase bkgnd here.
  // This necessary since we may be using a Memory DC.

#ifdef _DEBUG
  LARGE_INTEGER iStartCount;
  QueryPerformanceCounter(&iStartCount);
#endif

  CRect rect;
  int row, col;
  CGridCellBase* pCell;

  int nFixedRowHeight = GetFixedRowHeight();
  int nFixedColWidth  = GetFixedColumnWidth();

  CCellID idTopLeft = GetTopleftNonFixedCell();
  int minVisibleRow = idTopLeft.row,
    minVisibleCol = idTopLeft.col;

  CRect VisRect;
  CCellRange VisCellRange = GetVisibleNonFixedCellRange(VisRect);
  int maxVisibleRow = VisCellRange.GetMaxRow(),
    maxVisibleCol = VisCellRange.GetMaxCol();

  if (GetVirtualMode())
    SendCacheHintToParent(VisCellRange);

#ifdef GRIDCONTROL_GRIDLINEEXTENDTO_NOUSER
  do
  {
    int iColMax = GetColumnCount();
    if(iColMax <=0 ) break;

    //获取客户区大小
    CRect rcClient;
    GetClientRect(&rcClient);
    
    int icxUsed = 0;
    for(int iCol=0; iCol<iColMax-1; iCol++)
    {
      icxUsed += GetColumnWidth(iCol);
    }

    int iLastCX = rcClient.Width() - icxUsed;
    if(GetColumnWidth(iColMax-1) != iLastCX)
    {
      SetColumnWidth(iColMax-1, iLastCX);
      Invalidate();
    }
  }while(0);
#endif //GRIDCONTROL_GRIDLINEEXTENDTO_NOUSER

  // draw top-left cells 0..m_nFixedRows-1, 0..m_nFixedCols-1
  rect.bottom = -1;
  for (row = 0; row < m_nFixedRows; row++)
  {
    if (GetRowHeight(row) <= 0) continue;

    rect.top = rect.bottom+1;
    rect.bottom = rect.top + GetRowHeight(row)-1;
    rect.right = -1;

    for (col = 0; col < m_nFixedCols; col++)
    {
      if (GetColumnWidth(col) <= 0) continue;

      rect.left = rect.right+1;
      rect.right = rect.left + GetColumnWidth(col)-1;

      pCell = GetCell(row, col);
      if (pCell)
      {
        pCell->SetCoords(row,col);
        pCell->Draw(pDC, row, col, rect, FALSE);
      }
    }
  }

  // draw fixed column cells:  m_nFixedRows..n, 0..m_nFixedCols-1
  rect.bottom = nFixedRowHeight-1;
  for (row = minVisibleRow; row <= maxVisibleRow; row++)
  {
    if (GetRowHeight(row) <= 0) continue;

    rect.top = rect.bottom+1;
    rect.bottom = rect.top + GetRowHeight(row)-1;

    // rect.bottom = bottom pixel of previous row
    if (rect.top > clipRect.bottom)
      break;                // Gone past cliprect
    if (rect.bottom < clipRect.top)
      continue;             // Reached cliprect yet?

    rect.right = -1;
    for (col = 0; col < m_nFixedCols; col++)
    {
      if (GetColumnWidth(col) <= 0) continue;

      rect.left = rect.right+1;
      rect.right = rect.left + GetColumnWidth(col)-1;

      if (rect.left > clipRect.right)
        break;            // gone past cliprect
      if (rect.right < clipRect.left)
        continue;         // Reached cliprect yet?

      pCell = GetCell(row, col);
      if (pCell)
      {
        pCell->SetCoords(row,col);
        pCell->Draw(pDC, row, col, rect, FALSE);
      }
    }
  }

  // draw fixed row cells  0..m_nFixedRows, m_nFixedCols..n
  rect.bottom = -1;
  for (row = 0; row < m_nFixedRows; row++)
  {
    if (GetRowHeight(row) <= 0) continue;

    rect.top = rect.bottom+1;
    rect.bottom = rect.top + GetRowHeight(row)-1;

    // rect.bottom = bottom pixel of previous row
    if (rect.top > clipRect.bottom)
      break;                // Gone past cliprect
    if (rect.bottom < clipRect.top)
      continue;             // Reached cliprect yet?

    rect.right = nFixedColWidth-1;
    for (col = minVisibleCol; col <= maxVisibleCol; col++)
    {
      if (GetColumnWidth(col) <= 0) continue;

      rect.left = rect.right+1;
      rect.right = rect.left + GetColumnWidth(col)-1;

      if (rect.left > clipRect.right)
        break;        // gone past cliprect
      if (rect.right < clipRect.left)
        continue;     // Reached cliprect yet?

      pCell = GetCell(row, col);
      if (pCell)
      {
        pCell->SetCoords(row,col);
        pCell->Draw(pDC, row, col, rect, FALSE);
      }
    }

  }

  // draw rest of non-fixed cells
  rect.bottom = nFixedRowHeight-1;
  for (row = minVisibleRow; row <= maxVisibleRow; row++)
  {
    if (GetRowHeight(row) <= 0) continue;

    rect.top = rect.bottom+1;
    rect.bottom = rect.top + GetRowHeight(row)-1;

    // rect.bottom = bottom pixel of previous row
    if (rect.top > clipRect.bottom)
      break;                // Gone past cliprect
    if (rect.bottom < clipRect.top)
      continue;             // Reached cliprect yet?

    rect.right = nFixedColWidth-1;
    for (col = minVisibleCol; col <= maxVisibleCol; col++)
    {
      if (GetColumnWidth(col) <= 0) continue;

      rect.left = rect.right+1;
      rect.right = rect.left + GetColumnWidth(col)-1;

      if (rect.left > clipRect.right)
        break;        // gone past cliprect
      if (rect.right < clipRect.left)
        continue;     // Reached cliprect yet?

      pCell = GetCell(row, col);
      // TRACE(_T("Cell %d,%d type: %s\n"), row, col, pCell->GetRuntimeClass()->m_lpszClassName);
      if (pCell)
      {
        pCell->SetCoords(row,col);
        pCell->Draw(pDC, row, col, rect, FALSE);
      }
    }
  }


  CPen pen;
  pen.CreatePen(PS_SOLID, 0, m_crGridLineColour);
  pDC->SelectObject(&pen);

  // draw vertical lines (drawn at ends of cells)
  if (m_nGridLines == GVL_BOTH || m_nGridLines == GVL_VERT)
  {
    int x = nFixedColWidth;
    for (col = minVisibleCol; col <= maxVisibleCol; col++)
    {
      if (GetColumnWidth(col) <= 0) continue;

      x += GetColumnWidth(col);
      pDC->MoveTo(x-1, nFixedRowHeight);
      pDC->LineTo(x-1, VisRect.bottom);
    }
  }

  // draw horizontal lines (drawn at bottom of each cell)
  if (m_nGridLines == GVL_BOTH || m_nGridLines == GVL_HORZ)
  {
    int y = nFixedRowHeight;
    for (row = minVisibleRow; row <= maxVisibleRow; row++)
    {
      if (GetRowHeight(row) <= 0) continue;

      y += GetRowHeight(row);
      pDC->MoveTo(nFixedColWidth, y-1);
      pDC->LineTo(VisRect.right,  y-1);
    }
  }

  pDC->SelectStockObject(NULL_PEN);

  // Let parent know it can discard it's data if it needs to.
  if (GetVirtualMode())
    SendCacheHintToParent(CCellRange(-1,-1,-1,-1));

#ifdef _DEBUG
  LARGE_INTEGER iEndCount;
  QueryPerformanceCounter(&iEndCount);
  TRACE1("Draw counter ticks: %d\n", iEndCount.LowPart-iStartCount.LowPart);
#endif

}
测试发现,一列不能拉太长,拉太长,超出Grid区域长度,就会出现内存问题,调试很久没有解决。
zlxi 2020-02-13
  • 打赏
  • 举报
回复
1.为什么双击最后一列单元格,会多出这么这部分?看源码,看了好久都没找对地方。
引用 10 楼 zgl7903 的回复:
那你的占满是是什么概念,拉伸最后一列?
zlxi 2020-02-13
  • 打赏
  • 举报
回复
引用 10 楼 zgl7903 的回复:
那你的占满是是什么概念,拉伸最后一列?
可以这么说,最后一列拉伸。 不出现多余部分,像下图,去掉或者合并掉这部分
zgl7903 2020-02-13
  • 打赏
  • 举报
回复
那你的占满是是什么概念,拉伸最后一列?
zgl7903 2020-02-13
  • 打赏
  • 举报
回复
最大的可能性是没有考虑滚动条

#define GRIDCONTROL_GRIDLINEEXTENDTO_NOUSER //显示格扩展到非客户区

void CGridCtrl::OnDraw(CDC* pDC)
{
  if (!m_bAllowDraw)
    return;

  CRect clipRect;
  if (pDC->GetClipBox(&clipRect) == ERROR)
    return;

  EraseBkgnd(pDC);            // OnEraseBkgnd does nothing, so erase bkgnd here.
  // This necessary since we may be using a Memory DC.

#ifdef _DEBUG
  LARGE_INTEGER iStartCount;
  QueryPerformanceCounter(&iStartCount);
#endif

  CRect rect;
  int row, col;
  CGridCellBase* pCell;

  int nFixedRowHeight = GetFixedRowHeight();
  int nFixedColWidth  = GetFixedColumnWidth();

  CCellID idTopLeft = GetTopleftNonFixedCell();
  int minVisibleRow = idTopLeft.row,
    minVisibleCol = idTopLeft.col;

  CRect VisRect;
  CCellRange VisCellRange = GetVisibleNonFixedCellRange(VisRect);
  int maxVisibleRow = VisCellRange.GetMaxRow(),
    maxVisibleCol = VisCellRange.GetMaxCol();

  if (GetVirtualMode())
    SendCacheHintToParent(VisCellRange);

#ifdef GRIDCONTROL_GRIDLINEEXTENDTO_NOUSER
  do
  {
    int iColMax = GetColumnCount();
    if(iColMax <=0 ) break;

    //获取客户区大小
    CRect rcClient;
    GetClientRect(&rcClient);
    
    int icxUsed = 0;
    for(int iCol=0; iCol<iColMax-1; iCol++)
    {
      icxUsed += GetColumnWidth(iCol);
    }

    int iLastCX = rcClient.Width() - icxUsed;
    if(GetColumnWidth(iColMax-1) != iLastCX)
    {
      SetColumnWidth(iColMax-1, iLastCX);
      Invalidate();
    }
  }while(0);
#endif //GRIDCONTROL_GRIDLINEEXTENDTO_NOUSER

  // draw top-left cells 0..m_nFixedRows-1, 0..m_nFixedCols-1
  rect.bottom = -1;
  for (row = 0; row < m_nFixedRows; row++)
  {
    if (GetRowHeight(row) <= 0) continue;

    rect.top = rect.bottom+1;
    rect.bottom = rect.top + GetRowHeight(row)-1;
    rect.right = -1;

    for (col = 0; col < m_nFixedCols; col++)
    {
      if (GetColumnWidth(col) <= 0) continue;

      rect.left = rect.right+1;
      rect.right = rect.left + GetColumnWidth(col)-1;

      pCell = GetCell(row, col);
      if (pCell)
      {
        pCell->SetCoords(row,col);
        pCell->Draw(pDC, row, col, rect, FALSE);
      }
    }
  }

  // draw fixed column cells:  m_nFixedRows..n, 0..m_nFixedCols-1
  rect.bottom = nFixedRowHeight-1;
  for (row = minVisibleRow; row <= maxVisibleRow; row++)
  {
    if (GetRowHeight(row) <= 0) continue;

    rect.top = rect.bottom+1;
    rect.bottom = rect.top + GetRowHeight(row)-1;

    // rect.bottom = bottom pixel of previous row
    if (rect.top > clipRect.bottom)
      break;                // Gone past cliprect
    if (rect.bottom < clipRect.top)
      continue;             // Reached cliprect yet?

    rect.right = -1;
    for (col = 0; col < m_nFixedCols; col++)
    {
      if (GetColumnWidth(col) <= 0) continue;

      rect.left = rect.right+1;
      rect.right = rect.left + GetColumnWidth(col)-1;

      if (rect.left > clipRect.right)
        break;            // gone past cliprect
      if (rect.right < clipRect.left)
        continue;         // Reached cliprect yet?

      pCell = GetCell(row, col);
      if (pCell)
      {
        pCell->SetCoords(row,col);
        pCell->Draw(pDC, row, col, rect, FALSE);
      }
    }
  }

  // draw fixed row cells  0..m_nFixedRows, m_nFixedCols..n
  rect.bottom = -1;
  for (row = 0; row < m_nFixedRows; row++)
  {
    if (GetRowHeight(row) <= 0) continue;

    rect.top = rect.bottom+1;
    rect.bottom = rect.top + GetRowHeight(row)-1;

    // rect.bottom = bottom pixel of previous row
    if (rect.top > clipRect.bottom)
      break;                // Gone past cliprect
    if (rect.bottom < clipRect.top)
      continue;             // Reached cliprect yet?

    rect.right = nFixedColWidth-1;
    for (col = minVisibleCol; col <= maxVisibleCol; col++)
    {
      if (GetColumnWidth(col) <= 0) continue;

      rect.left = rect.right+1;
      rect.right = rect.left + GetColumnWidth(col)-1;

      if (rect.left > clipRect.right)
        break;        // gone past cliprect
      if (rect.right < clipRect.left)
        continue;     // Reached cliprect yet?

      pCell = GetCell(row, col);
      if (pCell)
      {
        pCell->SetCoords(row,col);
        pCell->Draw(pDC, row, col, rect, FALSE);
      }
    }

  }

  // draw rest of non-fixed cells
  rect.bottom = nFixedRowHeight-1;
  for (row = minVisibleRow; row <= maxVisibleRow; row++)
  {
    if (GetRowHeight(row) <= 0) continue;

    rect.top = rect.bottom+1;
    rect.bottom = rect.top + GetRowHeight(row)-1;

    // rect.bottom = bottom pixel of previous row
    if (rect.top > clipRect.bottom)
      break;                // Gone past cliprect
    if (rect.bottom < clipRect.top)
      continue;             // Reached cliprect yet?

    rect.right = nFixedColWidth-1;
    for (col = minVisibleCol; col <= maxVisibleCol; col++)
    {
      if (GetColumnWidth(col) <= 0) continue;

      rect.left = rect.right+1;
      rect.right = rect.left + GetColumnWidth(col)-1;

      if (rect.left > clipRect.right)
        break;        // gone past cliprect
      if (rect.right < clipRect.left)
        continue;     // Reached cliprect yet?

      pCell = GetCell(row, col);
      // TRACE(_T("Cell %d,%d type: %s\n"), row, col, pCell->GetRuntimeClass()->m_lpszClassName);
      if (pCell)
      {
        pCell->SetCoords(row,col);
        pCell->Draw(pDC, row, col, rect, FALSE);
      }
    }
  }


  CPen pen;
  pen.CreatePen(PS_SOLID, 0, m_crGridLineColour);
  pDC->SelectObject(&pen);

  // draw vertical lines (drawn at ends of cells)
  if (m_nGridLines == GVL_BOTH || m_nGridLines == GVL_VERT)
  {
    int x = nFixedColWidth;
    for (col = minVisibleCol; col <= maxVisibleCol; col++)
    {
      if (GetColumnWidth(col) <= 0) continue;

      x += GetColumnWidth(col);
      pDC->MoveTo(x-1, nFixedRowHeight);
      pDC->LineTo(x-1, VisRect.bottom);
    }
  }

  // draw horizontal lines (drawn at bottom of each cell)
  if (m_nGridLines == GVL_BOTH || m_nGridLines == GVL_HORZ)
  {
    int y = nFixedRowHeight;
    for (row = minVisibleRow; row <= maxVisibleRow; row++)
    {
      if (GetRowHeight(row) <= 0) continue;

      y += GetRowHeight(row);
      pDC->MoveTo(nFixedColWidth, y-1);
      pDC->LineTo(VisRect.right,  y-1);
    }
  }

  pDC->SelectStockObject(NULL_PEN);

  // Let parent know it can discard it's data if it needs to.
  if (GetVirtualMode())
    SendCacheHintToParent(CCellRange(-1,-1,-1,-1));

#ifdef _DEBUG
  LARGE_INTEGER iEndCount;
  QueryPerformanceCounter(&iEndCount);
  TRACE1("Draw counter ticks: %d\n", iEndCount.LowPart-iStartCount.LowPart);
#endif

}
zlxi 2020-02-12
  • 打赏
  • 举报
回复
引用 8 楼 zgl7903 的回复:
1 改造一下OnDraw 2 把背景色修改成字体背景色 //pGridLog->SetGridBkColor(RGB(0, 0, 0)); pGridLog->SetGridBkColor(RGB(248, 255, 255));


#define GRIDCONTROL_GRIDLINEEXTENDTO_NOUSER //显示格扩展到非客户区

void CGridCtrl::OnDraw(CDC* pDC)
{
  if (!m_bAllowDraw)
    return;

  CRect clipRect;
  if (pDC->GetClipBox(&clipRect) == ERROR)
    return;

  EraseBkgnd(pDC);            // OnEraseBkgnd does nothing, so erase bkgnd here.
  // This necessary since we may be using a Memory DC.

#ifdef _DEBUG
  LARGE_INTEGER iStartCount;
  QueryPerformanceCounter(&iStartCount);
#endif

  CRect rect;
  int row, col;
  CGridCellBase* pCell;

  int nFixedRowHeight = GetFixedRowHeight();
  int nFixedColWidth  = GetFixedColumnWidth();

  CCellID idTopLeft = GetTopleftNonFixedCell();
  int minVisibleRow = idTopLeft.row,
    minVisibleCol = idTopLeft.col;

  CRect VisRect;
  CCellRange VisCellRange = GetVisibleNonFixedCellRange(VisRect);
  int maxVisibleRow = VisCellRange.GetMaxRow(),
    maxVisibleCol = VisCellRange.GetMaxCol();

  if (GetVirtualMode())
    SendCacheHintToParent(VisCellRange);

#ifdef GRIDCONTROL_GRIDLINEEXTENDTO_NOUSER 
  CRect rcClient;
  GetClientRect(&rcClient);
  CBrush FixedRowColBack(GetDefaultCell(TRUE, TRUE)->GetBackClr());
#endif //GRIDCONTROL_GRIDLINEEXTENDTO_NOUSER

  // draw top-left cells 0..m_nFixedRows-1, 0..m_nFixedCols-1
  rect.bottom = -1;
  for (row = 0; row < m_nFixedRows; row++)
  {
    if (GetRowHeight(row) <= 0) continue;

    rect.top = rect.bottom+1;
    rect.bottom = rect.top + GetRowHeight(row)-1;
    rect.right = -1;

    for (col = 0; col < m_nFixedCols; col++)
    {
      if (GetColumnWidth(col) <= 0) continue;

      rect.left = rect.right+1;
      rect.right = rect.left + GetColumnWidth(col)-1;

      pCell = GetCell(row, col);
      if (pCell)
      {
        pCell->SetCoords(row,col);
        pCell->Draw(pDC, row, col, rect, FALSE);
      }
    }

#ifdef GRIDCONTROL_GRIDLINEEXTENDTO_NOUSER
    if(rect.bottom < rcClient.bottom)
    {
      CRect rcFill(rect.right, rect.top, rcClient.right, rect.bottom);
      pDC->FillRect(&rcFill, &FixedRowColBack);
    }
#endif //GRIDCONTROL_GRIDLINEEXTENDTO_NOUSER
  }

  // draw fixed column cells:  m_nFixedRows..n, 0..m_nFixedCols-1
  rect.bottom = nFixedRowHeight-1;
  for (row = minVisibleRow; row <= maxVisibleRow; row++)
  {
    if (GetRowHeight(row) <= 0) continue;

    rect.top = rect.bottom+1;
    rect.bottom = rect.top + GetRowHeight(row)-1;

    // rect.bottom = bottom pixel of previous row
    if (rect.top > clipRect.bottom)
      break;                // Gone past cliprect
    if (rect.bottom < clipRect.top)
      continue;             // Reached cliprect yet?

    rect.right = -1;
    for (col = 0; col < m_nFixedCols; col++)
    {
      if (GetColumnWidth(col) <= 0) continue;

      rect.left = rect.right+1;
      rect.right = rect.left + GetColumnWidth(col)-1;

      if (rect.left > clipRect.right)
        break;            // gone past cliprect
      if (rect.right < clipRect.left)
        continue;         // Reached cliprect yet?

      pCell = GetCell(row, col);
      if (pCell)
      {
        pCell->SetCoords(row,col);
        pCell->Draw(pDC, row, col, rect, FALSE);
      }
    }
  }

  // draw fixed row cells  0..m_nFixedRows, m_nFixedCols..n
  rect.bottom = -1;
  for (row = 0; row < m_nFixedRows; row++)
  {
    if (GetRowHeight(row) <= 0) continue;

    rect.top = rect.bottom+1;
    rect.bottom = rect.top + GetRowHeight(row)-1;

    // rect.bottom = bottom pixel of previous row
    if (rect.top > clipRect.bottom)
      break;                // Gone past cliprect
    if (rect.bottom < clipRect.top)
      continue;             // Reached cliprect yet?

    rect.right = nFixedColWidth-1;
    for (col = minVisibleCol; col <= maxVisibleCol; col++)
    {
      if (GetColumnWidth(col) <= 0) continue;

      rect.left = rect.right+1;
      rect.right = rect.left + GetColumnWidth(col)-1;

      if (rect.left > clipRect.right)
        break;        // gone past cliprect
      if (rect.right < clipRect.left)
        continue;     // Reached cliprect yet?

      pCell = GetCell(row, col);
      if (pCell)
      {
        pCell->SetCoords(row,col);
        pCell->Draw(pDC, row, col, rect, FALSE);
      }
    }

  }

  // draw rest of non-fixed cells
  rect.bottom = nFixedRowHeight-1;
  for (row = minVisibleRow; row <= maxVisibleRow; row++)
  {
    if (GetRowHeight(row) <= 0) continue;

    rect.top = rect.bottom+1;
    rect.bottom = rect.top + GetRowHeight(row)-1;

    // rect.bottom = bottom pixel of previous row
    if (rect.top > clipRect.bottom)
      break;                // Gone past cliprect
    if (rect.bottom < clipRect.top)
      continue;             // Reached cliprect yet?

    rect.right = nFixedColWidth-1;
    for (col = minVisibleCol; col <= maxVisibleCol; col++)
    {
      if (GetColumnWidth(col) <= 0) continue;

      rect.left = rect.right+1;
      rect.right = rect.left + GetColumnWidth(col)-1;

      if (rect.left > clipRect.right)
        break;        // gone past cliprect
      if (rect.right < clipRect.left)
        continue;     // Reached cliprect yet?

      pCell = GetCell(row, col);
      // TRACE(_T("Cell %d,%d type: %s\n"), row, col, pCell->GetRuntimeClass()->m_lpszClassName);
      if (pCell)
      {
        pCell->SetCoords(row,col);
        pCell->Draw(pDC, row, col, rect, FALSE);
      }
    }
  }


  CPen pen;
  pen.CreatePen(PS_SOLID, 0, m_crGridLineColour);
  pDC->SelectObject(&pen);

#ifdef GRIDCONTROL_GRIDLINEEXTENDTO_NOUSER
  VisRect.right = rcClient.right;
  VisRect.bottom = rcClient.bottom;
#endif //GRIDCONTROL_GRIDLINEEXTENDTO_NOUSER

  // draw vertical lines (drawn at ends of cells)
  if (m_nGridLines == GVL_BOTH || m_nGridLines == GVL_VERT)
  {
    int x = nFixedColWidth;
    for (col = minVisibleCol; col <= maxVisibleCol; col++)
    {
      if (GetColumnWidth(col) <= 0) continue;

      x += GetColumnWidth(col);
      pDC->MoveTo(x-1, nFixedRowHeight);
      pDC->LineTo(x-1, VisRect.bottom);
    }
  }

  // draw horizontal lines (drawn at bottom of each cell)
  if (m_nGridLines == GVL_BOTH || m_nGridLines == GVL_HORZ)
  {
    int y = nFixedRowHeight;
    for (row = minVisibleRow; row <= maxVisibleRow; row++)
    {
      if (GetRowHeight(row) <= 0) continue;

      y += GetRowHeight(row);
      pDC->MoveTo(nFixedColWidth, y-1);
      pDC->LineTo(VisRect.right,  y-1);
    }
  }

  pDC->SelectStockObject(NULL_PEN);

  // Let parent know it can discard it's data if it needs to.
  if (GetVirtualMode())
    SendCacheHintToParent(CCellRange(-1,-1,-1,-1));

#ifdef _DEBUG
  LARGE_INTEGER iEndCount;
  QueryPerformanceCounter(&iEndCount);
  TRACE1("Draw counter ticks: %d\n", iEndCount.LowPart-iStartCount.LowPart);
#endif

}

不能让这部分不延伸 出去?。这样感觉不美观。
zgl7903 2020-02-12
  • 打赏
  • 举报
回复
1 改造一下OnDraw 2 把背景色修改成字体背景色 //pGridLog->SetGridBkColor(RGB(0, 0, 0)); pGridLog->SetGridBkColor(RGB(248, 255, 255));


#define GRIDCONTROL_GRIDLINEEXTENDTO_NOUSER //显示格扩展到非客户区

void CGridCtrl::OnDraw(CDC* pDC)
{
  if (!m_bAllowDraw)
    return;

  CRect clipRect;
  if (pDC->GetClipBox(&clipRect) == ERROR)
    return;

  EraseBkgnd(pDC);            // OnEraseBkgnd does nothing, so erase bkgnd here.
  // This necessary since we may be using a Memory DC.

#ifdef _DEBUG
  LARGE_INTEGER iStartCount;
  QueryPerformanceCounter(&iStartCount);
#endif

  CRect rect;
  int row, col;
  CGridCellBase* pCell;

  int nFixedRowHeight = GetFixedRowHeight();
  int nFixedColWidth  = GetFixedColumnWidth();

  CCellID idTopLeft = GetTopleftNonFixedCell();
  int minVisibleRow = idTopLeft.row,
    minVisibleCol = idTopLeft.col;

  CRect VisRect;
  CCellRange VisCellRange = GetVisibleNonFixedCellRange(VisRect);
  int maxVisibleRow = VisCellRange.GetMaxRow(),
    maxVisibleCol = VisCellRange.GetMaxCol();

  if (GetVirtualMode())
    SendCacheHintToParent(VisCellRange);

#ifdef GRIDCONTROL_GRIDLINEEXTENDTO_NOUSER 
  CRect rcClient;
  GetClientRect(&rcClient);
  CBrush FixedRowColBack(GetDefaultCell(TRUE, TRUE)->GetBackClr());
#endif //GRIDCONTROL_GRIDLINEEXTENDTO_NOUSER

  // draw top-left cells 0..m_nFixedRows-1, 0..m_nFixedCols-1
  rect.bottom = -1;
  for (row = 0; row < m_nFixedRows; row++)
  {
    if (GetRowHeight(row) <= 0) continue;

    rect.top = rect.bottom+1;
    rect.bottom = rect.top + GetRowHeight(row)-1;
    rect.right = -1;

    for (col = 0; col < m_nFixedCols; col++)
    {
      if (GetColumnWidth(col) <= 0) continue;

      rect.left = rect.right+1;
      rect.right = rect.left + GetColumnWidth(col)-1;

      pCell = GetCell(row, col);
      if (pCell)
      {
        pCell->SetCoords(row,col);
        pCell->Draw(pDC, row, col, rect, FALSE);
      }
    }

#ifdef GRIDCONTROL_GRIDLINEEXTENDTO_NOUSER
    if(rect.bottom < rcClient.bottom)
    {
      CRect rcFill(rect.right, rect.top, rcClient.right, rect.bottom);
      pDC->FillRect(&rcFill, &FixedRowColBack);
    }
#endif //GRIDCONTROL_GRIDLINEEXTENDTO_NOUSER
  }

  // draw fixed column cells:  m_nFixedRows..n, 0..m_nFixedCols-1
  rect.bottom = nFixedRowHeight-1;
  for (row = minVisibleRow; row <= maxVisibleRow; row++)
  {
    if (GetRowHeight(row) <= 0) continue;

    rect.top = rect.bottom+1;
    rect.bottom = rect.top + GetRowHeight(row)-1;

    // rect.bottom = bottom pixel of previous row
    if (rect.top > clipRect.bottom)
      break;                // Gone past cliprect
    if (rect.bottom < clipRect.top)
      continue;             // Reached cliprect yet?

    rect.right = -1;
    for (col = 0; col < m_nFixedCols; col++)
    {
      if (GetColumnWidth(col) <= 0) continue;

      rect.left = rect.right+1;
      rect.right = rect.left + GetColumnWidth(col)-1;

      if (rect.left > clipRect.right)
        break;            // gone past cliprect
      if (rect.right < clipRect.left)
        continue;         // Reached cliprect yet?

      pCell = GetCell(row, col);
      if (pCell)
      {
        pCell->SetCoords(row,col);
        pCell->Draw(pDC, row, col, rect, FALSE);
      }
    }
  }

  // draw fixed row cells  0..m_nFixedRows, m_nFixedCols..n
  rect.bottom = -1;
  for (row = 0; row < m_nFixedRows; row++)
  {
    if (GetRowHeight(row) <= 0) continue;

    rect.top = rect.bottom+1;
    rect.bottom = rect.top + GetRowHeight(row)-1;

    // rect.bottom = bottom pixel of previous row
    if (rect.top > clipRect.bottom)
      break;                // Gone past cliprect
    if (rect.bottom < clipRect.top)
      continue;             // Reached cliprect yet?

    rect.right = nFixedColWidth-1;
    for (col = minVisibleCol; col <= maxVisibleCol; col++)
    {
      if (GetColumnWidth(col) <= 0) continue;

      rect.left = rect.right+1;
      rect.right = rect.left + GetColumnWidth(col)-1;

      if (rect.left > clipRect.right)
        break;        // gone past cliprect
      if (rect.right < clipRect.left)
        continue;     // Reached cliprect yet?

      pCell = GetCell(row, col);
      if (pCell)
      {
        pCell->SetCoords(row,col);
        pCell->Draw(pDC, row, col, rect, FALSE);
      }
    }

  }

  // draw rest of non-fixed cells
  rect.bottom = nFixedRowHeight-1;
  for (row = minVisibleRow; row <= maxVisibleRow; row++)
  {
    if (GetRowHeight(row) <= 0) continue;

    rect.top = rect.bottom+1;
    rect.bottom = rect.top + GetRowHeight(row)-1;

    // rect.bottom = bottom pixel of previous row
    if (rect.top > clipRect.bottom)
      break;                // Gone past cliprect
    if (rect.bottom < clipRect.top)
      continue;             // Reached cliprect yet?

    rect.right = nFixedColWidth-1;
    for (col = minVisibleCol; col <= maxVisibleCol; col++)
    {
      if (GetColumnWidth(col) <= 0) continue;

      rect.left = rect.right+1;
      rect.right = rect.left + GetColumnWidth(col)-1;

      if (rect.left > clipRect.right)
        break;        // gone past cliprect
      if (rect.right < clipRect.left)
        continue;     // Reached cliprect yet?

      pCell = GetCell(row, col);
      // TRACE(_T("Cell %d,%d type: %s\n"), row, col, pCell->GetRuntimeClass()->m_lpszClassName);
      if (pCell)
      {
        pCell->SetCoords(row,col);
        pCell->Draw(pDC, row, col, rect, FALSE);
      }
    }
  }


  CPen pen;
  pen.CreatePen(PS_SOLID, 0, m_crGridLineColour);
  pDC->SelectObject(&pen);

#ifdef GRIDCONTROL_GRIDLINEEXTENDTO_NOUSER
  VisRect.right = rcClient.right;
  VisRect.bottom = rcClient.bottom;
#endif //GRIDCONTROL_GRIDLINEEXTENDTO_NOUSER

  // draw vertical lines (drawn at ends of cells)
  if (m_nGridLines == GVL_BOTH || m_nGridLines == GVL_VERT)
  {
    int x = nFixedColWidth;
    for (col = minVisibleCol; col <= maxVisibleCol; col++)
    {
      if (GetColumnWidth(col) <= 0) continue;

      x += GetColumnWidth(col);
      pDC->MoveTo(x-1, nFixedRowHeight);
      pDC->LineTo(x-1, VisRect.bottom);
    }
  }

  // draw horizontal lines (drawn at bottom of each cell)
  if (m_nGridLines == GVL_BOTH || m_nGridLines == GVL_HORZ)
  {
    int y = nFixedRowHeight;
    for (row = minVisibleRow; row <= maxVisibleRow; row++)
    {
      if (GetRowHeight(row) <= 0) continue;

      y += GetRowHeight(row);
      pDC->MoveTo(nFixedColWidth, y-1);
      pDC->LineTo(VisRect.right,  y-1);
    }
  }

  pDC->SelectStockObject(NULL_PEN);

  // Let parent know it can discard it's data if it needs to.
  if (GetVirtualMode())
    SendCacheHintToParent(CCellRange(-1,-1,-1,-1));

#ifdef _DEBUG
  LARGE_INTEGER iEndCount;
  QueryPerformanceCounter(&iEndCount);
  TRACE1("Draw counter ticks: %d\n", iEndCount.LowPart-iStartCount.LowPart);
#endif

}

zlxi 2020-02-12
  • 打赏
  • 举报
回复
引用 5 楼 zgl7903 的回复:
可以把你的项目打个包发上来看看
https://download.csdn.net/download/zltian/12152226 版主大神 csdn 审核通过了,也可以下载了
zlxi 2020-02-12
  • 打赏
  • 举报
回复
引用 5 楼 zgl7903 的回复:
可以把你的项目打个包发上来看看
csdn 上传还没审核 百度网盘分享 https://pan.baidu.com/s/1dCnjeH7PlDo1P9lOu4XhBA 提取码 ov0k
zgl7903 2020-02-12
  • 打赏
  • 举报
回复
可以把你的项目打个包发上来看看
zlxi 2020-02-12
  • 打赏
  • 举报
回复
引用 3 楼 zgl7903 的回复:
CGridCtrl 其实有一个很好的功能 AutoSize
pGridLog->MoveWindow(m_AloneListRect); 
pGridLog->AutoSize(GVS_BOTH);
//后面的行列调整的可以都不要
关闭行列调整, 备注那一列,直接现实黑色背景。
zgl7903 2020-02-12
  • 打赏
  • 举报
回复
CGridCtrl 其实有一个很好的功能 AutoSize
pGridLog->MoveWindow(m_AloneListRect); 
pGridLog->AutoSize(GVS_BOTH);
//后面的行列调整的可以都不要
zlxi 2020-02-12
  • 打赏
  • 举报
回复
引用 1 楼 zgl7903 的回复:
pGridLog->SetGridBkColor(RGB(0, 0, 0)); //是否和这个颜色有关?
不管用什么颜色都会,多出一截。 1.拉动最后一个表格,就会多出这么一截。 期望表格占满,整个背景
zgl7903 2020-02-12
  • 打赏
  • 举报
回复
pGridLog->SetGridBkColor(RGB(0, 0, 0)); //是否和这个颜色有关?

15,980

社区成员

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

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