gpt4 book ai didi

delphi - 从 Delphi 包中的 mdi 子窗体访问主要应用程序变量

转载 作者:行者123 更新时间:2023-12-03 15:54:45 25 4
gpt4 key购买 nike

我正在开发一个 Delphi 7 应用程序,其中包含包中的 mdi 子表单。主应用程序中有一些变量(例如登录用户,...),我想从包表单中访问它们。我如何访问它们?

最佳答案

这个问题的关键似乎是如何访问定义的接口(interface)。
这里SysUtils.Supports ,如果支持的话,它将返回一个接口(interface),这可能是更好的方法。

示例实现可能如下所示:

主窗体中的声明

type

IMainFormProperties = interface
['{2F4913C6-09D6-472B-8D03-9A04B312B36C}']
function GetAProperty: string;
procedure SetAProperty(const Value:String);
property AProperty: string read GetAProperty Write SetAProperty;
end;


TMainForm = class(TForm,IMainFormProperties)
// ......
private
{ Private-Deklarationen }
FAProperty:String;
function GetAProperty: string;
procedure SetAProperty(const Value:String);

public
{ Public-Deklarationen }
property AProperty: string read GetAProperty Write SetAProperty;
end;

对于应访问 MainForm 的表单

type
IMainFormProperties = interface
['{2F4913C6-09D6-472B-8D03-9A04B312B36C}']
function GetAProperty: string;
procedure SetAProperty(const Value:String);
property AProperty: string read GetAProperty write SetAProperty;
end;


TMDIChild = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private-Deklarationen }
public

end;

var
MDIChild: TMDIChild;

implementation

{$R *.dfm}

procedure TMDIChild.Button1Click(Sender: TObject);

var
cnt : integer;
iApp : IMainFormProperties;
begin
begin
if Supports(Application.Mainform, IMainFormProperties, iApp) then
begin
Showmessage(iApp.AProperty); // show existing value
iApp.AProperty := Edit1.Text;// change existing value
end;
end;
end;

相关文章可以在这里找到:Interfaces in Delphi Programming 101

关于delphi - 从 Delphi 包中的 mdi 子窗体访问主要应用程序变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20343787/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com