Older Version Newer Version

JanetTerra JanetTerra Jul 6, 2011

=Custom Cursors With LoadCursorFromFile= //Janet Terra// [[toc|flat]] ---- =Changing the Cursor= Mitchell Kotler wrote two articles for the Liberty BASIC Newsletters about loading Windows cursors using the 16 bit user.dll, "LoadCursor" API. The demo was written for Liberty BASIC v2. As an update to Liberty BASIC v4.x and the 32 bit user32.dll, Gordon Sweet submitted [[newsletter.2004#x--Issue%20#121%20Jun.%202004|Changing the Cursor - Two Examples]] for the Liberty BASIC Newsletter #121. These programs temporarily change the cursor to any of 25 predefined Windows images. It is possible to temporarily change the cursor to a custom designed cursor using the user32.dll, "LoadCursorFromFile" API. =Finding Custom Cursors= This article originally appeared in the Liberty BASIC Newsletters #135 and was based upon information valid for Windows XP. The available cursors and cursor names differ in Windows 7. In either version, the cursor files are found in the C:\WINDOWS\Cursors\ directory. You may need to adjust the variable path$ if your Windows path differs. This demo loads four cursors found in the Windows\Cursors\ folder. In addition, four custom cursors, can be loaded and used. These public domain cursors were obtained from [[media type="custom" key="9928323"]]. * [[file:BlueTranspCircle.cur]] * [[file:GreenTranspCircle.cur]] * [[file:LumaBlue.ani]] * [[file:LumaGreen.ani]] Custom cursors can be saved in the same folder as the .bas file. It is not necessary to save them in the WINDOWS>Cursors> folder. =Setting the Cursor in the Control Panel= My Computer> Control Properties> Mouse> Pointers brings up a dialog that will allow you to change the Mouse Pointer scheme. You can search the web to find numerous libraries of cursors in just about every theme imaginable. **<span style="color: #ff0000;">This demo DOES NOT alter or add to your computer mouse settings</span>**. =SetCursor vs SetSystemCursor= The code in this demo temporarily changes the cursor while it is over the graphicbox only. Each time the mouse moves, the call to SetCursor must be made. The cursor reverts to the original when not moving over the graphicbox. It is possible to permanently change the look of the cursor with the API call, SetSystemCursor. Be careful, though, as this call can make irreversible and unpleasant changes. **<span style="color: #ff0000;">This demo DOES NOT call SetSystemCursor</span>**. =Static Cursors and Animated Cursors= Static cursors end with the extension **.cur**. An animated cursor usually ends with the extension **.avi**. Both are loaded with the user32.dll "LoadCursorFromFile" API call. This demo displays both static and animated cursors. Use the Menu to sample the different icons. [[code format="lb"]] 'Demo by Janet Terra 'Originally published Liberty BASIC Newsletter #135 'Updated July, 2011, for LBPE 'Requires BlueTranspCircle.cur, GreenTranspCircle.cur, LumaBlue.ani, LumaGreen.ani 'or substitute with your own cursor files Nomainwin Dimnomainwin dim StyleCursor(8) path$ = "C:\WINDOWS\Cursors\" 'Cursors following path$ found in Windows Directory ' Sample Cursors common to both Windows XP and Windows 7 StyleCursor$(1) = path$;"arrow_rl.cur" StyleCursor$(2) = path$;"pen_il.cur" StyleCursor$(3) = path$;"lnodrop.cur" StyleCursor$(4) = path$;"help_im.cur" StyleCursor$(5) = "BlueTranspCircle.cur" StyleCursor$(6) = "GreenTranspCircle.cur" StyleCursor$(7) = "LumaBlue.ani" StyleCursor$(8) = "LumaGreen.ani" WindowWidth = 308 WindowHeight = 368 UpperLeftX = Int((DisplayWidthint((DisplayWidth - WindowWidth)/2) UpperLeftY = Int((DisplayHeightint((DisplayHeight - WindowHeight)/2) msg$ = " Move Mouse Over " Menumenu #main, "Cursors", "Windows - Black Arrow", _ [cursor1], "Windows - Blue Pen", _ [cursor2], "Windows - No Drop", [cursor3], _ "Windows - Arrow with Help", [cursor4],|, _ "Custom - Blue Circle", [cursor5], "Custom - Green Circle", _ [cursor6],|, "Custom Animated - Luma Blue", [cursor7], _ "Custom Animated - Luma Green", [cursor8],|, "Normal", _ [cursor0],|, "E&xit", [endDemo] Graphicboxgraphicbox #main.g, 0, 0, 300, 100 Statictext statictext #main.s, "", 0, 110, 300, 100 Stylebits stylebits #main.s, _SS_CENTERIMAGE, 0, 0, 0 Textbox textbox #main.t, 0, 220, 300, 100 Stylebits stylebits #main.t, _ES_READONLY or _ES_MULTILINE, _ES_AUTOHSCROLL, 0, 0 Open open "Load Cursor From File" for Window_NFwindow_nf as #main #main "Trapclose"trapclose [endDemo]" hMain = hWnd(#main) hGB = hWnd(#main.g) Callcall SetClass hGB 'Text Messages to User #main, "Font"font Microsoft_Sans_Serif 12 Bold"bold" #main.g "Font"font Microsoft_Sans_Serif 12 Bold"bold" #main.g "Down; Fill Yellow; Color Darkgreen; Backcolor Yellow""down; fill yellow; color darkgreen; backcolor yellow" #main.g "\\\\";msg$;"Graphic Window" #main.g "Flush""flush" #main.s msg$;"Regular Window" #main.t Chr$(13);Chr$(10);Chr$(13);Chr$(10);msg$;"Textbox"chr$(13);chr$(10);chr$(13);chr$(10);msg$;"Textbox" 'Trapping When mouseMove" #main.g "When"when mouseMove [showCursor]" #main.g "Setfocus""setfocus" [cursor0] cursorHandle = _OCR_NORMAL Waitwait [cursor1] cursorFile$ = StyleCursor$(1) cursorHandle = LoadCursorFromFile(cursorFile$) Waitwait [cursor2] cursorFile$ = StyleCursor$(2) cursorHandle = LoadCursorFromFile(cursorFile$) Waitwait [cursor3] cursorFile$ = StyleCursor$(3) cursorHandle = LoadCursorFromFile(cursorFile$) Waitwait [cursor4] cursorFile$ = StyleCursor$(4) cursorHandle = LoadCursorFromFile(cursorFile$) Waitwait [cursor5] cursorFile$ = StyleCursor$(5) cursorHandle = LoadCursorFromFile(cursorFile$) Waitwait [cursor6] cursorFile$ = StyleCursor$(6) cursorHandle = LoadCursorFromFile(cursorFile$) Waitwait [cursor7] cursorFile$ = StyleCursor$(7) cursorHandle = LoadCursorFromFile(cursorFile$) Waitwait [cursor8] cursorFile$ = StyleCursor$(8) cursorHandle = LoadCursorFromFile(cursorFile$) Waitwait [showCursor] Call SetCursor cursorHandle Waitwait [endDemo] Closeclose #main End end 'Call just once in the beginning of the program Subsub SetClass hWin index = _GCL_HCURSOR or 0 value = 0 CallDLLcalldll #user32, "SetClassLongA", _ hWin as ulong, _ index as long, _ value as long, _ result as long End Subend sub 'Set Cursor to Handle of Loaded Cursor Subsub SetCursor hCursor CallDLL calldll #user32, "SetCursor", _ hCursor as ulong, _ result as long End Subend sub 'Load Cursor from File Functionfunction LoadCursorFromFile(file$) CallDLL calldll #user32, "LoadCursorFromFileA", _ file$ as Ptr,ptr, _ LoadCursorFromFile as ulong End Functionend function [[code]] =Finding System Cursors= Not all Windows cursors are common to both Windows XP and Windows 7. This next demo uses [[media type="custom" key="9947277"]] to list the available cursors. Hover the mouse over the graphicbox to preview that cursor. Note that some .ani cursor files appear to be non-functioning. [[code format="lb"]] ' Use the Files command to find cursors available ' on the system dim info$(10, 10) path$ = "C:\WINDOWS\Cursors\" files path$, info$() nFiles = val(info$(0, 0)) ' Fill an array with the available cursors dim sysCursors$(nFiles) for i = 1 to nFiles sysCursors$(i) = info$(i, 0) next i ' The Preview nomainwin WindowWidth = 400 WindowHeight = 450 graphicbox #demo.g, 0, 0, 400, 200 listbox #demo.cur, sysCursors$(), [selCursor], 25, 220, 340, 180 open "Preview System Cursors" for window as #demo #demo "trapclose [closeApp]"[endDemo]" #demo.g "down; fill cyan; backcolor cyan" #demo.g "font verdana 14 bold; place 20 40" #demo.g "\\Hover Mouse to Preview Cursor" #demo.g "flush" #demo "font verdana 12 bold" #demo.cur "singleclickselect" ' Necessary Cursor Information path$ = "C:\WINDOWS\Cursors\" hDemo = hWnd(#demo) hGB = hWnd(#demo.g) Call SetClass hGB ' Trapping When mouseMove #demo.g "When mouseMove [showCursor]" #demo.g "Setfocus" wait [selCursor] #demo.cur "selectionindex? selCur" cursorHandle = LoadCursorFromFile(path$;sysCursors$(selCur)) wait [showCursor] Call SetCursor cursorHandle wait [closeApp][endDemo] close #demo end sub SetClass hWin index = _GCL_HCURSOR or 0 value = 0 calldll #user32, "SetClassLongA", _ hWin as ulong, _ index as long, _ value as long, _ result as long end sub sub SetCursor hCursor calldll #user32, "SetCursor", _ hCursor as ulong, _ result as long end sub function LoadCursorFromFile(file$) calldll #user32, "LoadCursorFromFileA", _ file$ as Ptr, _ LoadCursorFromFile as ulong end function [[code]] ---- [[toc|flat]]