c++ builder ListView实现可编辑任意列(转)

时间:2024-01-16 16:06:44
 // ---------------------------------------------------------------------------
// Form的构造函数中填充StrinGrid单元格
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
for (int i = StringGrid1->FixedRows; i < StringGrid1->RowCount; i++)
{
for (int j = StringGrid1->FixedCols; j < StringGrid1->ColCount; j++)
{
StringGrid1->Cells[i][j] = i * + j;
}
}
} // ---------------------------------------------------------------------------
// 在StrinGrid的OnDrawCell事件中在指定的单元格绘制按钮
void __fastcall TForm1::StringGrid1DrawCell(TObject *Sender, int ACol,
int ARow, TRect &Rect, TGridDrawState State)
{
TStringGrid *sg = dynamic_cast<TStringGrid *>(Sender); // 填充背景,准备重绘
sg->Canvas->FillRect(Rect); // 如果当前正在绘制第三行第四列的格子(准备内嵌按钮的格子)
if (ARow == && ACol == )
{
// 63 63 72 75 6E 2E 63 6F 6D
// 用StringGrid的Tag作按钮弹起或按下的标志, 1为弹起, 0为按下
if (sg->Tag == )
DrawFrameControl(sg->Canvas->Handle, &Rect,
DFC_BUTTON, DFCS_BUTTONPUSH | DFCS_PUSHED);
else
DrawFrameControl(sg->Canvas->Handle, &Rect,
DFC_BUTTON, DFCS_BUTTONPUSH);
} // 以背景透明方式绘制出单元格的字符串
sg->Canvas->Brush->Style = bsClear; sg->Canvas->TextOut(Rect.Left + ,
(Rect.Height() - sg->Canvas->TextHeight("A") ) / + Rect.Top,
sg->Cells[ARow][ACol]);
} // ---------------------------------------------------------------------------
// 在StringGrid的OnMouseDown事件中判断如果当前鼠标下的格子是指定的格子
// 就改变StringGrid->Tag并使其重绘,以显示出按钮被按下的样子
void __fastcall TForm1::StringGrid1MouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
TStringGrid *sg = dynamic_cast<TStringGrid *>(Sender);
if (!sg) return; // 获取StringGrid中当前鼠标下的行和列
int nCol, nRow;
TPoint ptMouse = sg->ScreenToClient(Mouse->CursorPos);
sg->MouseToCell(ptMouse.x, ptMouse.y, nCol, nRow); sg->Tag = nRow == && nCol == ? : ;
sg->Invalidate();
} // ---------------------------------------------------------------------------
// 在StringGrid的OnMouseUp事件中判断如果当前鼠标下的格子是指定的格子
// 就改变StringGrid->Tag并使其重绘,以显示出按钮被弹起的样子
void __fastcall TForm1::StringGrid1MouseUp(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
TStringGrid *sg = dynamic_cast<TStringGrid *>(Sender);
if (!sg) return; // 获取StringGrid中当前鼠标下的行和列
int nCol, nRow;
TPoint ptMouse = StringGrid1->ScreenToClient(Mouse->CursorPos);
sg->MouseToCell(ptMouse.x, ptMouse.y, nCol, nRow); sg->Tag = ; // 注意这里和上面OnMouseDown的不同
sg->Invalidate(); // 在OnMouseUp事件中处理点击事件
if (nRow == && nCol == )
{
// 这里加入自己的代码即可 ShowMessage(String().sprintf(
TEXT("按钮 %s 被点击"), sg->Cells[nRow][nCol]));
}
}