Recent

Author Topic: Make mouse cursor bigger.  (Read 776 times)

seghele0

  • Full Member
  • ***
  • Posts: 222
Make mouse cursor bigger.
« on: September 20, 2024, 05:54:50 pm »
When mouseEnter on a button I want to double the size of the cursor (crHandPoint). With mouseLeave the cursor returns to its standard size and type.
Can anyone provide the support with code please.

Hansvb

  • Hero Member
  • *****
  • Posts: 700
Re: Make mouse cursor bigger.
« Reply #1 on: September 21, 2024, 10:35:40 am »
Hi,
A fun Saturday morning puzzle so I did some googling and came up with the following.

In the examples folder that is installed with Lazarus you have: ...examples\cursors
See button 5 of this example. This button uses a custom cursor.

If you want to do that with your own cursor, you must first create a res file. You can find out how that works at:
Quote
https://wiki.freepascal.org/lazres

I looked for an cur file on my PC and then created a new res file with lazres by putting in a command window (I am a Windows user):
Code: Pascal  [Select][+][-]
  1. lazres person_l.res "D:\Development\Pascal\34\Forum\test_mouse_cursor \person_l.cur"
If you need a large cursor you search for a file. There  are a lot of free cur files. (Or you can probably create one with a editor).

Place the created person_l.res file  in your project folder.


Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.          // Add this ^^^^^^^^^^
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     procedure Button1Click(Sender: TObject);
  18.   private
  19.  
  20.   public
  21.  
  22.   end;
  23.  
  24. var
  25.   Form1: TForm1;
  26.  
  27. implementation
  28.  
  29. {$R *.lfm}
  30. {$R person_l.res} // <<--------Add this
  31.  
  32. { TForm1 }
  33.  
  34. procedure TForm1.Button1MouseMove(Sender: TObject; Shift: TShiftState; X,
  35.   Y: Integer);
  36. var
  37.   Cur: TCursorImage;
  38. begin
  39.   Cur := TCursorImage.Create;
  40.   Cur.LoadFromResourceName(HInstance, 'person_l');
  41.   Screen.Cursors[1] := Cur.ReleaseHandle;
  42.   Cur.Free;
  43.   Button1.Cursor := 1;
  44. end;  
  45.  
  46. end.    


So if you add a larger cursor as a resource, you can include it in your program.
« Last Edit: September 21, 2024, 11:45:25 am by Hansvb »

seghele0

  • Full Member
  • ***
  • Posts: 222
Re: Make mouse cursor bigger.
« Reply #2 on: September 21, 2024, 11:59:35 am »
Thank you.
I still don't understand how to create a "MYres.res" and with what content and where to install a bitmap (BMP or JPG). Can I create the .res file with notepad ?
 ::)
Perhaps the code below (Google - A.I.) would be a different solution, but 'crUser' gives error.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5.   Windows, Classes, SysUtils, Forms, Controls, Graphics,
  6.   Dialogs, StdCtrls, LCLtype, LResources;
  7. type
  8.   { TForm1 }
  9.   TForm1 = class(TForm)
  10.     Button1: TButton;
  11.     procedure Button1MouseEnter(Sender: TObject);
  12.     procedure Button1MouseLeave(Sender: TObject);
  13.   private
  14.     { private declarations }
  15.     OriginalCursor: TCursor;
  16.   public
  17.     { public declarations }
  18.   end;
  19. var
  20.   Form1: TForm1;
  21. implementation
  22. {$R *.lfm}
  23.  
  24. { TForm1 }
  25.  
  26. procedure TForm1.Button1MouseEnter(Sender: TObject);
  27. var
  28.   CursorImage: TBitmap;
  29. begin
  30.   OriginalCursor := Screen.Cursor;
  31.   CursorImage := TBitmap.Create;
  32.   try
  33.     CursorImage.LoadFromResourceName(HInstance, 'DEFAULT_CURSOR');
  34.     CursorImage.Width := CursorImage.Width * 2;
  35.     CursorImage.Height := CursorImage.Height * 2;
  36.     Screen.Cursors[crUser] := CursorImage;
  37.     Screen.Cursor := Screen.crUser;
  38.   finally
  39.     CursorImage.Free;
  40.   end;
  41. end;
  42.  
  43. procedure TForm1.Button1MouseLeave(Sender: TObject);
  44. begin
  45.   Screen.Cursor := OriginalCursor;
  46. end;
  47.  
  48. end.                

TRon

  • Hero Member
  • *****
  • Posts: 3231
Re: Make mouse cursor bigger.
« Reply #3 on: September 21, 2024, 12:15:02 pm »
If you want to do that with your own cursor, you must first create a res file. You can find out how that works at:
Quote
https://wiki.freepascal.org/lazres
Hi Hans,

A word of warning. It is not advisable (anymore) to use lazres. You can add the (raw icon file, which automatically turns into a resource) in your project options (resource entry in the tree view). see also wiki: https://wiki.lazarus.freepascal.org/IDE_Window:_Project_Options#Resources
All software is open source (as long as you can read assembler)

Hansvb

  • Hero Member
  • *****
  • Posts: 700
Re: Make mouse cursor bigger.
« Reply #4 on: September 21, 2024, 01:58:44 pm »
@TRon,

I didn't know that. I had just googled a possible solution. But good to know.

seghele0

  • Full Member
  • ***
  • Posts: 222
Re: Make mouse cursor bigger.
« Reply #5 on: September 21, 2024, 05:27:13 pm »
Is there anyone who has a solution to my post " #2' ?
Thanks.

TRon

  • Hero Member
  • *****
  • Posts: 3231
Re: Make mouse cursor bigger.
« Reply #6 on: September 21, 2024, 05:34:17 pm »
@seghele0:
I refuse to answer questions on code generated by AI. The AI generated it ergo let the AI explain why it does or doesn't work. If it can't explain it, it isn't AI.

That is why forums like these exist... so that normal code gets posted and no machine generated crap that makes no sense to anyone but the AI and the prompt being used.

2 cents.
All software is open source (as long as you can read assembler)

seghele0

  • Full Member
  • ***
  • Posts: 222
Re: Make mouse cursor bigger.
« Reply #7 on: September 22, 2024, 10:32:06 am »
At the age of 75 and without any studies in programming, I try to relax using Lazarus, as this is a real creative hobby.

Sorry, I certainly didn't mean to offend professional programmers. My only intention to use A.I. to find a solution without bothering this forum, as I realize that your loss of time is precious.

Maybe there is a way to enlarge the cursor without having to use an "x.res" and I personally can't find the necessary code on Google.



seghele0

  • Full Member
  • ***
  • Posts: 222
Re: Make mouse cursor bigger.
« Reply #8 on: September 22, 2024, 01:53:29 pm »
Problem:  "Adjust the size of the cursor."

I continued to persevere to find the correct code. Finally I referred to Delphi and there I had success.
The code below works perfectly without having to use a specific (*.res) file.

The support of this forum remains a great asset and I am grateful for it.
 ;)
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5.   Windows, Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
  6.  
  7. type
  8.   { TForm1 }
  9.   TForm1 = class(TForm)
  10.     Button1: TButton;
  11.     procedure Button1MouseEnter(Sender: TObject);
  12.     procedure Button1MouseLeave(Sender: TObject);
  13.   private
  14.     OriginalCursor: TCursor;
  15.   public
  16.   end;
  17. var
  18.   Form1: TForm1;
  19. implementation
  20. {$R *.lfm}
  21. { TForm1 }
  22.  
  23. procedure TForm1.Button1MouseEnter(Sender: TObject);
  24. var
  25.   NewCursorInfo: TIconInfo;
  26.   CursorHandle: HICON;
  27.   ScreenCursor: TCursor;
  28. begin
  29.   OriginalCursor := Screen.Cursor;  // Save the original
  30.   GetIconInfo(LoadCursor(0, IDC_HAND), NewCursorInfo);
  31.   NewCursorInfo.hbmMask := CopyImage(NewCursorInfo.hbmMask,
  32.                 IMAGE_BITMAP, 64, 64, LR_COPYFROMRESOURCE); //Make the cursor twice as large
  33.   NewCursorInfo.hbmColor := CopyImage(NewCursorInfo.hbmColor,
  34.                 IMAGE_BITMAP, 64, 64, LR_COPYFROMRESOURCE);
  35.   CursorHandle := CreateIconIndirect(NewCursorInfo); //Create a new cursor based on the changed information
  36.   ScreenCursor := Screen.Cursors[crArrow]; //Move the cursor to the newly created large cursor
  37.   Screen.Cursors[crArrow] := CursorHandle;
  38.   Screen.Cursor := crArrow;
  39. end;
  40.  
  41. procedure TForm1.Button1MouseLeave(Sender: TObject);
  42. begin
  43.    Screen.Cursor := OriginalCursor;
  44. end;
  45.  
  46. end.      

Hansvb

  • Hero Member
  • *****
  • Posts: 700
Re: Make mouse cursor bigger.
« Reply #9 on: September 22, 2024, 03:28:20 pm »
Hi,

Nicely done. Now you'll never forget it. :)

I tested it and if I adjust it to the one below, the cursor becomes large and the height and width spring position remains good.

Code: Pascal  [Select][+][-]
  1.   NewCursorInfo.hbmMask := CopyImage(NewCursorInfo.hbmMask,
  2.                 IMAGE_BITMAP, 128, 256, LR_COPYFROMRESOURCE); //Make the cursor twice as large
  3.   NewCursorInfo.hbmColor := CopyImage(NewCursorInfo.hbmColor,
  4.                 IMAGE_BITMAP, 128, 256, LR_COPYFROMRESOURCE);
  5.                                                                  

seghele0

  • Full Member
  • ***
  • Posts: 222
Re: Make mouse cursor bigger.
« Reply #10 on: September 22, 2024, 03:55:15 pm »
Thanks for the info.
 :)

Khrys

  • Jr. Member
  • **
  • Posts: 90
Re: Make mouse cursor bigger.
« Reply #11 on: September 23, 2024, 08:48:33 am »
Thank you.
I still don't understand how to create a "MYres.res" and with what content and where to install a bitmap (BMP or JPG). Can I create the .res file with notepad ?
 ::)
Perhaps the code below (Google - A.I.) would be a different solution, but 'crUser' gives error.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5.   Windows, Classes, SysUtils, Forms, Controls, Graphics,
  6.   Dialogs, StdCtrls, LCLtype, LResources;
  7. type
  8.   { TForm1 }
  9.   TForm1 = class(TForm)
  10.     Button1: TButton;
  11.     procedure Button1MouseEnter(Sender: TObject);
  12.     procedure Button1MouseLeave(Sender: TObject);
  13.   private
  14.     { private declarations }
  15.     OriginalCursor: TCursor;
  16.   public
  17.     { public declarations }
  18.   end;
  19. var
  20.   Form1: TForm1;
  21. implementation
  22. {$R *.lfm}
  23.  
  24. { TForm1 }
  25.  
  26. procedure TForm1.Button1MouseEnter(Sender: TObject);
  27. var
  28.   CursorImage: TBitmap;
  29. begin
  30.   OriginalCursor := Screen.Cursor;
  31.   CursorImage := TBitmap.Create;
  32.   try
  33.     CursorImage.LoadFromResourceName(HInstance, 'DEFAULT_CURSOR');
  34.     CursorImage.Width := CursorImage.Width * 2;
  35.     CursorImage.Height := CursorImage.Height * 2;
  36.     Screen.Cursors[crUser] := CursorImage;
  37.     Screen.Cursor := Screen.crUser;
  38.   finally
  39.     CursorImage.Free;
  40.   end;
  41. end;
  42.  
  43. procedure TForm1.Button1MouseLeave(Sender: TObject);
  44. begin
  45.   Screen.Cursor := OriginalCursor;
  46. end;
  47.  
  48. end.                

Absolute garbage code slop generated by artificial "intelligence". Where do I even begin?  :(
  • Unused units:  Windows, Classes, SysUtils, Dialogs, LCLType  and  LResources  are not needed. Copy-pasted straight from training data perhaps?
  • Loading and scaling the cursor bitmap every single time the control is entered? Seriously? Is Moore's law making AI faster just so AI can make everything else slower?
  • Reassigning a bitmap's width and height like that does not scale its contents. What filtering should be used? What if the height was adjusted first?
  • You can't directly assign a bitmap to a cursor handle like that (unless you ask Gemini to generate a gnarly assignment operator overload, I suppose).
  • Screen.crUser  is a pure hallucination. Custom cursor constants are of course a thing, but here Gemini didn't define any. Does it expect you to extend  TScreen  with a type helper?
  • try ... finally  will still propagate any exceptions upwards unless there's an additional  try ... except  block somewhere. Does failure to set a custom cursor really need to go to the top-level exception handler? Imagine an error popup every time you hover over a button...
LLMs, while extremely impressive in many regards, are a far cry from actual intelligence and reasoning. They are glorified autocomplete, predicting what words may follow an input prompt, based on heaps upon heaps of training data. Free Pascal/Lazarus throws a wrench in that system by (unfortunately) being much less widely used than C++, Python etc. - there simply isn't nearly as much training data around for Pascal as there is for the most popular languages. Try asking Gemini to implement an automatically reference-counted type using the record management operators  Initialize, Finalize, AddRef, Copy  and watch it fail spectacularly (without even admitting it).

seghele0

  • Full Member
  • ***
  • Posts: 222
Re: Make mouse cursor bigger.
« Reply #12 on: September 23, 2024, 10:35:24 am »
Why is reply #8 not appreciated.
This code works perfectly and will certainly be of help to beginners or hobbyists.
 :)
The A.I. code (in reply #2) is becoming a nightmare for professional programmers, which is understandable.
What will the imminent future bring us with A.I.?
 :-\
In any case, this forum is an important tool for Pascal-Lazarus enthusiasts.

 

TinyPortal © 2005-2018