Save current mouse cursor to file

Windows specific forum
Cournefinates
New User
New User
Posts: 5
Joined: Tue Jul 27, 2010 11:05 am

Save current mouse cursor to file

Post by Cournefinates »

Greetings.

I'd like to capture the current mouse cursor and save it as a bitmap file. I'm struggling with this for about 4 hours now and I can't seem to make any progress. My code for now:

Code: Select all

Global CursorInfo.CURSORINFO
Global gIconInfo.IconInfo
Global GetObjectBitmap.BITMAP
Global CopyIconResult, GetCursorInfoResult, GetIconInfoResult, GetObjectResult
	
CursorInfo\cbSize = SizeOf(CURSORINFO)
	
GetCursorInfoResult = GetCursorInfo_(CursorInfo)
GetIconInfoResult = GetIconInfo_(CursorInfo\hCursor, gIconInfo)
GetObjectResult = GetObject_(gIconInfo\hbmMask, SizeOf(BITMAP), GetObjectBitmap)
CopyIconResult = CopyIcon_(CursorInfo\hCursor)
I'm not sure I'm on the right track, especially the last 2 lines with GetObject_() and CopyIcon_(). I believe the solution is in CursorInfo\hCursor, the handle of the current cursor. Does anyone have some ideas to make this happen?

Thanks!
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Save current mouse cursor to file

Post by PB »

Here's some starting code. Adapt it as you will.

Code: Select all

OpenWindow(0,200,200,150,100,"Mouse",#PB_Window_SystemMenu)

ButtonGadget(0,20,20,110,22,"Click to see mouse")
ImageGadget(1,70,60,0,0,0)

Repeat

  ev=WaitWindowEvent()

  If ev=#PB_Event_Gadget
    i=CreateImage(#PB_Any,32,32,32,#Red)
    StartDrawing(ImageOutput(i))
      DrawImage(GetCursor_(),0,0)
    StopDrawing()
    SetGadgetState(1,ImageID(i))
    SaveImage(i,"c:\mouse.bmp")
  EndIf

Until ev=#PB_Event_CloseWindow
Last edited by PB on Thu Jan 30, 2014 9:43 am, edited 1 time in total.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Cournefinates
New User
New User
Posts: 5
Joined: Tue Jul 27, 2010 11:05 am

Re: Save current mouse cursor to file

Post by Cournefinates »

Thanks for the code! I changed it to the following, to grab the cursor, even if it's not within the own window:

Code: Select all

CallDebugger
Global ci.CURSORINFO

ci\cbSize = SizeOf(CURSORINFO)

CreateImageResult = CreateImage(#PB_Any, 64, 64, 32, #Red)
StartDrawing(ImageOutput(CreateImageResult))
GetCursorInfo_(ci)
DrawImage(ci\hCursor, 0, 0)
StopDrawing()
SaveImage(CreateImageResult, "mouse.bmp")
But now I have the problem, that I always get this busy cursor as result: Image

Just before GetCursorInfo_() I moved the mouse in the editor, so I get the 'I' shape cursor to be grabbed, but no avail. It's always the busy cursor.

Did I miss something else here?
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8453
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Save current mouse cursor to file

Post by netmaestro »

Dunno. This doesn't show busy here:

Code: Select all

UsePNGImageEncoder()
UsePNGImageDecoder()
OpenWindow(0,200,200,150,100,"Mouse",#PB_Window_SystemMenu)

ButtonGadget(0,20,20,110,22,"Click to see mouse")
ImageGadget(1,70,60,0,0,0)

Repeat

  ev=WaitWindowEvent()

  If ev=#PB_Event_Gadget
    ci.CURSORINFO
    ci\cbSize = SizeOf(CURSORINFO)
    GetCursorInfo_(@ci)
    i=CreateImage(#PB_Any,32,32,32,#PB_Image_Transparent)
    hdc = StartDrawing(ImageOutput(i))
      DrawIconEx_(hdc, 0,0, ci\hCursor, 32, 32, 0, 0, #DI_NORMAL|#DI_COMPAT )
    StopDrawing()
    SetGadgetState(1,ImageID(i))
    SaveImage(i,GetTemporaryDirectory()+"mouse.png",#PB_ImagePlugin_PNG)
  EndIf

Until ev=#PB_Event_CloseWindow
BERESHEIT
Cournefinates
New User
New User
Posts: 5
Joined: Tue Jul 27, 2010 11:05 am

Re: Save current mouse cursor to file

Post by Cournefinates »

Thank you very much! Now it's grabbing it correctly! Maybe the debugger got in the way somehow, I did step the code before.
Post Reply