Page 1 of 1

Save current mouse cursor to file

Posted: Wed Jan 29, 2014 10:44 am
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!

Re: Save current mouse cursor to file

Posted: Wed Jan 29, 2014 11:22 am
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

Re: Save current mouse cursor to file

Posted: Wed Jan 29, 2014 3:48 pm
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?

Re: Save current mouse cursor to file

Posted: Wed Jan 29, 2014 9:33 pm
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

Re: Save current mouse cursor to file

Posted: Wed Jan 29, 2014 11:39 pm
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.