gpt4 book ai didi

delphi - 将delphi过程作为方法调用

转载 作者:行者123 更新时间:2023-12-02 22:23:22 25 4
gpt4 key购买 nike

这是我的简单代码,编译良好,但引发访问冲突。它进入MD程序并且调试器显示一些X和Y值,但是退出程序后AV发生。希望有人能帮忙。

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Controls, Forms, ExtCtrls;

type
TForm1 = class(TForm)
Panel1: TPanel;
procedure FormCreate(Sender: TObject);
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure MD(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
form1.caption:= inttostr(x)+ ' '+ inttostr(y);
end;

procedure TForm1.FormCreate(Sender: TObject);

function MakeMethod(data, code: pointer): TMethod;
begin
result.Data:= data;
result.Code:= code;
end;

begin
panel1.OnMouseDown:= TMouseEvent(MakeMethod(nil, @MD));
end;

end.

谢谢

最佳答案

MD 签名应包含额外的隐藏参数;它解决了 AV 问题。

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls;


type
TForm1 = class(TForm)
Panel1: TPanel;
procedure FormCreate(Sender: TObject);
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure MD(Instance, Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
form1.caption:= inttostr(x)+ ' '+ inttostr(y);
end;

procedure TForm1.FormCreate(Sender: TObject);

function MakeMethod(data, code: pointer): TMethod;
begin
result.Data:= data;
result.Code:= code;
end;

begin
panel1.OnMouseDown:= TMouseEvent(MakeMethod(nil, @MD));
end;

end.

关于delphi - 将delphi过程作为方法调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38323445/

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