!!! 难题一个
要在窗体界面上显示比分(我用的 TLabel),如下面四种,每个比分用“:” 分割:
8:11
123:23
22:2
8:222
问题是每个比分需要按照“:”对其,即要求如下显示:
8:11
123:23
22:2
8:222
每个比分长度不固定,这个算法该怎么写好呢?
[解决办法]
放三个 TLabel显示一行设置他们的Alignment属性分别为taRightJustify, taCenter,taLeftJustify
[解决办法]
下班前作了个例子:
新建一个工程,窗体的下半部分放1个Memo,2个Button和1个FontDialog,Memo的Lines里面复制你的例子,然后单元文件里粘贴以下代码:
unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, Contnrs;type TForm1 = class(TForm) Memo1: TMemo; Button1: TButton; Button2: TButton; FontDialog1: TFontDialog; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); private { Private declarations } FLabelList: TObjectList; public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject); function GetMax(Lines: TStrings):Integer; var i,j: integer; s: string; begin Result := 0; for i := 0 to Lines.Count - 1 do begin s := Copy(Lines.Strings[i],0,Pos(':',Lines.Strings[i]) - 1); j := StrToInt(s); if j > Result then Result := j; end; end;var i, j, k, iMax: Integer; Img: TImage; s: string; Lb: TLabel;begin FLabelList.Clear; iMax := GetMax(Memo1.Lines); Img := TImage.Create(Self); with Img.Canvas do begin try Img.Visible := False; Font.Assign(Memo1.Font); iMax := TextWidth(IntToStr(iMax)); for i := 0 to Memo1.Lines.Count - 1 do begin s := Copy(Memo1.Lines.Strings[i],0,Pos(':',Memo1.Lines.Strings[i]) - 1); j := TextWidth(s); k := Memo1.Left; lb := TLabel.Create(Self); with lb do begin Font := Memo1.Font; Width := 200; Visible := True; Name := 'Label' + IntToStr(i); Caption := Memo1.Lines.Strings[i]; Left := k + iMax - j; Top := i * (Height + 2) + 30; Parent := Self; FLabelList.Add(lb); end; end; finally Img.Free; end; end;end;procedure TForm1.Button2Click(Sender: TObject);begin if FontDialog1.Execute then begin Memo1.Font.Assign(FontDialog1.Font); end;end;procedure TForm1.FormCreate(Sender: TObject);begin FLabelList := TObjectList.Create(True);end;procedure TForm1.FormDestroy(Sender: TObject);begin FLabelList.Free;end;end.
[解决办法]
简单问题复杂化,1楼 失踪的月亮 的方法已经可以解决这个问题,一行三个标签,四行12个标签就解决问题。其中,每行的中间那个标签是显示 “:”。
如:8:11
显示8的标签对其方式采用右对齐;
显示“:”对齐方式无所谓;
显示11的对齐方式采用左对齐