関于TStringGrid控件。我座東、進入者有分!

ailibuli 2004-04-16 04:29:22
最近回答過一些有関TStringGrid控件的文字排版問題。
到昨日為至発現有誤↓、在此更正一下。
http://expert.csdn.net/Expert/topic/2967/2967468.xml?temp=.7409632

為表誠意、対以前看到一些問題做了総結、希望同仁們指正!

unit Unit1;
interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, StdCtrls;

type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
Button1: TButton;
procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
procedure Button1Click(Sender: TObject);
private
{ Private 宣言 }
public
{ Public 宣言 }
end;

var
Form1: TForm1;

implementation
{$R *.dfm}

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; //調整上下

DrawText(StringGrid1.Canvas.Handle,PChar(CellStr),Length(CellStr),Rect,Mode);
end;

procedure TForm1.Button1Click(Sender: TObject);
var i,j: integer;
begin
StringGrid1.Cells[0,0] := 'Column0';
StringGrid1.Cells[1,0] := 'Column1';
StringGrid1.Cells[2,0] := 'Column2';
StringGrid1.Cells[3,0] := 'Column3';
StringGrid1.Cells[4,0] := 'Column4';

StringGrid1.RowCount := 1001;
for i:=1 to 1000 do begin
for j:=0 to 4 do begin
StringGrid1.Cells[j,i] := '行:'+inttostr(i) + ' 列:'+inttostr(j);
end;
end;
end;

{Mode的設定値:DT_LEFT, DT_RIGHT, DT_CENTER...
 例設定如下:)
  StringGrid1.FixedCols := 0;
 StringGrid1.FixedRows := 1;
}
end.

(注:根据個人需要可対①~④増加、削除)
...全文
72 40 打赏 收藏 转发到动态 举报
写回复
用AI写文章
40 条回复
切换为时间正序
请发表友善的回复…
发表回复
Tensionli 2004-04-19
  • 打赏
  • 举报
回复
学习+DING
onebody 2004-04-19
  • 打赏
  • 举报
回复
weijian0131 2004-04-19
  • 打赏
  • 举报
回复
为楼主的务实态度敬礼!
ailibuli 2004-04-19
  • 打赏
  • 举报
回复
to djk2003(★飘香一剑★)
俺査到了:http://www.tommstudio.com
 君是指↓此段嗎? 
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
stringgrid1.Canvas.TextOut(rect.left+14,rect.top+8,'标题居中');
end;

可是:当StringGrid1.Options := StringGrid1.Options + [goColSizing]...時、
   就無法動態的居中了?
fim 2004-04-19
  • 打赏
  • 举报
回复
好贴,接分
ailibuli 2004-04-19
  • 打赏
  • 举报
回复
to djk2003(★飘香一剑★)
http://www.tommstudio.comde好象没有此頁呀?

to aiirii(ari-爱的眼睛)
"可到 delphibbs 參考 大富翁日記, 裹面也有好几篇"
可俺在http://www.delphibbs.com/delphibbs中做检索、有五百多条、実在是...

不看上面的乱碼也吧!其実也想借此題給大家散点技術分、大部分散分都放到了非技術区、
也怪可惜的、就算俺為加速大家走向”★光大道”添砌加瓦吧!
当然更歓迎提意見。(^Q^)


lzy6204 2004-04-19
  • 打赏
  • 举报
回复
呵呵,我是来混分的!
  • 打赏
  • 举报
回复
接分
zfang 2004-04-19
  • 打赏
  • 举报
回复
收藏
hhzqf1980 2004-04-19
  • 打赏
  • 举报
回复
排版不错,收藏
djk2003 2004-04-19
  • 打赏
  • 举报
回复
你参考一下www.tommstudio.comde论坛,有这样一个问题解答。
zfang 2004-04-16
  • 打赏
  • 举报
回复
up先
anan3000 2004-04-16
  • 打赏
  • 举报
回复
ding
nyf1220 2004-04-16
  • 打赏
  • 举报
回复
排版不错,收藏
nizijian 2004-04-16
  • 打赏
  • 举报
回复
8错8错
jb99334 2004-04-16
  • 打赏
  • 举报
回复
进来看看
snowyxx 2004-04-16
  • 打赏
  • 举报
回复
只为分来
哈哈
22932565 2004-04-16
  • 打赏
  • 举报
回复
不错
Dlwxn 2004-04-16
  • 打赏
  • 举报
回复
学习!
juliens 2004-04-16
  • 打赏
  • 举报
回复
领教,学习!
加载更多回复(20)

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧