gpt4 book ai didi

delphi - Firemonkey 中的屏幕.光标

转载 作者:行者123 更新时间:2023-12-03 15:01:16 31 4
gpt4 key购买 nike

在 Delphi 6 中,我可以使用 Screen.Cursor 更改所有表单的鼠标光标:

procedure TForm1.Button1Click(Sender: TObject);
begin
Screen.Cursor := crHourglass;
end;

我正在 Firemonkey 中搜索等效项。

以下功能不起作用:

procedure SetCursor(ACursor: TCursor);
var
CS: IFMXCursorService;
begin
if TPlatformServices.Current.SupportsPlatformService(IFMXCursorService) then
begin
CS := TPlatformServices.Current.GetPlatformService(IFMXCursorService) as IFMXCursorService;
end;
if Assigned(CS) then
begin
CS.SetCursor(ACursor);
end;
end;

当我在程序末尾插入 Sleep(2000); 时,我可以看到光标 2 秒钟。但接口(interface)可能会被释放,因此,光标会在过程结束时自动重置。我还尝试将 CS 定义为全局变量,并在过程末尾添加 CS._AddRef 以防止接口(interface)被释放。但这也没有帮助。

以下代码确实有效,但仅适用于主窗体:

procedure TForm1.Button1Click(Sender: TObject);
begin
Application.MainForm.Cursor := crHourGlass;
end;

由于我想更改所有表单的光标,因此我需要遍历所有表单,但是回滚到以前的光标很棘手,因为我需要知道每个表单的前一个光标。

我的意图:

procedure TForm1.Button1Click(Sender: TObject);
var
prevCursor: TCursor;
begin
prevCursor := GetCursor;
SetCursor(crHourglass); // for all forms
try
Work;
finally
SetCursor(prevCursor);
end;
end;

最佳答案

您必须实现自己的光标服务,以便可以强制执行特定的光标。

unit Unit2;

interface

uses
FMX.Platform, FMX.Types, System.UITypes;

type
TWinCursorService = class(TInterfacedObject, IFMXCursorService)
private
class var FPreviousPlatformService: IFMXCursorService;
class var FWinCursorService: TWinCursorService;
class var FCursorOverride: TCursor;
class procedure SetCursorOverride(const Value: TCursor); static;
public
class property CursorOverride: TCursor read FCursorOverride write SetCursorOverride;

class constructor Create;
procedure SetCursor(const ACursor: TCursor);
function GetCursor: TCursor;
end;

implementation

{ TWinCursorService }

class constructor TWinCursorService.Create;
begin
FWinCursorService := TWinCursorService.Create;

FPreviousPlatformService := TPlatformServices.Current.GetPlatformservice(IFMXCursorService) as IFMXCursorService; // TODO: if not assigned

TPlatformServices.Current.RemovePlatformService(IFMXCursorService);
TPlatformServices.Current.AddPlatformService(IFMXCursorService, FWinCursorService);
end;

function TWinCursorService.GetCursor: TCursor;
begin
result := FPreviousPlatformService.GetCursor;
end;

procedure TWinCursorService.SetCursor(const ACursor: TCursor);
begin
if FCursorOverride = crDefault then
begin
FPreviousPlatformService.SetCursor(ACursor);
end
else
begin
FPreviousPlatformService.SetCursor(FCursorOverride);
end;
end;


class procedure TWinCursorService.SetCursorOverride(const Value: TCursor);
begin
FCursorOverride := Value;
TWinCursorService.FPreviousPlatformService.SetCursor(FCursorOverride);
end;

end.

主要单元:

procedure TForm1.Button1Click(Sender: TObject);
var
i: integer;
begin
TWinCursorService.CursorOverride := crHourGlass;
try
Sleep(2000);
finally
TWinCursorService.CursorOverride := crDefault;
end;
end;

关于delphi - Firemonkey 中的屏幕.光标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29229374/

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