怎么样使TStringGrid的某列对齐(左或中)?
解决方案 »
- 如何取得服务器上做的RAID5 以后硬盘的ID?
- delphi调用VC的动态库的问题啊!!!!!
- 高分请教 delphi7 中 indy 版本的问题
- 我在DEPHI 7 中找不到dxDBgrid控件
- 请问我想在工程里做几个公用函数,不需要界面的,就新建了一个UNIT,里面写了函数,可说申明出错
- 安装分发程序
- 如何写一个不使用SOCKET控件的SOCKET程序?请各位帮忙。
- 如何调用windows标准的API函数?
- 请问在combobox中如何加入列表
- QuickReport错误:A component named QRStandared Preview already exists
- 谁会用DBGrid显示 文本文件域者 显示自己的数组
- 怎么样使TStringGrid中的某列对齐?
以前看到一个,给楼主参考一下,下面的是第一行中间对其,你修改一下就可以了
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
CellStr: String;
Mode,CellLen: Integer;
begin
//①先清除CELL
StringGrid1.Canvas.FillRect(Rect); CellStr := StringGrid1.Cells[ACol,ARow];
//②根据文字長度自動調節列寛
CellLen := StringGrid1.Canvas.TextWidth(CellStr)+3;
if (CellLen>StringGrid1.ColWidths[ACol]) then
StringGrid1.ColWidths[ACol] := CellLen; //③底色設定
if ARow>0 then begin
if ARow mod 2 = 0 then
StringGrid1.Canvas.Brush.Color := $00DBDBDB
else
StringGrid1.Canvas.Brush.Color := clWhite;
StringGrid1.Canvas.TextRect(Rect,Rect.Left,Rect.Top,'');
end; //④文字排版
if ARow=0 then begin
StringGrid1.Canvas.Font.Color := clRed; //可設定字体的顔色、大小
Mode := DT_CENTER //調整左右
end
else begin
StringGrid1.Canvas.Font.Color := clWindowText;
Mode := DT_LEFT;
Rect.Left := Rect.Left+2; //微調(使字不圧左側的線)
end;
Rect.Top := Rect.Top+3; //調整上下
//StringGrid1.Canvas.TextRect(rect,rect.Left,rect.Top,cellstr); 这种方法和下面的方法效果相同
DrawText(StringGrid1.Canvas.Handle,PChar(CellStr),Length(CellStr),Rect,Mode);end;
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
with StringGrid1 do
begin
if ACol = 2 then
begin
Canvas.FillRect(Rect);
DrawText(Canvas.Handle,PChar(Cells[ACol,ARow]), -1, Rect, DT_RIGHT);
end;
end;
end;
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
l_Left, l_Top: Integer;
begin
l_Left := (StringGrid1.Colwidths[ACol] - StringGrid1.Canvas.Textwidth(StringGrid1.Cells[ACol, ARow]) - 2); //右对齐
l_Top := (StringGrid1.Rowheights[ARow] - StringGrid1.Canvas.Textheight(StringGrid1.Cells[ACol, ARow])) div 2;
StringGrid1.Canvas.Textrect(Rect, Rect.Left + l_Left, Rect.Top + l_Top, StringGrid1.Cells[ACol, ARow]);
end;