Delphi: Screen.Cursor does not work and cannot detect Windows.SetCursor (crHourGlass)

In my application I have

Screen.Cursor := crHourGlass;
Application.ProcessMessages;
try
...

finally
  Screen.Cursor := crDefault;
  Application.ProcessMessages;
end;

But that just doesn't work as expected. It seems to immediately return to crDefault when it is processed.

After some Googling, I decided to try Windows.SetCursor () - but I searched for MSDN and cannot find a list of cursor types.

Update I thought I found a solution (using SetSystemCursor (Screen.Cursors [crHourGlass], OCR_NORMAL);), but I can't seem to change the cursor to normal: (.

+3
source share
3 answers

I think I have a solution:

Here's how to change the cursor for "whole desktop" - not just for your application:

SetSystemCursor(Screen.Cursors[crDefault], OCR_NORMAL);

: /, , - , , . .

MSDN, SetCursor, , , .

Update: , , SetSystemCursor (Screen.Cursors [crHourGlass], OCR_NORMAL); - , , , - , - 1. , .

: :

procedure TForm1.Button1Click(Sender: TObject);
var
  cArrow, cHour: HCURSOR;
begin
  cArrow := CopyImage(Screen.Cursors[crArrow], IMAGE_CURSOR, 0, 0, LR_COPYFROMRESOURCE);
  cHour := CopyImage(Screen.Cursors[crHourGlass], IMAGE_CURSOR, 0, 0, LR_COPYFROMRESOURCE);
  if (cArrow <> 0) and (cHour <> 0) and SetSystemCursor(cHour, OCR_NORMAL) then
    try

      // do processing

    finally
      SetSystemCursor(cArrow, OCR_NORMAL);
    end;
end;
+4

, , , , Application.ProcessMessages

:

// MainFormUnit
type
  TMainForm = class(TForm)
    btnClickMe: TButton;
    procedure btnClickMeClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  MainForm: TMainForm;

implementation

uses
  LazyFormUnit;

{$R *.dfm}

procedure TMainForm.btnClickMeClick(Sender: TObject);
var
  oLazyForm: TLazyForm
begin
  oLazyForm := TLazyForm.Create(Self, 0);
  oLazyForm.ShowModal;
  oLazyForm.Free;
end;

// LazyFormUnit
type
  TLazyForm = class(TForm)
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
  public
    constructor Create(p_oComponent: TComponent; p_nValue: Integer); reintroduce;
  end;

implementation

{$R *.dfm}

constructor TLazyForm.Create(p_oComponent: TComponent; p_nValue: Integer);
begin
  inherited Create(p_oComponent);
  Application.ProcessMessages;
end;

procedure TLazyForm.FormShow(Sender: TObject);
begin
  Screen.Cursor := crHourGlass;
  Application.ProcessMessages;
  try
    Sleep(1000 * 5);
  finally
    Screen.Cursor := crDefault;
  end;
end;

.

+3

, try. , . debug , , , crDefault.

In addition, you should not necessarily assume that the cursor was crDefault when you started your program. Safe method:

var
  C: TCursor;

begin

  C : = Screen.Cursor;
  Screen.Cursor := crHourGlass;

  try
    // long running code here
  finally
    Screen.Cursor := C;
  end;

end;

And finally (if you excuse the expression), you do not need Application.ProcessMessages if the purpose for which you use it is to ensure that the changed cursor is displayed.

+1
source

Source: https://habr.com/ru/post/1775340/


All Articles