您的位置:首页 > 其它

实现TStringGrid的左对齐、居中和右对齐

2012-05-12 11:02 155 查看
//sgrdQty: TStringGrid

procedure TfrmPDStock.sgrdQtyDrawCell(Sender: TObject; ACol, ARow: Integer;

Rect: TRect; State: TGridDrawState);

var

sText: String;

iTextWidth: Integer;

rNum: Double;

begin

inherited;

with sgrdQty do

begin

sText := Cells[ACol, ARow]; //輸出內容

iTextWidth := canvas.TextWidth(sText); //輸出內容寬度

//

if ARow = 0 then

Canvas.TextRect(Rect, (Rect.left+Rect.right-iTextWidth) div 2, Rect.Top+2, sText) //第1行, 居中

else if ACol = 0 then

Canvas.TextRect(Rect, Rect.Left+2, Rect.top+2, sText) //第1列, 左對齊

else

begin

//如果<0以紅色顯示

if TryStrToFloat(sText, rNum) and (rNum < 0) then

Canvas.Font.Color := clRed

else

Canvas.Font.Color := clWindowText;

//

Canvas.TextRect(Rect, Rect.right - iTextWidth - 2 ,Rect.Top + 2, sText); //其它, 右對齊心

end;

end;

end;

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: