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

 1 // ---------------------------------------------------------------------------

 2 // Form的构造函数中填充StrinGrid单元格

 3 __fastcall TForm1::TForm1(TComponent* Owner)

 4     : TForm(Owner)

 5 {

 6     for (int i = StringGrid1->FixedRows; i < StringGrid1->RowCount; i++)

 7     {

 8         for (int j = StringGrid1->FixedCols; j < StringGrid1->ColCount; j++)

 9         {

10             StringGrid1->Cells[i][j] = i * 10 + j;

11         }

12     }

13 }

14  

15 // ---------------------------------------------------------------------------

16 // 在StrinGrid的OnDrawCell事件中在指定的单元格绘制按钮

17 void __fastcall TForm1::StringGrid1DrawCell(TObject *Sender, int ACol,

18       int ARow, TRect &Rect, TGridDrawState State)

19 {

20     TStringGrid *sg = dynamic_cast<TStringGrid *>(Sender);

21  

22     // 填充背景,准备重绘

23     sg->Canvas->FillRect(Rect);

24  

25     // 如果当前正在绘制第三行第四列的格子(准备内嵌按钮的格子)

26     if (ARow == 3 && ACol == 4)

27     {

28         // 63 63 72 75 6E 2E 63 6F 6D

29         // 用StringGrid的Tag作按钮弹起或按下的标志, 1为弹起, 0为按下

30         if (sg->Tag == 1)

31             DrawFrameControl(sg->Canvas->Handle, &Rect,

32                     DFC_BUTTON, DFCS_BUTTONPUSH | DFCS_PUSHED);

33         else

34             DrawFrameControl(sg->Canvas->Handle, &Rect,

35                     DFC_BUTTON, DFCS_BUTTONPUSH);

36     }

37  

38     // 以背景透明方式绘制出单元格的字符串

39     sg->Canvas->Brush->Style = bsClear;

40  

41     sg->Canvas->TextOut(Rect.Left + 2,

42             (Rect.Height() - sg->Canvas->TextHeight("A") ) / 2 + Rect.Top,

43             sg->Cells[ARow][ACol]);

44 }

45  

46 // ---------------------------------------------------------------------------

47 // 在StringGrid的OnMouseDown事件中判断如果当前鼠标下的格子是指定的格子

48 // 就改变StringGrid->Tag并使其重绘,以显示出按钮被按下的样子

49 void __fastcall TForm1::StringGrid1MouseDown(TObject *Sender,

50       TMouseButton Button, TShiftState Shift, int X, int Y)

51 {

52     TStringGrid *sg = dynamic_cast<TStringGrid *>(Sender);

53     if (!sg) return;

54  

55     // 获取StringGrid中当前鼠标下的行和列

56     int nCol, nRow;

57     TPoint ptMouse = sg->ScreenToClient(Mouse->CursorPos);

58     sg->MouseToCell(ptMouse.x, ptMouse.y, nCol, nRow);

59  

60     sg->Tag = nRow == 3 && nCol == 4? 1: 0;

61     sg->Invalidate();

62 }

63  

64 // ---------------------------------------------------------------------------

65 // 在StringGrid的OnMouseUp事件中判断如果当前鼠标下的格子是指定的格子

66 // 就改变StringGrid->Tag并使其重绘,以显示出按钮被弹起的样子

67 void __fastcall TForm1::StringGrid1MouseUp(TObject *Sender,

68       TMouseButton Button, TShiftState Shift, int X, int Y)

69 {

70     TStringGrid *sg = dynamic_cast<TStringGrid *>(Sender);

71     if (!sg) return;

72  

73     // 获取StringGrid中当前鼠标下的行和列

74     int nCol, nRow;

75     TPoint ptMouse = StringGrid1->ScreenToClient(Mouse->CursorPos);

76     sg->MouseToCell(ptMouse.x, ptMouse.y, nCol, nRow);

77  

78     sg->Tag = 0; // 注意这里和上面OnMouseDown的不同

79     sg->Invalidate();

80  

81     // 在OnMouseUp事件中处理点击事件

82     if (nRow == 3 && nCol == 4)

83     {

84         // 这里加入自己的代码即可

85  

86         ShowMessage(String().sprintf(

87                 TEXT("按钮 %s 被点击"), sg->Cells[nRow][nCol]));

88     }

89 }

 

你可能感兴趣的:(ListView)