How to create custom message box similar to this code: Inno Setup - Hide X button (close) at message box (for a message box with one button) but with two buttons, yes/no, with the possibility of different actions for each election.
Just add the second TButton:
function MyYesNoMessageBox: Integer;
var
Form: TSetupForm;
YesButton, NoButton: TNewButton;
MesssageLabel: TLabel;
begin
Form := CreateCustomForm;
Form.BorderStyle := bsDialog;
Form.Position := poOwnerFormCenter;
Form.ClientWidth := ScaleX(400);
Form.ClientHeight := ScaleY(130);
Form.Caption := 'Caption';
MesssageLabel := TLabel.Create(Form);
MesssageLabel.Parent := Form;
MesssageLabel.Left := ScaleX(16);
MesssageLabel.Top := ScaleX(16);
MesssageLabel.Width := Form.ClientWidth - 2*ScaleX(16);
MesssageLabel.Height := ScaleY(32);
MesssageLabel.AutoSize := False;
MesssageLabel.WordWrap := True;
MesssageLabel.Caption := 'Lorem ipsum dolor sit amet, ...';
YesButton := TNewButton.Create(Form);
YesButton.Parent := Form;
YesButton.Width := ScaleX(80);
YesButton.Height := ScaleY(24);
YesButton.Left := Form.ClientWidth - 2 * (YesButton.Width + ScaleX(8));
YesButton.Top := Form.ClientHeight - YesButton.Height - ScaleY(8);
YesButton.Caption := '&Yes';
YesButton.ModalResult := mrYes;
NoButton := TNewButton.Create(Form);
NoButton.Parent := Form;
NoButton.Width := YesButton.Width;
NoButton.Height := YesButton.Height;
NoButton.Left := YesButton.Left + YesButton.Width + ScaleX(8);
NoButton.Top := YesButton.Top;
NoButton.Caption := '&No';
NoButton.ModalResult := mrNo;
Result := Form.ShowModal;
end;
And test return code of Form.ShowModal (or check Form.ModalResult):
if MyYesNoMessageBox = mrYes then
begin
MsgBox('Yes selected', mbInformation, MB_OK);
end
else
begin
MsgBox('No selected', mbInformation, MB_OK);
end;
Related
I got a code from List all physical printers using WMI query in Inno Setup and I want to add the results to a list box. I have tried to do it before asking, but I just can't add all items. This is an my code:
var
Query, AllPrinters: string;
WbemLocator, WbemServices, WbemObjectSet: Variant;
Printer: Variant;
I: Integer;
begin
WbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
WbemServices := WbemLocator.ConnectServer('.', 'root\CIMV2');
Query := 'SELECT Name FROM Win32_Printer';
WbemObjectSet := WbemServices.ExecQuery(Query);
if not VarIsNull(WbemObjectSet) and (WbemObjectSet.Count > 0) then
begin
for I := 0 to WbemObjectSet.Count - 1 do
begin
Printer := WbemObjectSet.ItemIndex(I);
if not VarIsNull(Printer) then
begin
Log(Printer.Name);
AllPrinters := Printer.Name;
end;
end;
end;
end;
Then on a custom page do this:
ListBoxPrinters.Items.Add(AllPrinters);
You add the items (printers) to the list box the same way, the original code adds them to the log: in the loop!
for I := 0 to WbemObjectSet.Count - 1 do
begin
Printer := WbemObjectSet.ItemIndex(I);
if not VarIsNull(Printer) then
begin
ListBoxPrinters.Items.Add(Printer.Name);
end;
end;
Of course, you have to create the custom page with the ListBoxPrinters before iterating the printers.
If you cannot run the query after creating the page for whatever reason, you can store a printer list into TStringList.
var
Printers: TStringList;
Printers := TStringList.Create;
for I := 0 to WbemObjectSet.Count - 1 do
begin
Printer := WbemObjectSet.ItemIndex(I);
if not VarIsNull(Printer) then
begin
Printers.Add(Printer.Name);
end;
end;
And once you have the list box ready, you just copy the list over to the box:
ListBoxPrinters.Items.Assign(Printers);
You overwrite always with the next AllPrinters := Printer.Name; the previous value !
simple build the AllPrinters string like that
....
AllPrinters := '';
....
for I := 0 to WbemObjectSet.Count - 1 do
begin
Printer := WbemObjectSet.ItemIndex(I);
if not VarIsNull(Printer) then
begin
Log(Printer.Name);
AllPrinters := AllPrinters + Printer.Name + #13#10;
end;
end;
end;
and
ListBoxPrinters.Items.Text := AllPrinters;
How to hide X button at message box?
I want to see this, if is possible:
I do not think this is possible.
One possible workaround is to implement the message box from the scratch.
And remove the biSystemMenu from the TForm.BorderIcons (or actually setting it empty).
procedure MyMessageBoxWithoutCloseButton;
var
Form: TSetupForm;
Button: TNewButton;
MesssageLabel: TLabel;
begin
Form := CreateCustomForm;
Form.BorderStyle := bsDialog;
Form.Position := poOwnerFormCenter;
Form.ClientWidth := ScaleX(400);
Form.ClientHeight := ScaleY(130);
Form.BorderIcons := []; { No close button }
Form.Caption := 'Caption';
MesssageLabel := TLabel.Create(Form);
MesssageLabel.Parent := Form;
MesssageLabel.Left := ScaleX(16);
MesssageLabel.Top := ScaleX(16);
MesssageLabel.Width := Form.ClientWidth - 2*ScaleX(16);
MesssageLabel.Height := ScaleY(32);
MesssageLabel.AutoSize := False;
MesssageLabel.WordWrap := True;
MesssageLabel.Caption := 'Lorem ipsum dolor sit amet, ...';
Button := TNewButton.Create(Form);
Button.Parent := Form;
Button.Width := ScaleX(80);
Button.Height := ScaleY(24);
Button.Left := Form.ClientWidth - Button.Width - ScaleX(8);
Button.Top := Form.ClientHeight - Button.Height - ScaleY(8);
Button.Caption := 'Accept';
Button.ModalResult := mrOK;
Form.ShowModal;
end;
Note that it's still possible to close the message box using Alt-F4.
To prevent that handle OnCloseQuery. For an example, see How to Delete / Hide / Disable [OK] button on message box.
I want to use custom uninstall wizard pages as in my Inno Setup script shown in question Custom Uninstall page (not MsgBox).
The problem I have is that the Caption of the TSetupForm does not update, when a page changes.
I tried using the following code.
[Code]
const
ControlGap = 5;
procedure UpdateButtonsState(Form: TSetupForm);
var
Notebook: TNewNotebook;
BtnBack, BtnNext: TButton;
begin
Notebook := TNewNotebook(Form.FindComponent('Notebook'));
BtnBack := TButton(Form.FindComponent('BtnBack'));
BtnNext := TButton(Form.FindComponent('BtnNext'));
BtnBack.Enabled := (Notebook.ActivePage <> Notebook.Pages[0]);
if Notebook.ActivePage <> Notebook.Pages[Notebook.PageCount - 1] then
begin
BtnNext.Caption := SetupMessage(msgButtonNext)
BtnNext.ModalResult := mrNone;
end
else
begin
BtnNext.Caption := SetupMessage(msgButtonFinish);
BtnNext.ModalResult := mrYes;
end;
end;
procedure BtnPageChangeClick(Sender: TObject);
var
NextPage: TNewNotebookPage;
Notebook: TNewNotebook;
Form: TWinControl;
Button, BtnBack, BtnNext: TButton;
begin
Button := TButton(Sender);
Form := Button;
while not (Form is TSetupForm) do
Form := Form.Parent;
Notebook := TNewNotebook(Form.FindComponent('Notebook'));
BtnBack := TButton(Form.FindComponent('BtnBack'));
BtnNext := TButton(Form.FindComponent('BtnNext'));
if (Button = BtnBack) and (Notebook.ActivePage = Notebook.Pages[0]) then
NextPage := nil
else
if (Button = BtnNext) and (Notebook.ActivePage = Notebook.Pages[Notebook.PageCount - 1]) then
NextPage := nil
else
NextPage := Notebook.FindNextPage(Notebook.ActivePage, Button = BtnNext);
Notebook.ActivePage := NextPage;
UpdateButtonsState(TSetupForm(Form));
end;
function AddPage(NotebookForm: TSetupForm): TNewNotebookPage;
var
Notebook: TNewNotebook;
begin
Notebook := TNewNotebook(NotebookForm.FindComponent('Notebook'));
Result := TNewNotebookPage.Create(Notebook);
Result.Notebook:=Notebook;
Result.Parent:=Notebook;
Result.Align := alClient;
if Notebook.ActivePage = nil then
Notebook.ActivePage := Result;
UpdateButtonsState(NotebookForm);
end;
function CreateNotebookForm: TSetupForm;
var
Notebook: TNewNotebook;
NotebookPage: TNewNotebookPage;
Pan: TPanel;
TmpLabel: TLabel;
MaxWidth, i: Integer;
BtnBack, BtnNext, BtnCancel: TButton;
BtnLabelMsgIDs: Array Of TSetupMessageID;
begin
Result := CreateCustomForm;
Result.SetBounds(0, 0, UninstallProgressForm.Width, UninstallProgressForm.Height);
Result.Position := poOwnerFormCenter;
Notebook := TNewNotebook.Create(Result);
Notebook.Parent := Result;
Notebook.Name := 'Notebook';
Notebook.Align := alClient;
Pan := TPanel.Create(Result);
Pan.Parent := Notebook;
Pan.Caption := '';
Pan.Align := alBottom;
BtnNext := TNewButton.Create(Result);
with BtnNext do
begin
Parent := Pan;
Name := 'BtnNext';
Caption := SetupMessage(msgButtonNext);
OnClick := #BtnPageChangeClick;
ParentFont := True;
end;
BtnBack := TNewButton.Create(Result);
with BtnBack do
begin
Parent := Pan;
Caption := SetupMessage(msgButtonBack);
Name := 'BtnBack';
OnClick := #BtnPageChangeClick;
ParentFont := True;
end;
BtnCancel := TNewButton.Create(Result);
with BtnCancel do
begin
Parent := Pan;
Name := 'BtnCancel';
Caption := SetupMessage(msgButtonCancel);
ModalResult := mrCancel;
Cancel := True;
ParentFont := True;
end;
TmpLabel := TLabel.Create(Result);
with TmpLabel do
begin
Left := 0;
Top := 0;
Parent := Pan;
ParentFont := True;
Visible := False;
WordWrap := False;
Autosize := True;
end;
SetArrayLength(BtnLabelMsgIDs, 4);
BtnLabelMsgIDs[0] := msgButtonBack;
BtnLabelMsgIDs[1] := msgButtonNext;
BtnLabelMsgIDs[2] := msgButtonCancel;
BtnLabelMsgIDs[3] := msgButtonFinish;
MaxWidth := 0;
for i := Low(BtnLabelMsgIDs) to High(BtnLabelMsgIDs) do
begin
TmpLabel.Caption := SetupMessage(BtnLabelMsgIDs[i]) + 'WWW';
if MaxWidth < TmpLabel.Width then
MaxWidth := TmpLabel.Width;
end;
TmpLabel.Caption := 'Yy';
with BtnBack do
begin
Width := MaxWidth;
Height := TmpLabel.Height*2;
Left := Parent.ClientWidth - 3*(MaxWidth + ScaleX(ControlGap));
Top := (Parent.ClientHeight - Height) div 2;
end;
with BtnNext do
begin
Width := MaxWidth;
Height := TmpLabel.Height*2;
Left := Parent.ClientWidth - 2*(MaxWidth + ScaleX(ControlGap));
Top := (Parent.ClientHeight - Height) div 2;
end;
with BtnCancel do
begin
Width := MaxWidth;
Height := TmpLabel.Height*2;
Left := Parent.ClientWidth - 1*(MaxWidth + ScaleX(ControlGap));
Top := (Parent.ClientHeight - Height) div 2;
end;
end;
procedure InitializeUninstallProgressForm;
var
Form: TSetupForm;
i: Integer;
NotebookPage: TNewNotebookPage;
ModResult: Integer;
begin
Form := CreateNotebookForm;
for i := 1 to 4 do
begin
NotebookPage := AddPage(Form);
with NotebookPage do
begin
Color := clWindow;
with TLabel.Create(Form) do
begin
Parent := NotebookPage;
SetBounds(0, 0, 50, 30);
Autosize := true;
Font.Size := 14;
Caption := 'Label ' + IntToStr(i);
end;
Form.Caption := 'CAPTION - ' + IntToStr(i);
{<<<NEVER UPDATES AND KEEPS SHOWING "CAPTION - 4">>>>}
end;
end;
ModResult := Form.ShowModal;
if ModResult = mrYes then
MsgBox('Continuing uninstall', mbInformation, MB_OK)
else
begin
MsgBox('Cancelled', mbInformation, MB_OK);
Abort;
end;
end;
Please help me to find why Caption of the TSetupForm (Here it is declared as Form) never updates.
Thanks in advance.
Of course, it doesn't. You have to update the form caption as the page changes.
A good place for this is the end of the UpdateButtonsState function:
procedure UpdateButtonsState(Form: TSetupForm);
{ ... }
begin
{ ... }
if Notebook.ActivePage <> nil then
Form.Caption := 'CAPTION - ' + IntToStr(Notebook.ActivePage.PageIndex + 1);
end;
I'm trying to create a custom component window. So far i have found some code here on stackoverflow that lets me set the outerwindow height of the components window. The problem i have is that the actual component selection box doesnt grow with it, like i have a big window but still a small component selection box.
Picture of my problem:
Hope someone can help me with this.
The code i have is this
var
DefaultTop,
DefaultLeft,
DefaultHeight,
DefaultBackTop,
DefaultNextTop,
DefaultCancelTop,
DefaultBevelTop,
DefaultOuterHeight: Integer;
const
LicenseHeight = 600;
LicenseWidth = 600;
procedure InitializeWizard();
begin
DefaultTop := WizardForm.Top;
DefaultLeft := WizardForm.Left;
DefaultHeight := WizardForm.Height;
DefaultBackTop := WizardForm.BackButton.Top;
DefaultNextTop := WizardForm.NextButton.Top;
DefaultCancelTop := WizardForm.CancelButton.Top;
DefaultBevelTop := WizardForm.Bevel.Top;
end;
procedure CurPageChanged(CurPageID: Integer);
begin
if CurPageID = wpSelectComponents then
begin
WizardForm.Top := DefaultTop - (LicenseHeight - DefaultHeight) div 2;
WizardForm.Height := LicenseHeight;
WizardForm.Width := LicenceWidth;
WizardForm.OuterNotebook.Height := WizardForm.OuterNotebook.Height + (LicenseHeight - DefaultHeight);
WizardForm.CancelButton.Top := DefaultCancelTop + (LicenseHeight - DefaultHeight);
WizardForm.NextButton.Top := DefaultNextTop + (LicenseHeight - DefaultHeight);
WizardForm.BackButton.Top := DefaultBackTop + (LicenseHeight - DefaultHeight);
WizardForm.Bevel.Top := DefaultBevelTop + (LicenseHeight - DefaultHeight);
end
else
begin
WizardForm.Top := DefaultTop;
WizardForm.Left := DefaultLeft;
WizardForm.Height := DefaultHeight;
WizardForm.OuterNotebook.Height := DefaultOuterHeight;
WizardForm.CancelButton.Top := DefaultCancelTop;
WizardForm.NextButton.Top := DefaultNextTop;
WizardForm.BackButton.Top := DefaultBackTop;
WizardForm.Bevel.Top := DefaultBevelTop;
end;
end;
I have a variable of TNewStaticText datatype and i want to set different colors for the text, For example
if the the variable contains text like "Have a nice day"
"Have" should be displayed in red color
"a nice" should be displayed in blue color
"day" should be displayed in black color
Font color you can change with the Font.Color property, but it's for the whole control's text. For your aim you need to create three controls. Here is an example:
[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;
Or, to make your code more flexible, you can wrap this process to a function like follows:
[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;
You might want to try something like this:
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;
This example is copied from .htm">http://read.pudn.com/downloads115/sourcecode/windows/system/488914/Examples/CodeClasses.iss_.htm