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 }

 

posted on 2013-08-25 23:01 kernel_main 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/kernel0815/p/3281656.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值