gpt4 book ai didi

Delphi 界面帮助程序/解决方法

转载 作者:行者123 更新时间:2023-12-03 14:40:30 26 4
gpt4 key购买 nike

我意识到Delphi不支持接口(interface)助手,但是在阅读了Spring4D的几个SO主题和来源等之后,我想知道是否有任何方法可以实现以下目标?源代码注释几乎总结了我想要做的事情,所以这里是:

program IHelper;

{$APPTYPE CONSOLE}

{$R *.res}

uses
Spring,
System.SysUtils;

type

IMyThing = interface
['{01E799A5-9141-4C5E-AA85-B7C9792024D9}']
procedure BasicThing;
end;

TMyThing = class(TInterfacedObject, IMyThing)
strict private
procedure BasicThing;
end;

IMyThingHelper = record
private
FOutage: IMyThing;
public
class operator Implicit(const Value: IMyThing): IMyThingHelper;
procedure HelpfulThing;
end;

TMyThingHelper = class helper for TMyThing
public
class procedure ObjectThing;
end;

{ TOutage }

procedure TMyThing.BasicThing;
begin
Writeln('Basic Thing');
end;


{ IOutageHelper }

procedure IMyThingHelper.HelpfulThing;
begin
Writeln('Helpful thing');
end;

class operator IMyThingHelper.Implicit(const Value: IMyThing): IMyThingHelper;
begin
Result.FOutage := Value;
end;

{ TMyThingHelper }

class procedure TMyThingHelper.ObjectThing;
begin
Writeln('Object thing');
end;

var
LThing: IMyThing;

begin
try
LThing := TMyThing.Create;
LThing.BasicThing;
//LThing.HelpfulThing; // <--- **** prefer this syntax but obviously does not compile
IMyThingHelper(LThing).HelpfulThing; // <--- this works ok but prefer not to have to cast it here

//LThing.ObjectThing; // <--- obviously does not compile
(LThing as TMyThing).ObjectThing; // <--- class helpers work ok but no good for interfaces

Readln;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.

关于如何使这段代码在 **** 显示的地方工作,有什么想法或建议吗?我知道答案可能是完全“不”,但似乎正在采取一些非常聪明的解决方法,也许比我聪明得多的人知道如何做? (德尔福XE5)

另一个例子

var
dataObject: IDataObject;

//Get clipboard IDataObject
OleGetClipboard({out}dataObject);

//Check if they want us to move or copy what's on the clipboard
preferredDropEffect: DWORD := dataObject.GetPreferredDropEffect;

//...do the stuff with the clipboard

//Tell them what we did
dataObject.SetPerformedDropEffect(DROPEFFECT_NONE); //we moved the underlying data; sender need not do anything
dataObject.SetPasteSucceeded(DROPEFFECT_MOVE); //Paste complete

有助手:

TDataObjectHelper = interface helper for IDataObject
public
function GetPreferredDropEffect(DefaultPreferredDropEffect: DWORD=DROPEFFECT_NONE): DWORD;
end;

function TDataObjectHelper.GetPreferredDropEffect(DefaultPreferredDropEffect: DWORD=DROPEFFECT_NONE): DWORD;
begin
{
DROPEFFECT_NONE = 0; //Drop target cannot accept the data.
DROPEFFECT_COPY = 1; //Drop results in a copy. The original data is untouched by the drag source.
DROPEFFECT_MOVE = 2; //Drag source should remove the data.
DROPEFFECT_LINK = 4; //Drag source should create a link to the original data.
DROPEFFECT_SCROLL = 0x80000000 //Scrolling is about to start or is currently occurring in the target. This value is used in addition to the other values.
}
if TDataObjectHelper.ContainsFormat(Source, CF_PreferredDropEffect) then
Result := TDataObjectHelper.GetUInt32(Source, CF_PREFERREDDROPEFFECT)
else
Result := DefaultDropEffect;
end;

最佳答案

为什么不直接使用另一个接口(interface)呢?

program IHelper;

{$APPTYPE CONSOLE}

{$R *.res}

uses
Spring,
System.SysUtils;

type

IMyThing = interface
['{01E799A5-9141-4C5E-AA85-B7C9792024D9}']
procedure BasicThing;
end;

IMyThingHelper = interface
['{...}']
procedure HelpfulThing;
end;

TMyThing = class(TInterfacedObject, IMyThing, IMyThingHelper)
strict private
procedure BasicThing;
procedure HelpfulThing;
end;

{ TOutage }

procedure TMyThing.BasicThing;
begin
Writeln('Basic Thing');
end;

{ IOutageHelper }

procedure TMyThing.HelpfulThing;
begin
Writeln('Helpful thing');
end;

var
LThing: IMyThing;
LHelper: IMyThingHelper;
begin
try
LThing := TMyThing.Create;
LThing.BasicThing;
if Supports(LThing, IMyThingHelper, LHelper) then
LHelper.HelpfulThing;
Readln;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.

关于Delphi 界面帮助程序/解决方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24793678/

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