前天下载了MFCGrid2.27版本,地址就不说了,www.codeproject.com上自己找吧..
这个控件无比强大.简直就可以和我以前用的TAdvStringGrid相比了.当然c++builder下的东西比vc下的东西,仅从小例子小工程来说,还是方便多了...废话不多说了,直入正题吧.
MFCGrid支持隐藏行和列的功能,但是它是将该行和列宽度设置为0的,当隐藏后再次去拖动隐藏的列的头的时候,可以将隐藏的列再显示出来.这显然不符合隐藏的概念,既然隐藏了,就不允许手动操作再显示出来吧?比如,我有3列分别显示用户内部编号,用户编号,用户姓名,事实是我是不能显示用户的内部编号这一列的,因为在业务上来说,使用者根本不关心用户的内部编号,但是系统又需要这个用户内部编号来保持用户的唯一性.那怎么办,只能隐藏它了,于是就只显示两列,但是第一列用户内部编号的值都是存在的,只是用户看不见,也无法让它显示出来而已.为实现该想法,我修改了一下MFCGrid的代码,如下:
// Get cell from point.
// point - client coordinates
// bAllowFixedCellCheck - if TRUE then fixed cells are checked
CCellID CGridCtrl::GetCellFromPt(CPoint point, BOOL bAllowFixedCellCheck /*=TRUE*/)
{
CCellID cellID; // return value
CCellID idTopLeft = GetTopleftNonFixedCell();
if (!bAllowFixedCellCheck && !IsValid(idTopLeft))
return cellID;
// calculate column index
int fixedColWidth = GetFixedColumnWidth();
if (point.x < 0 || (!bAllowFixedCellCheck && point.x < fixedColWidth)) // not in window
cellID.col = -1;
else if (point.x < fixedColWidth) // in fixed col
{
int xpos = 0;
int col = 0;
int ncol = 0;
int width = 0;
while (col < m_nFixedCols)
{
width = GetColumnWidth(col);
xpos += width;
if (xpos > point.x)
break;
if(width>0)
ncol =col;
col++;
}
cellID.col = (MOUSE_OVER_COL_DIVIDE==m_MouseMode) && (col-ncol>1) &&(point.x-(xpos-width)<=m_nResizeCaptureRange) ? ncol:col;
}
else // in non-fixed col
{
int xpos = fixedColWidth;
int col = idTopLeft.col; //m_nFixedCols;
int ncol= col;
int width=0;
while ( col < GetColumnCount())
{
width = GetColumnWidth(col);
xpos += width;
if (xpos > point.x)
break;
if(width>0)
ncol = col;
col++;
}
if (col >= GetColumnCount())
cellID.col = -1;
else
cellID.col = (MOUSE_OVER_COL_DIVIDE==m_MouseMode) && (col-ncol>1) &&(point.x-(xpos-width)<=m_nResizeCaptureRange) ? ncol:col;
}
// calculate row index
int fixedRowHeight = GetFixedRowHeight();
if (point.y < 0 || (!bAllowFixedCellCheck && point.y < fixedRowHeight)) // not in window
cellID.row = -1;
else if (point.y < fixedRowHeight) // in fixed col
{
int ypos = 0;
int row = 0;
int nrow = 0;
int height = 0;
while (row < m_nFixedRows)
{
height= GetRowHeight(row);
ypos += height;
if (ypos > point.y)
break;
if(height>0)
nrow = row;
row++;
}
cellID.row = (MOUSE_OVER_ROW_DIVIDE==m_MouseMode) && (row-nrow>1) &&(point.y-(ypos-height)<=m_nResizeCaptureRange) ? nrow:row;
}
else
{
int ypos = fixedRowHeight;
int row = idTopLeft.row; //m_nFixedRows;
int nrow = row;
int height = 0;
while ( row < GetRowCount() )
{
height= GetRowHeight(row);
ypos += height;
if (ypos > point.y)
break;
if(height>0)
nrow = row;
row++;
}
if (row >= GetRowCount())
cellID.row = -1;
else
cellID.row = (MOUSE_OVER_ROW_DIVIDE==m_MouseMode) && (row-nrow>1) &&(point.y-(ypos-height)<=m_nResizeCaptureRange) ? nrow:row;
}
return cellID;
}
初步测试OK,这个需要也不知道原作者是否认可,就不给作者发邮件了,版权什么的都归原作者所有.