一.设置文本属性.
注意到,在CELL这个方法中,与我们常用的VBA参数的写法是相反的.
DELPHI先列后行: property Cells[ACol, ARow: Integer]: string read GetCells write SetCells;
VBA先行后列: CELLS(ROW,COL)
procedure TForm1.FormCreate(Sender: TObject); beginStringGrid1.Cells[0,0]:='序号';StringGrid1.Cells[1,0]:='操作';StringGrid1.Cells[2,0]:='结果';StringGrid1.Cells[0,1]:='1'; end;procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: LongInt;Rect: TRect; State: TGridDrawState); varS: string;DrawFlags: Integer;LGrid: TStringGrid; beginLGrid := TStringGrid(Sender);// 1. 处理标题行 (FixedRows, 这里假定是第0行)if (ARow = 0) thenbegin// 设置标题行单元格背景色LGrid.Canvas.Brush.Color := clBtnFace; // 使用标准按钮面部颜色作为标题背景LGrid.Canvas.FillRect(Rect); // 用画刷颜色填充单元格矩形区域// 设置标题字体为加粗LGrid.Canvas.Font.Style := [fsBold];// 设置文本颜色(可选)// LGrid.Canvas.Font.Color := clWindowText; S := LGrid.Cells[ACol, ARow]; // 获取单元格文本DrawFlags := DT_VCENTER or DT_SINGLELINE or DT_CENTER; // 垂直居中、单行、水平居中// 使用 DrawText 函数绘制文本,实现居中 DrawText(LGrid.Canvas.Handle, PChar(S), Length(S), Rect, DrawFlags);endelsebegin// 2. 处理数据行(非标题行)的默认绘制(可选)// 如果你想保持数据行的默认绘制方式,或者也想自定义数据行,可以在这里处理LGrid.Canvas.Brush.Color := clWindow; // 默认窗口背景色 LGrid.Canvas.FillRect(Rect);LGrid.Canvas.Font.Style := []; // 常规字体// 使用 TextRect 绘制数据行文本(默认左对齐)LGrid.Canvas.TextRect(Rect, Rect.Left + 2, Rect.Top + 2, LGrid.Cells[ACol, ARow]);// 或者,如果你希望数据行也居中,可以类似标题行那样使用 DrawText// DrawFlags := DT_VCENTER or DT_SINGLELINE or DT_CENTER;// DrawText(LGrid.Canvas.Handle, PChar(LGrid.Cells[ACol, ARow]), -1, Rect, DrawFlags);end; end;
二.表格右键菜单设置.
这里提醒一下大家,菜单在创建时,其name是随机的,比如K1,N1,M1等等,请务必修改为具有实际意义的名称,方便阅读与引用.
在引用菜单是,请直接使用菜单的name,不要使用Item属性,比如下面这种写法非常不推荐使用.
PopupMenu1.Items[0].Visible := False; // '删除行'
然后设置表格的OnMouseDown事件
procedure TForm1.StringGrid1MouseDown(Sender: TObject; Button: TMouseButton;Shift: TShiftState; X, Y: Integer); varACol, ARow: Integer; beginif Button = mbRight then // 检查是否是右键点击beginStringGrid1.MouseToCell(X, Y, ACol, ARow); // 将鼠标坐标转换为单元格坐标.一个常用的,强大而有越的方法// 使用菜单项的 Name 来引用,这样更清晰可靠mnuDeleteRow.Visible := (ACol = 0) and (ARow > 0); // 在第一列(非标题行)mnuDeleteCol.Visible := (ARow = 0) and (ACol > 0); // 在第一行(非标题列)mnuClear.Visible := (ARow > 0) and (ACol > 0); // 在数据区域end; end;