Recent

Author Topic: [SOLVED] How do I change the screen mouse pointer to a busy pointer?  (Read 477 times)

AMJF

  • New Member
  • *
  • Posts: 48
My project has parts where it taking seconds to achieve a task, but the mouse doesn't change during that time so it looks like the program has frozen.

I used to use busy mouse pointers in Delphi back in the day, but I'm not sure how to change the mouse pointer image in Lazarus.

Can anyone help me on this matter, please?
« Last Edit: March 24, 2023, 09:44:15 pm by AMJF »

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: How do I change the screen mouse pointer to a busy pointer?
« Reply #1 on: March 24, 2023, 10:12:03 am »
The correct instruction, beginning with Laz 2.0, is the Screen.BeginWaitCursor/EndWaitCursor pair

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button2Click(Sender: TObject);
  2. var
  3.   i,j: Integer;
  4. begin
  5.   Screen.BeginWaitCursor;
  6.   try
  7.     // Execute the time-consuming task here, e.g.
  8.     for i := 1 to 1000*1000*1000 do
  9.       j := Random(1000);
  10.   finally
  11.     Screen.EndWaitCursor;
  12.   end;
  13. end;

In older versions you simply set/reset the Screen.Cursor
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   i,j: Integer;
  4. begin
  5.   Screen.Cursor := crHourglass;
  6.   try
  7.     // Execute the time-consuming task here, e.g.
  8.     for i := 1 to 1000*1000*1000 do
  9.       j := Random(1000);
  10.   finally
  11.     Screen.Cursor := crDefault;
  12.   end;
  13. end;

The former solution (BeginWaitCursor/EndWaitCursor) has the advantage that it stores the previously used cursor internally so that successive calls always return to the previous state.

AMJF

  • New Member
  • *
  • Posts: 48
Re: How do I change the screen mouse pointer to a busy pointer?
« Reply #2 on: March 24, 2023, 10:18:35 am »
Thanks, wp!

I remember now, that was the old Delphi method, the crHourglass and crDefault.

What about clicks made on controls when in the "BeginWaitCursor" setting? I found that on my old Delphi project, I had to put in extra commands to disable the code that would run otherwise, is that still the case now, or will it ignore clicks?

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: How do I change the screen mouse pointer to a busy pointer?
« Reply #3 on: March 24, 2023, 10:48:50 am »
This has nothing to do with the cursor. It depends on whether your code in the tight loop is allowed to handle the message queue. If it is, clicks on other controls are executed as usual. This is not a disadvantage because you could cancel the operation this way:
Code: Pascal  [Select][+][-]
  1. var
  2.   ClickCounter: Integer = 0;
  3.   OperationStopped: Boolean = false;
  4.  
  5. procedure TForm1.Button1Click(Sender: TObject);
  6. var
  7.   i,j: Integer;
  8. begin
  9.   Screen.BeginWaitCursor;
  10.   try
  11.     OperationStopped := false;
  12.     for i := 1 to 1000*1000*1000 do
  13.     begin
  14.       // Process message loop
  15.       if i mod 1000 = 0 then
  16.         Application.ProcessMessages;
  17.       // Break if stop has been requested by click on other button
  18.       if OperationStopped then
  19.         break;
  20.       // Operation
  21.       j := Random(1000);
  22.     end;
  23.   finally
  24.     Screen.EndWaitCursor;
  25.   end;
  26. end;
  27.  
  28. procedure TForm1.Button2Click(Sender: TObject);
  29. begin
  30.   inc(ClickCounter);
  31.   Caption := 'Click #' + IntToStr(ClickCounter);
  32.   OperationStopped := true;
  33. end;

 

TinyPortal © 2005-2018