我有一个TNewStaticText数据类型的变量,我想为文本设置不同的颜色,例如 如果变量包含“有美好的一天”等文本
“Have”应显示为红色 “一个好的”应该以蓝色显示 “day”应以黑色显示
答案 0 :(得分:3)
您可以使用Font.Color
属性更改字体颜色,但它适用于整个控件的文本。为了您的目标,您需要创建三个控件。这是一个例子:
[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program
[Code]
procedure InitializeWizard;
var
TopPos: Integer;
LeftPos: Integer;
StaticText: TNewStaticText;
begin
TopPos := WizardForm.NextButton.Top;
LeftPos := 8;
StaticText := TNewStaticText.Create(WizardForm);
StaticText.Parent := WizardForm;
StaticText.Left := LeftPos;
StaticText.Top := TopPos;
StaticText.Font.Color := clRed;
StaticText.Caption := 'Have ';
LeftPos := StaticText.Left + StaticText.Width;
StaticText := TNewStaticText.Create(WizardForm);
StaticText.Parent := WizardForm;
StaticText.Left := LeftPos;
StaticText.Top := TopPos;
StaticText.Font.Color := clBlue;
StaticText.Caption := 'a nice ';
LeftPos := StaticText.Left + StaticText.Width;
StaticText := TNewStaticText.Create(WizardForm);
StaticText.Parent := WizardForm;
StaticText.Left := LeftPos;
StaticText.Top := TopPos;
StaticText.Font.Color := clBlack;
StaticText.Caption := 'day!';
end;
或者,为了使您的代码更加灵活,您可以将此过程包装到如下函数中:
[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program
[Code]
type
TColorArray = array of TColor;
procedure CreateColoredText(const ATexts: TArrayOfString; const AColors: TColorArray;
AParent: TWinControl; ALeft, ATop: Integer);
var
I: Integer;
LeftPos: Integer;
begin
if GetArrayLength(ATexts) <> GetArrayLength(AColors) then
RaiseException('Inconsistent array parameters.');
LeftPos := ALeft;
for I := 0 to GetArrayLength(ATexts) - 1 do
begin
with TNewStaticText.Create(AParent) do
begin
Parent := AParent;
Left := LeftPos;
Top := ATop;
Font.Color := AColors[I];
Caption := ATexts[I];
LeftPos := Left + Width;
end;
end;
end;
procedure InitializeWizard;
begin
CreateColoredText(['Have ', 'a nice ', 'day!'], [clRed, clBlue, clBlack],
WizardForm, 8, WizardForm.NextButton.Top);
end;
答案 1 :(得分:-2)
你可能想尝试这样的事情:
procedure FormButtonOnClick(Sender: TObject);
var
Form: TSetupForm;
OKButton, CancelButton: TNewButton;
begin
Form := CreateCustomForm();
try
Form.ClientWidth := ScaleX(256);
Form.ClientHeight := ScaleY(256);
Form.Caption := 'TSetupForm';
Form.CenterInsideControl(WizardForm, False);
OKButton := TNewButton.Create(Form);
OKButton.Parent := Form;
OKButton.Width := ScaleX(75);
OKButton.Height := ScaleY(23);
OKButton.Left := Form.ClientWidth - ScaleX(75 + 6 + 75 + 10);
OKButton.Top := Form.ClientHeight - ScaleY(23 + 10);
OKButton.Caption := 'OK';
OKButton.ModalResult := mrOk;
CancelButton := TNewButton.Create(Form);
CancelButton.Parent := Form;
CancelButton.Width := ScaleX(75);
CancelButton.Height := ScaleY(23);
CancelButton.Left := Form.ClientWidth - ScaleX(75 + 10);
CancelButton.Top := Form.ClientHeight - ScaleY(23 + 10);
CancelButton.Caption := 'Cancel';
CancelButton.ModalResult := mrCancel;
CancelButton.Cancel := True;
Form.ActiveControl := OKButton;
if Form.ShowModal() = mrOk then
MsgBox('You clicked OK.', mbInformation, MB_OK);
finally
Form.Free();
end;
end;
此示例是从.htm“&gt; http://read.pudn.com/downloads115/sourcecode/windows/system/488914/Examples/CodeClasses.iss_.htm
复制的