Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi: Screen.Cursor not working and can't figure out Windows.SetCursor(crHourGlass)

Tags:

delphi

In my app I have

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

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

But this simply isn't working as expected. It seems to immediately change back to crDefault when it is processing.

After some Googling I decided to try Windows.SetCursor() - but I've searched the MSDN and I can't find the list of cursor types.

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

like image 411
Richard Woolf Avatar asked Jan 21 '23 02:01

Richard Woolf


1 Answers

I think I have the solution:

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

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

But be warned: any other applications/windows that want to change cursors will do so - so this is only effective if your user doesn't mess around with other applications while YOUR application is busy. As an over-ride, you could temporarily change all your systems default cursors to the cursor you want - and change them all back after the process.

I am still disappointed at the MSDN for not providing its cursor types for SetCursor - but fortunately I didn't end up having to use it.

Update: This seems to be the right track, but I can't seem to change the cursor back after SetSystemCursor(Screen.Cursors[crHourGlass], OCR_NORMAL); If anybody's reading this, I would appreciate if you take a moment to provide me with some working code - that 1. Sets the System Cursor to an hourglass, and then back to an arrow.

edit: Sample code for reverting back to default cursor:

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;
like image 130
Richard Woolf Avatar answered Feb 02 '23 00:02

Richard Woolf