控件StringGrid1使用中的一个小问题(30分)

  • 主题发起人 主题发起人 天空4567
  • 开始时间 开始时间

天空4567

Unregistered / Unconfirmed
GUEST, unregistred user!
控件StringGrid1使用时,如何让其单元格cells水平和垂直居中?
 
用DrawText 。
 
二者有什么本质的不同吗?StringGrid1没有对齐属性吗?
DrawText在哪个面板上?
 
哦, DrawText不是控件。是个函数。
 
procedure TForm4.StringGrid1DrawCell(Sender: TObject; ACol,
ARow: Integer; Rect: TRect; State: TGridDrawState);
const
WSpace = 2;
HSpace = 2;
var
tmpstr: string;
w,h: integer;
iType:Integer;
Area:TRect;
begin
if ImpStringGrid1.Cells[ACol,ARow]='NG' then begin
with ImpStringGrid1.Canvas do
begin
FillRect(Rect);
Font.Color:=clred;
TextOut(Rect.left,Rect.top,ImpStringGrid1.cells[Acol,Arow]);
end;
end;
iType:=1;
if iType=0 then begin
tmpstr := (Sender as TStringGrid).Cells[ACol,ARow];
with (Sender as TStringGrid).Canvas do begin
w := TextWidth(tmpstr);
h := TextHeight(tmpstr);
if (gdFixed in State) then
TextRect(Rect,trunc((Rect.Right+Rect.Left-w)/2)+WSpace, //居中显示
trunc((Rect.Bottom+Rect.Top-H)/2)+HSpace,tmpstr)
else //右对齐显示
TextRect(Rect,Rect.Right-WSpace-W,Rect.Bottom-HSpace-H,tmpstr);
end;
end else begin
//画文本,可以设置显示的对齐方式
with (Sender as TStringGrid).Canvas do begin
FillRect(Rect);
Area:= Rect;
InflateRect(Area, -2, -2);
//if ARow=0 then
DrawText(Handle, PChar((Sender as TStringGrid).Cells[ACol, ARow]),Length((Sender as TStringGrid).Cells[ACol, ARow]), Area,DT_CENTER + DT_SINGLELINE + DT_VCENTER);//居中
//DrawText(Handle, PChar((Sender as TStringGrid).Cells[ACol, ARow]),Length((Sender as TStringGrid).Cells[ACol, ARow]), Area, DT_CENTER or DT_VCENTER);//居中
//else if (ACol=0) and (ARow<>0) then
//DrawText(Handle, PChar(Cells[ACol, ARow]),Length(Cells[ACol, ARow]), Area, DT_RIGHT)//左对齐
//else
//DrawText(Handle, PChar(Cells[ACol, ARow]),Length(Cells[ACol, ARow]), Area, DT_LEFT);//右对齐
end;
end;
end;
 
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
StringGrid1.Canvas.FillRect(Rect);
DrawText(StringGrid1.Canvas.Handle,pChar(StringGrid1.Cells[ACol,ARow]),Length(StringGrid1.Cells[ACol,ARow]),Rect,DT_SINGLELINE + DT_VCENTER + DT_CENTER);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
StringGrid1.Cells[0,0]:= '测试';
StringGrid1.Cells[0,1]:= 'Delphi';
StringGrid1.Cells[2,1]:= '程序';
end;
 
建议你使用advstringgrid 它含有单元格cells水平和垂直居中等
 
谢谢楼上的。
DrawText(StringGrid1.Canvas.Handle,pChar(StringGrid1.Cells[ACol,ARow]),Length(StringGrid1.Cells[ACol,ARow]),Rect,DT_SINGLELINE + DT_VCENTER + DT_CENTER);
里的参数该如何写?(假定StringGrid1为10行10列)
 
if (ACol =10) and (ARow = 10) then
DrawText(StringGrid1.Canvas.Handle,pChar(StringGrid1.Cells[10,10]),Length(StringGrid1.Cells[10,10]),Rect,DT_SINGLELINE + DT_VCENTER + DT_CENTER);
 
建议你使用advstringgrid
 
advstringgrid --在哪??
 
后退
顶部