2007. november 23., péntek
Managing MDI forms
Problem/Question/Abstract:
Manage MDI forms. Each form will be visble only once. Form will be created if needed.
Answer:
{
This article describes a base class for your mainform.
(You can inherite from this form or use it as is)
Three new routines are presented. They will enable you to easily
manage MDI forms:
- Only one instance per MDI Class will be activated
- Creation of MDI class will be handled by routines if needed
- Activated MDI class will be focused if needed
}
type
TMDIClass = class of TForm;
type
TBaseMainForm = class(TForm)
{... }
public
{ Public declarations }
function ActivateMDIClass(MDIClass: TMDIClass): TForm;
function GetMDIClassIndex(MDIClass: TMDIClass): Integer;
function MDIClassIsActive(MDIClass: TMDIClass): Boolean;
{ ... }
end;
implementation
{
Use ActivateMDIClass() to activate a mdi child class.
If the class is not created yet, it will be.
The mdi child will be shown on screen and focused.
}
function TBaseMainForm.ActivateMDIClass(MDIClass: TMDIClass): TForm;
var
i: Integer;
begin
// Try to find index of MDIClass form in MDI child list
i := GetMDIClassIndex(MDIClass);
// if index is not found (-1) then create the form
if i = -1 then
Result := MDIClass.Create(Application)
else
Result := MDIChildren[i];
// bring it to front
Result.Show;
Result.BringToFront;
end;
{
Get mdi child index of specified MDIClass.
Returns -1 if the MDIClass does not exist as a created MDI form
}
function TBaseMainForm.GetMDIClassIndex(
MDIClass: TMDIClass): Integer;
var
i: Integer;
begin
// Default index -1 = MDIClass not found
Result := -1;
// try to find a MDI child of correct MDIClass class
for i := 0 to MDIChildCount - 1 do
if MDIChildren[i].ClassType = MDIClass then
Result := i;
end;
{
Returns true is the MDIClass exists as a created MDI form
}
function TBaseMainForm.MDIClassIsActive(
MDIClass: TMDIClass): Boolean;
begin
Result := GetMDIClassIndex(MDIClass) <> -1;
end;
Usage Example
Create a mainform, inherited from TBaseMainForm.
Create two mdi forms called TfrmBrainstorm and TfrmReport.
Make sure ...FormStyle=fsMDIChild.
Make sure MDI childs can be closed:
procedure...FormClose(Sender: TObject;
var Action: TCloseAction);
begin
Action := caFree;
end;
Now use the following code to activate those mdi forms:
procedure TMainForm.OnClick1(Sender: TObject);
begin
ActivateMDIClass(TfrmBrainstorm);
end;
procedure TMainForm.OnClick2(Sender: TObject);
begin
ActivateMDIClass(TfrmReport);
end;
Feliratkozás:
Megjegyzések küldése (Atom)
Nincsenek megjegyzések:
Megjegyzés küldése