Delphi 如何实现某种形式的表单管理器? 我正处于一个有许多子窗体的项目中。许多表单可能会同时打开。我想知道是否已经有一些东西可以用来管理和跟踪这些表单,就像windows任务栏和/或任务管理器一样。如果不是,那么最好的方法是什么?如果这已经完成了,我不想重新发明轮子
说明 如上所述,该项目有多种形式,可以一次打开。我还将实现一些可视化列表控件(很像任务栏或任务管理器),用于用户控制这些表单(或者在用户的情况下,这些表单称为windows)。管理这些表单最理想的方法是在创建表单时首先捕获每个表单,并在某处记录它们。有些表单需要这种行为,有些表单则不需要。例如,模态表单永远不需要这种处理 我将为用户提供显示、最小化或关闭这些表单的访问权限,以及其他一些未经思考的处理,比如与其中一个表单相关联的自定义弹出菜单(但这是另一个主题)。关键是,我需要构建一些东西来捕获这些表单并保持它们的有序 这还将包括一些其他用户与所有表单的交互,以及对每个表单的简单访问,类似于Screen.forms的工作方式。例如,用于最小化所有表单(Delphi 如何实现某种形式的表单管理器? 我正处于一个有许多子窗体的项目中。许多表单可能会同时打开。我想知道是否已经有一些东西可以用来管理和跟踪这些表单,就像windows任务栏和/或任务管理器一样。如果不是,那么最好的方法是什么?如果这已经完成了,我不想重新发明轮子,delphi,delphi-7,tform,Delphi,Delphi 7,Tform,说明 如上所述,该项目有多种形式,可以一次打开。我还将实现一些可视化列表控件(很像任务栏或任务管理器),用于用户控制这些表单(或者在用户的情况下,这些表单称为windows)。管理这些表单最理想的方法是在创建表单时首先捕获每个表单,并在某处记录它们。有些表单需要这种行为,有些表单则不需要。例如,模态表单永远不需要这种处理 我将为用户提供显示、最小化或关闭这些表单的访问权限,以及其他一些未经思考的处理,比如与其中一个表单相关联的自定义弹出菜单(但这是另一个主题)。关键是,我需要构建一些东西来捕获这
FormManager.MinimizeAll
),最大化当前活动表单(FormManager.ActiveForm.maximize
),或使用特定表单(FormManager[3]。最大化
)的命令
可能的选项
我知道有几种不同的方法可以实现类似的结果,但还没有开始编码,因为每种方法都有不同的起点。选项是
- 从屏幕包装Screen.Forms和其他相关功能(这不允许我有太多的灵活性)
- 每次我创建表单时,请向表单管理器注册它(这非常灵活,但我必须确保始终注册每个创建的表单)
- 构建一个主窗体,以便向窗体管理器注册它自己,并从中继承所有内容(这也是非常灵活的,但方式不同,而且要复杂得多)
第二种选择听起来是迄今为止最有希望的。但再一次,如果已经有了解决方案,我不想开始构建它。我很有信心,我不是第一个这样做的人。我不知道如何搜索这样的东西,我在谷歌上找不到与我想要的东西相关的东西。如果我理解正确,你想在应用程序运行时在代码中跟踪吗?
也许你可以用Screen.Forms做些什么?全局变量
Screen
(在Forms
单元中)进行一些“跟踪”,即
- 列出所有当前打开的表格李>
- 具有输入焦点的表单(另请参见)李>
- 事件李>
unit uFormstack;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Contnrs;
type
TFormstack = class(TComponent)
private
{ Private declarations }
FormList: TObjectList;
protected
{ Protected declarations }
public
{ Public declarations }
Constructor Create(AOwner: TComponent); Override;
Destructor Destroy; Override;
Procedure Add(InstanceClass: TComponentClass; Var Reference);
Procedure RemoveLast;
Procedure RemoveAll;
Function FindForm(AComponentClass: TComponentClass): Boolean;
Function GetForm(AComponentClass: TComponentClass): TObject;
Function GetByIndex(AIndex: Integer): TObject;
Procedure RemoveByIndex(AIndex: Integer);
published
{ Published declarations }
end;
procedure Register;
implementation
//{$R *.res}
procedure Register;
begin
RegisterComponents('FormStack', [TFormstack]);
end;
{-----------------------------------------------------------------------------
TFormStack
-----------------------------------------------------------------------------}
Constructor TFormStack.Create(AOwner: TComponent);
Begin
Inherited Create(AOwner);
FormList := TObjectList.Create;
FormList.OwnsObjects := True;
End;
Destructor TFormStack.Destroy;
Begin
FormList.Free;
Inherited Destroy;
End;
Procedure TFormStack.Add(InstanceClass: TComponentClass; Var Reference);
Var
Instance: TComponent;
Begin
Instance := TComponent(InstanceClass.NewInstance);
TComponent(Reference) := Instance;
Instance.Create(Self); // Owner is FormList <<-- blows up if datamodule in D2010
FormList.Add(Instance);
Instance.Tag := FormList.Count-1;
End;
Procedure TFormStack.RemoveAll;
Var
I: Integer;
Begin
For I := FormList.Count -1 downto 0 do // last in first out
begin
Self.RemoveLast;
End;
End;
// This removes the last form on the stack
Procedure TFormStack.RemoveLast;
Begin
if FormList.Count > 0 then
FormList.Remove(FormList.Items[FormList.Count-1]);
End;
Function TFormStack.FindForm(AComponentClass: TComponentClass): Boolean;
Var
I: Integer;
Begin
Result := False;
For I := FormList.Count-1 downto 0 do
If Formlist.Items[I].ClassType = AComponentClass then
Result := True;
End;
Function TFormStack.GetForm(AComponentClass: TComponentClass): TObject;
Var
I: Integer;
begin
Result := Nil;
For I := FormList.Count-1 downto 0 do
If Formlist.Items[I].ClassType = AComponentClass then
Result := FormList.Items[I];
end;
Function TFormStack.GetByIndex(AIndex: Integer): TObject;
begin
Result := Nil;
If FormList.Count-1 >= AIndex then
Result := FormList.Items[AIndex];
end;
Procedure TFormStack.RemoveByIndex(AIndex: Integer);
begin
If FormList.Count-1 >= AIndex then
FormList.Remove(FormList.Items[AIndex]);
end;
end.
单位uFormstack;
界面
使用
窗口、消息、系统工具、变体、类、图形、控件、窗体、,
对话框、StdCtrls、Contnrs;
类型
TFormstack=类别(t组件)
私有的
{私有声明}
FormList:TObjectList;
受保护的
{受保护的声明}
平民的
{公开声明}
构造函数创建(AOwner:TComponent);推翻
毁灭者毁灭;推翻
过程添加(InstanceClass:TComponentClass;Var引用);
手术切除;
程序移除;
函数FindForm(acComponentClass:TComponentClass):布尔值;
函数GetForm(acComponentClass:TComponentClass):toObject;
函数GetByIndex(AIndex:Integer):TObject;
过程RemoveByIndex(AIndex:整数);
出版
{已发布声明}
终止
程序登记册;
实施
//{$R*.res}
程序登记册;
开始
注册表组件('FormStack',[TFormstack]);
终止
{-----------------------------------------------------------------------------
TFormStack
-----------------------------------------------------------------------------}
构造函数TFormStack.Create(所有者:TComponent);
开始
继承的创建(AOOwner);
FormList:=TObjectList.Create;
FormList.OwnsObjects:=True;
终止
破坏器TFormStack.破坏;
开始
表单列表。免费;
继承性破坏;
终止
过程TFormStack.Add(InstanceClass:TComponentClass;变量引用);
变量
实例:TComponent;
开始
实例:=TComponent(InstanceClass.NewInstance);
TComponent(引用):=实例;
实例。创建(自身);//所有者是FormList=AIndex,然后
结果:=FormList.Items[AIndex];
终止
过程TFormStack.RemoveByIndex(AIndex:Integer);
开始
如果FormList.Count-1>=AIndex,则
FormList.Remove(FormList.Items[AIndex]);
终止
终止
定义“管理并跟踪”。如果你的意思是“知道一个表单存在,知道它的标题和类名”,那么屏幕。表单
或应用程序。表单
就是你想要的,正如艾因所说的那样。@WarrenP问题修改了更多细节。干得好Jerry+1.我认为添加自己的主类没有任何好处,包装Screen.Forms也没有任何好处。根据您的要求,Screen.Forms已经很完美了。您不需要最小化所有表单,只需执行应用程序即可。最小化所有表单并将其最小化。@好的,所有这些表单都包含在主表单中,作为它们的父表单。实际上,它很像一个包含的操作系统。我有很多的需要去做这种类型的控制