Delphi:如何制作单元格';TStringGrid中的文本是否对齐?
这似乎是显而易见的。我希望文本位于单元格的中心,但由于某些原因,我无法在属性中找到它。如何执行此操作?TStringGrid中没有将文本居中的属性,但您可以在DrawCell事件中执行此操作,如下所示:Delphi:如何制作单元格';TStringGrid中的文本是否对齐?,delphi,Delphi,这似乎是显而易见的。我希望文本位于单元格的中心,但由于某些原因,我无法在属性中找到它。如何执行此操作?TStringGrid中没有将文本居中的属性,但您可以在DrawCell事件中执行此操作,如下所示: procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); var S: string; SavedAlign: wor
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
S: string;
SavedAlign: word;
begin
if ACol = 1 then begin // ACol is zero based
S := StringGrid1.Cells[ACol, ARow]; // cell contents
SavedAlign := SetTextAlign(StringGrid1.Canvas.Handle, TA_CENTER);
StringGrid1.Canvas.TextRect(Rect,
Rect.Left + (Rect.Right - Rect.Left) div 2, Rect.Top + 2, S);
SetTextAlign(StringGrid1.Canvas.Handle, SavedAlign);
end;
end;
我发布的代码
更新:
要在单元格中写入时将文本居中,请将此代码添加到GetEditText
事件:
procedure TForm1.StringGrid1GetEditText(Sender: TObject; ACol, ARow: Integer;
var Value: string);
var
S : String;
I: Integer;
IE : TInplaceEdit ;
begin
for I := 0 to StringGrid1.ControlCount - 1 do
if StringGrid1.Controls[i].ClassName = 'TInplaceEdit' then
begin
IE := TInplaceEdit(StringGrid1.Controls[i]);
ie.Alignment := taCenter
end;
end;
这是一个比其他解决方案好得多的解决方案,在它们上面有一个错误的输入过程
TStringGrid.SetCellsAlignment
和TStringGrid.SetCellsAlignment
比较(-1
是正确的,但是然后
和其他
部分被交换了。。。正确的版本(这一个)将显示,当索引大于-1时,它将覆盖存储的值,否则它将添加一个新条目,其他人将只执行oposite操作,从索引消息中拿出一个列表,感谢您检测到这种情况
我还可以将所有内容都放在另一个单独的单元中,所以这里就是(希望现在它是正确的,感谢您检测到此类错误类型):
单元对齐网格;
接口
使用Windows、SysUtils、类和网格;
类型
TStringGrid=class(Grids.TStringGrid)
私有的
FCellsAlignment:TStringList;
FColsDefaultAlignment:TStringList;
函数GetCellsAlignment(ACol,ARow:Integer):TAlignment;
过程设置cellsalignment(ACol,ARow:Integer;常量对齐:TAlignment);
函数GetColsDefaultAlignment(ACol:Integer):TAlignment;
过程SetColsDefaultAlignment(ACol:Integer;const-Alignment:TAlignment);
受保护的
程序绘图单元(ACol,ARow:Longint;ARect:TRect;AState:TGridDrawState);推翻
公众的
构造函数创建(AOwner:TComponent);推翻
毁灭者毁灭;推翻
属性CellsAlignment[ACol,ARow:Integer]:t指定读取GetCellsAlignment写入SetCellsAlignment;
属性ColsDefaultAlignment[ACol:Integer]:t指定读取GetColsDefaultAlignment写入SetColsDefaultAlignment;
结束;
实施
构造函数TStringGrid.Create(AOwner:TComponent);
开始
继承的创建(AOOwner);
FCellsAlignment:=TStringList.Create;
FCellsAlignment.CaseSensitive:=True;
FCellsAlignment.Sorted:=True;
FCellsAlignment.Duplicates:=dupIgnore;
FColsDefaultAlignment:=TStringList.Create;
FColsDefaultAlignment.CaseSensitive:=True;
FColsDefaultAlignment.Sorted:=真;
FColsDefaultAlignment.Duplicates:=dupIgnore;
结束;
析构函数TStringGrid.Destroy;
开始
FCellsAlignment.Free;
FColsDefaultAlignment.Free;
继承性破坏;
结束;
过程TStringGrid.SetCellsAlignment(ACol,ARow:Integer;const-Alignment:TAlignment);
变量
索引:整数;
开始
如果(-1<索引),则开始
FCellsAlignment.Objects[Index]:=TObject(对齐);
结束,否则开始
AddObject(IntToStr(ACol)+'-'+IntToStr(ARow)、TObject(Alignment));
结束;
结束;
函数TStringGrid.GetCellsAlignment(ACol,ARow:Integer):TAlignment;
变量
索引:整数;
开始
索引:=FCellsAlignment.IndexOf(IntToStr(ACol)+'-'+IntToStr(ARow));
如果(-1<索引),则开始
GetCellsAlignment:=TAlignment(FCellsAlignment.Objects[Index]);
结束,否则开始
GetCellsAlignment:=ColsDefaultAlignment[ACol];
结束;
结束;
过程TStringGrid.SetColsDefaultAlignment(ACol:Integer;const-Alignment:TAlignment);
变量
索引:整数;
开始
索引:=FColsDefaultAlignment.IndexOf(IntToStr(ACol));
如果(-1<索引),则开始
对象[索引]:=TObject(对齐);
结束,否则开始
AddObject(IntToStr(ACol)、TObject(Alignment));
结束;
结束;
函数TStringGrid.GetColsDefaultAlignment(ACol:Integer):TAlignment;
变量
索引:整数;
开始
索引:=FColsDefaultAlignment.IndexOf(IntToStr(ACol));
如果(-1<索引),则开始
GetColsDefaultAlignment:=TAlignment(FColsDefaultAlignment.Objects[Index]);
结束,否则开始
GetColsDefaultAlignment:=taLeftJustify;
结束;
结束;
程序TStringGrid.DrawCell(ACol,ARow:Longint;ARect:TRect;AState:TGridDrawState);
变量
旧图形:布尔;
开始
如果默认,则开始绘制
案例单元销售[ACol,ARow]
taLeftJustify:开始
TextRect(ARect,ARect.Left+2,ARect.Top+2,单元格[ACol,ARow]);
结束;
开始
TextRect(ARect,ARect.Right-2-Canvas.TextWidth(单元格[ACol,ARow]),ARect.Top+2,单元格[ACol,ARow]);
结束;
塔中心:开始
TextRect(ARect,(ARect.Left+ARect.Right Canvas.TextWidth(Cells[ACol,ARow]))div 2,ARect.Top+2,Cells[ACol,ARow]);
结束;
结束;
结束;
Old_DefaultDrawing:=DefaultDrawing;
DefaultDrawing:=假;
遗传性牵引细胞(ACol、ARow、ARect、AState);
DefaultDrawing:=旧的\u DefaultDrawing;
结束;
结束。
这是一个完整的单元,请将其保存到名为AlignedTStringGrid.pas
的文件中
然后在任何表单上,在接口uses子句的末尾都有一个TStringGrid
add,AlignedTStringGrid
注意:行也可以这样做,但现在我不知道如何混合(cols和rows),因为如何选择优先级,如果有人对此非常感兴趣,请告诉我
p.D.:TEdit也有同样的想法,只需在stackoverflow.com上搜索
TEdit.CreateParams
或阅读post谢谢。因此,如果我希望它在用户在其单元格中写入时居中,那么我应该在onEdit()中执行此操作?上面的代码在OnDrawCell
事件中实现,应该保留在那里。如果您希望输入以用户中心的it为中心,那么您应该使用编辑器TEdit/TWhateverEdit的绘画事件来实现这一点。谢谢Mohammed。这是一个很好的实施方法。我想知道,在StringGrid这样的表中添加/编辑数据难道没有更好的组件吗?因为StringGrid的功能有限。您可以从www.bergsoft.net查看NextGrid或从
unit AlignedTStringGrid;
interface
uses Windows,SysUtils,Classes,Grids;
type
TStringGrid=class(Grids.TStringGrid)
private
FCellsAlignment:TStringList;
FColsDefaultAlignment:TStringList;
function GetCellsAlignment(ACol,ARow:Integer):TAlignment;
procedure SetCellsAlignment(ACol,ARow:Integer;const Alignment:TAlignment);
function GetColsDefaultAlignment(ACol:Integer):TAlignment;
procedure SetColsDefaultAlignment(ACol:Integer;const Alignment:TAlignment);
protected
procedure DrawCell(ACol,ARow:Longint;ARect:TRect;AState:TGridDrawState);override;
public
constructor Create(AOwner:TComponent);override;
destructor Destroy;override;
property CellsAlignment[ACol,ARow:Integer]:TAlignment read GetCellsAlignment write SetCellsAlignment;
property ColsDefaultAlignment[ACol:Integer]:TAlignment read GetColsDefaultAlignment write SetColsDefaultAlignment;
end;
implementation
constructor TStringGrid.Create(AOwner:TComponent);
begin
inherited Create(AOwner);
FCellsAlignment:=TStringList.Create;
FCellsAlignment.CaseSensitive:=True;
FCellsAlignment.Sorted:=True;
FCellsAlignment.Duplicates:=dupIgnore;
FColsDefaultAlignment:=TStringList.Create;
FColsDefaultAlignment.CaseSensitive:=True;
FColsDefaultAlignment.Sorted:=True;
FColsDefaultAlignment.Duplicates:=dupIgnore;
end;
destructor TStringGrid.Destroy;
begin
FCellsAlignment.Free;
FColsDefaultAlignment.Free;
inherited Destroy;
end;
procedure TStringGrid.SetCellsAlignment(ACol,ARow: Integer; const Alignment: TAlignment);
var
Index:Integer;
begin
if (-1 < Index) then begin
FCellsAlignment.Objects[Index]:= TObject(Alignment);
end else begin
FCellsAlignment.AddObject(IntToStr(ACol) + '-' + IntToStr(ARow), TObject(Alignment));
end;
end;
function TStringGrid.GetCellsAlignment(ACol,ARow: Integer): TAlignment;
var
Index:Integer;
begin
Index:= FCellsAlignment.IndexOf(IntToStr(ACol)+'-'+IntToStr(ARow));
if (-1 < Index) then begin
GetCellsAlignment:= TAlignment(FCellsAlignment.Objects[Index]);
end else begin
GetCellsAlignment:= ColsDefaultAlignment[ACol];
end;
end;
procedure TStringGrid.SetColsDefaultAlignment(ACol: Integer; const Alignment: TAlignment);
var
Index:Integer;
begin
Index:= FColsDefaultAlignment.IndexOf(IntToStr(ACol));
if (-1 < Index) then begin
FColsDefaultAlignment.Objects[Index]:= TObject(Alignment);
end else begin
FColsDefaultAlignment.AddObject(IntToStr(ACol), TObject(Alignment));
end;
end;
function TStringGrid.GetColsDefaultAlignment(ACol:Integer):TAlignment;
var
Index:Integer;
begin
Index:= FColsDefaultAlignment.IndexOf(IntToStr(ACol));
if (-1 < Index) then begin
GetColsDefaultAlignment:= TAlignment(FColsDefaultAlignment.Objects[Index]);
end else begin
GetColsDefaultAlignment:=taLeftJustify;
end;
end;
procedure TStringGrid.DrawCell(ACol,ARow:Longint;ARect:TRect;AState:TGridDrawState);
var
Old_DefaultDrawing:Boolean;
begin
if DefaultDrawing then begin
case CellsAlignment[ACol,ARow] of
taLeftJustify: begin
Canvas.TextRect(ARect,ARect.Left+2,ARect.Top+2,Cells[ACol,ARow]);
end;
taRightJustify: begin
Canvas.TextRect(ARect,ARect.Right -2 -Canvas.TextWidth(Cells[ACol,ARow]), ARect.Top+2,Cells[ACol,ARow]);
end;
taCenter: begin
Canvas.TextRect(ARect,(ARect.Left+ARect.Right-Canvas.TextWidth(Cells[ACol,ARow]))div 2,ARect.Top+2,Cells[ACol,ARow]);
end;
end;
end;
Old_DefaultDrawing:= DefaultDrawing;
DefaultDrawing:=False;
inherited DrawCell(ACol,ARow,ARect,AState);
DefaultDrawing:= Old_DefaultDrawing;
end;
end.