Mega Code Archive

 
Categories / Delphi / Examples
 

Cursors

Try this: Create a cursor with Image Editor. name it CRBOMBSITE (all caps). Create a constant named crBombSite and set it to a value >1. Use this code in you program in the Form1.Create procedure Screen.Cursors[crBombSite] := LoadCursor(hInstance,'crBombsite'); Cursor := crBombSite; You can use any the custom cursor in other form by setting the Cursor property to crBombSite. -- -------------------------- Wishing I had a Virtual Shotgun to take care of all these Internet CackleDemons. BOOM!!!! shick-shuck <grin> -------------------------- ------------------------------------------------------------------------------- From: Timothy Barr <tabarr@ix.netcom.com> Subject: Re: DELPHI: Using custom cursors? Date: 19 May 1995 13:58:31 GMT WARNING!!! One problem I have found with Image Edit and Cursors is that the name of the cursor in the RES file must be ALL CAPS. For some reason the LoadCursor function seems to translate the string name to ALL CAPS before using it. ------------------------------------------------------------------------------- From: bimmer@ibm.net Subject: Re: DELPHI: Using custom cursors? Date: 16 May 1995 09:26:37 GMT 1) First create a *.res file with your cursor in. (do not use the saeme same as your application, because delphi maintains a resourcefile with this name) 2) Delphi uses 0 .. -16 (negative number) indexes for its default cursors. Use a new number for your index ex. 1. (Use a constant to make readable . e.g. crMyCursor = 1) 3) In your main forms FormCreate event, place the following code: Screen.Cursors[CrMyCursor]:= LoadCursor(hInstance, 'CURSORNAME'); (CURSORNAME is the name of the cursor in you resource file) 4) When you want to use your cursor in a form, panel or whatever just place the following code: Anotherform.Cursor:= crMyCursor; Some folks believe that the cursor property contains a cursor handle... This is NOT the case. It's a index into the component list which the Screen object maintains. All cursors in an application is maintained by this object. If you want more help lookup the TScreen object, in the on-line manuals. Hope this helps. Regards Bimmer (Per Bakkendorff) ********************************************************************************* Cursor code: eg you call a search routine (that takes some time) and say: Form1.Caption := 'File Search BUSY...'; Screen.Cursor := crHourglass; doSearch(); and then when prog execution returns from the search routine: (ie in the next line) you say: Form1.Caption := 'File Search SEARCH COMPLETED'; Screen.Cursor := crDefault; ************************************************************* Screen.Cursor := crHourglass; Screen.Cursor := crDefault; ************************************************************* Hmmm, interesting approach, cursors and icons are indeed very much alike internally, the one thing that's missing from this implementation is setting the cursor's "hot spot" which is controlled by a couple of DWORD fields in the icon/cursor info structure (ICONINFO.xHotspot & ICONINFO.yHotspot). Since these will have presumably been set to zero in icons, this would mean that the hotspot would always end up in the upper left corner of any cursor created by this technique as-is. A small addition addresses that: function NewCursor2(hIco: HICON; idxCursor: integer; Control: TControl; {!! added} HotX, HotY: DWord ): HICON; var pIcoInfo : PIconInfo; begin GetMem(pIcoInfo, SizeOf(TIconInfo)); try GetIconInfo(hIco, pIcoInfo^); {!! added} pIcoInfo^.xHotspot := HotX; {!! added} pIcoInfo^.yHotspot := HotY; Result := CreateIconIndirect(pIcoInfo^); Screen.Cursors[idxCursor] := Result; if Control <> nil then Control.Cursor := idxCursor; finally FreeMem(pIcoInfo); end; end; Note: I'm not sure what the result of this would be pre-Win95, as color cursors weren't supported (assuming anyone even cares at this point ;-)). In any case, very cool, thanks a lot! Stephen Posey slposey@concentric.net ********************************************************* COLOURED CURSOR ------------------------------------------------------------------------------------- unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Buttons, ExtCtrls; type TForm1 = class(TForm) Image1: TImage; BitBtn1: TBitBtn; procedure BitBtn1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.DFM} {uses btGraphics;} (*---------------------------------------------------------------------------------- Description : creates a new mouse cursor using an Icon handle. Returns the handle of the newly created cursor Parameters : -- hIco : HICON Handle of the icon to use for creating the cursor. It might come from a TImage or an icon resource. ie: Image1.Picture.Icon.Handle or LoadIcon(hInstance, PChar(sCursorName) -- idxCursor : integer The index the of the newly create cursor into the Screen.Cursors. Pass the index you want here. For more info check TCursor and TScreen.Cursors -- Control : TControl The function can set the newly created cursor as the cursor of a control immediately. Pass the control here or leave it nil -- HotX: DWord = 0; HotY: DWord = 0 The hot spot of the newly created cursor (added by Stephen Posey [StephenP@turbopower.com]) Tested : Delphi 5, Win2K Uses : Windows, Forms, Controls Example call : 1. NewCursor(Image1.Picture.Icon.Handle, 1, Form1); 2. NewCursor(Image1.Picture.Icon.Handle, crDrag, nil); Error checking : ? Notes : if you load the icon from a resource using something like hIco := LoadIcon(hInstance, PChar(sCursorName); you should call DestroyIcon(hIco); when you don't need the cursor anymore. Author : Theo Bebekis - bebekis@otenet.gr -----------------------------------------------------------------------------------*) function NewCursor(hIco: HICON; idxCursor: integer; Control: TControl; HotX: DWord = 0; HotY: DWord = 0): HICON; var pIcoInfo : PIconInfo; begin GetMem(pIcoInfo, SizeOf(TIconInfo)); try GetIconInfo(hIco, pIcoInfo^); pIcoInfo^.xHotspot := HotX; pIcoInfo^.yHotspot := HotY; Result := CreateIconIndirect(pIcoInfo^); Screen.Cursors[idxCursor] := Result; if Control <> nil then Control.Cursor := idxCursor; finally FreeMem(pIcoInfo); end; end; procedure TForm1.BitBtn1Click(Sender: TObject); begin NewCursor(Image1.Picture.Icon.Handle, 1, Self); end; Regards Theo ----------------------------------- Theo Bebekis Thessaloniki, Greece bebekis@otenet.gr ----------------------------------- ________________________________ Delphi mailing list -> Delphi@elists.org http://elists.org/mailman/listinfo/delphi **************************************************************** From: "The Nomad" <tnomad@digital.net> Subject: Re: [delphi] Delphi Cursors Date: Mon, 24 Jul 1995 09:29:29 +0000 > Now if I create an image component (TImage), change the Form1MouseDown event > to TImage1.MouseDown, with the line: Form1.Image1.Cursor := crHourGlass, the > cursor will only change shape when the mouse button is released. :-<<<< Ahhhh. I use: GetCursorPos(pt); SetCursorPos(pt.x, pt.y); When the cursor is set to the position it is already in, it doesn't move, but the cursor will be set properly. ************************************ * The Nomad * * tnomad@digital.net * ************************************