Am I restricted to the cursor numbers defined in Controls.pas under Delphi 7?
Asked Answered
C

3

1

I am using Delphi 7 under Windows 7 to download files.

I want to change the cursor during the download.

I set the Screen.Cursor := crHourGlass; , but , after looking at the constant cursor numbers in Controls.pas, I was wondering whether there are other numbers I can use to change the cursor into ( I don't want to add a cursor to my resource file, I just want to use standard numbers which I can use without having to add resources ).

Corneille answered 25/5, 2011 at 9:18 Comment(0)
M
2

does other numbers produce meaning full cursors

No. Other numbers besides built-in cursors constants will produce a default cursor which is identical to TCursor(crDefault) (in other terms - HCURSOR(Screen.Cursors[crDefault])). These built-in cursors resides in the application resources and preloaded on VCL startup. To add custom cursor you HAVE to add CURSOR resource and then load it and add it to VCL.

procedure TForm1.FormCreate(Sender: TObject); platform;
const
  crCustom = 42;
var
  Cursor: HCURSOR;
begin
  Cursor := LoadCursor(HInstance, 'CUSTOM');
  Win32Check(Cursor <> 0);  // required error check
  Screen.Cursors[crCustom] := Cursor;
  { Done, newly added crCustom is ready to use }

  Self.Cursor := crCustom; // for example - lets show custom cursor

  { also, TScreen object will manage resource handle }
  { and perform cleanup for you, so DestroyCursor call is unnecessary }
end;

More complicated example with indirect cursor construction NB: example have numerous flaws: 1) DestroyIcon call is erroneous 2) they'd noticed it if there was error checking after all API calls

Melanimelania answered 26/5, 2011 at 6:6 Comment(0)
H
1

The "standard numbers" (crHourglass, crDefault, etc) are the predefined cursors that are provided by Delphi's VCL. You can define your own and load them into the application from a resource or from file via the Windows API, but there are no magic unpublished TCursor definitions (or stray numbers) that will mean anything. Trying to set Screen.Cursors[] to an unknown number without first loading a cursor will cause an array out of bounds error at minimum, and an access violation at the worst result in the default cursor being displayed (see TScreen.GetCursors in Forms.pas).

Quick explanation: TCursorRec is defined in the VCL source as a record containing a pointer to the next record, an index, and a cursor handle (HCURSOR). It's basically a singly-linked list, and when you ask for a cursor by accessing the Cursors list, the VCL looks through the list starting at the first item and sequentially stepping through until it either finds an index that matches the one you requested (at which point it sets the cursor to that item's HCURSOR value), or determines that the index you requested isn't assigned, in which case it returns the default cursor.

Healion answered 25/5, 2011 at 15:44 Comment(0)
S
0

crHourGlass is of type TCursor, which is an integer alias (more or less). It is an Index that can be used to set a cursor from stock.

You can add cursors using

Screen.Cursors[Number] := ...   needs to be a HCURSOR.

So if you have a handle to a new cursor, you can use that in Delphi.

Note that the crXXX constants an the TCursor type are defined in Controls and the Screen class is defined in Forms. So you can see the code for yourself.

Shulock answered 25/5, 2011 at 12:22 Comment(1)
Yes, I know and have found all that. The question is, does other numbers produce meaning full cursors.Corneille

© 2022 - 2024 — McMap. All rights reserved.