gpt4 book ai didi

delphi - 知道 Onclick 事件被触发

转载 作者:行者123 更新时间:2023-12-01 23:42:52 29 4
gpt4 key购买 nike

我使用服务器客户端组件,当在此组件的 TransferFile 事件中接收文件时,我使用警报消息组件。所以我希望,如果用户单击警报消息,程序将继续执行 TransferFile 事件中的代码,以在单击按钮时接受文件传输,或者在未单击按钮时退出程序。请看下面的代码:

procedure TfrmReadFile.ServerReceiveEvent(Sender: TObject;
Client: TSimpleTCPClient; Event: TTOOCSEvent);
begin
if (Event is TTOOCSEventFileTransfert) then
begin
Alert.Show;
if Alert.OnAlertClick then
begin
with (Event as TTOOCSEventFileTransfert) do
if (dlgSaveFile.Execute) then
with TMemoryStream.Create do
try
Write(Content[1], Length(Content));
SaveToFile(dlgSaveFile.FileName);
finally
Free;
end;
end;
end;
end;

但是“if Alert.OnAlertClick then”是错误的

procedure TfrmReadFile.AlertAlertClick(Sender: TObject);
begin

end;

请帮我获取这些代码。

AlertMessage 是 TMS 组件之一,它没有 ShowModal,但有我使用的 Alert.Show procedure。我想暂停执行代码,直到警报显示时间结束,如果用户没有单击警报,则执行代码将中止并且没有保存文件。

最佳答案

从您的代码中可以明显看出,当您显示警报时,文件传输已经发生:这只是“我是否保存到文件”的问题> 或“我是否丢弃已收到的内容”。我从您对 TMemoryStream.Write() 的使用中推断出此信息 - 该函数采用缓冲区作为参数,因此我假设 Content[1] 为您提供了缓冲区。这也意味着 Content 已填充您需要的数据。不传输它已经太晚了,它已经在内存中了,您所能做的就是将其保存到磁盘或丢弃它。

我也不知道 TMS 的警报是如何工作的,但我假设在任何给定时间只能显示一个警报,并且我将假设您将 Alert 放在一个组件(即:整个程序中只有一个Alert)。

您应该首先更改“收到的事件”的代码,以立即将内容移动到TMemoryStream。还要确保您不会因递归重新进入而陷入麻烦。在表单中添加一个私有(private)字段,将其命名为 FLastContentReceived: TMemoryStream;现在将您的代码更改为如下所示:

procedure TfrmReadFile.ServerReceiveEvent(Sender: TObject;
Client: TSimpleTCPClient; Event: TTOOCSEvent);
begin
if (Event is TTOOCSEventFileTransfert) then
begin
// Re-entry before we managed to handle the previous received file?
if Assigned(FLastContentReceived) then
raise Exception.Create('Recursive re-entry not supported.');

// No re-entry, let's save the content we received so we can save it to file
// if the user clicks the Alert button.
FLastContentReceived := TMemoryStream.Create;

// I don't know what Content is, but you've got that in your code so I
// assume this will work:
FLastContentReceived.Write(Content[1], Length(Content);

// Show the alert; If the OnAlertClick event fires we'll have the received file content
// in the FLastContentRecevied and we'll use that to save the file.
Alert.Show;
end;
end;

您正在尝试在 Alert.OnAlertClick 上执行 if - 所以我假设您的 Alert 组件中有一个名为 OnAlertClick 的事件。将其写入其事件处理程序中:

procedure TfrmReadFile.AlertAlertClick(Sender: TObject);
begin
if not Assigned(FLastContentReceived) then raise Exception.Create('Bug');
try
if dlgSaveFile.Execute then
FLastContentReceived.SaveToFile(dlgSaveFile.FileName);
finally FreeAndNil(FLastContentReceived);
end;
end;

如果在单击“警报”按钮之前关闭表单或者超时(警报在用户没有单击它的情况下消失),您还需要一种方法来丢弃 FLastContentReceived。表单关闭时的第一项工作(摆脱 FLastContentReceived)很简单:将其添加到表单的 OnDestroy 中:

FLastContentRecevid;Free;

处理超时可能会有点困难。如果警报有一个事件,当警报超时并且气球在未被单击的情况下消失时调用,则使用该事件处理程序来执行此操作:

FreeAndNil(FLastContentRecevid);

如果它没有提供这样的东西,您可以设置一个 TTimer ,其间隔等于警报超时(或者为了安全起见稍长一些),在显示警报之前启用它并执行此操作来自它的OnTimer:

procedure TFrmReadFile.Timer1Timer(Sender: TObject);
begin
Timer1.Enabled := False;
FreeAndNil(FLastContentRecevid);
end;

关于delphi - 知道 Onclick 事件被触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14332923/

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