Jump to content

Save active mouse Cursor to Png


Recommended Posts

objective: dynamically save active mouse cursor as shown on screen to png where ever the mouse pointer is at.

below is the a test code, it is working 99% percent of the time except for one instance where it fails while mouse cursor is in SciTE with 'Ibeam' pointer.

To test this, open paint.exe and change the 'tool' to [pencil, eraser, color picker, magnifer ect..] and move mouse down to the drawing board will get info of each cursor info and it is all good. (replace $bString2 > $bString in $info if you want to see binary readout of captured cursor)

but if bring up Autoit SciTE, the cursor quickly changes to 'I-beam' and that is where it fails to get this cursor. Any idea why or any solutions? 

 

#include <GUIConstantsEx.au3>
#include <ScreenCapture.au3>
#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
#include <WinAPI.au3>
#include <WinAPIRes.au3>
#include <Memory.au3>
#include <Array.au3>


Global $_CURSOR_BINARY
HotKeySet("{ESC}","Esc")

;~ run('paint.exe')

While 1
  Sleep(250)
  _GetActiveCursor()
WEnd

#-------------------------------------------------------#
#                #
#-------------------------------------------------------#
Func _GetActiveCursor()

    Local $hCursor, $hBitmap
    _GDIPlus_Startup()

    ; get current mouse cursor as image
    $hCursor = _WinAPI_CopyIcon(_WinAPI_GetCursorInfo()[2])                ; get Active Cursor (cant IBeam in SciTE)
;~  $hCursor = _WinAPI_CopyIcon(_WinAPI_LoadCursor (0, $IDC_IBEAM))    ; get default 'Ibeam' pointer cursor (cant get I-Beam)

    Local $hBitmap = _GDIPlus_BitmapCreateFromHICON32($hCursor)

    _BitmapInfoAndSaveToPng($hBitmap, 'BitmapInfo - Active Cursor')

    _WinAPI_DestroyIcon($hCursor)
    _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_Shutdown()
EndFunc

#-------------------------------------------------------#
#                #
#-------------------------------------------------------#
Func _BitmapInfoAndSaveToPng($hBitmap, $sTitle='BitmapInfo', $sw='tooltip')
  
    Local $bString, $bString2, $x64String, $info, $sDim
    
;~     _GDIPlus_Startup()
        
    ; show image binary string
    $bString = Image2BinaryString($hBitmap)
;~     $x64String = _ConvertToBase64($bString)
    
    
    If StringCompare($bString, $_CURSOR_BINARY) Then 
          
        $_CURSOR_BINARY = $bString
            
        ; show image type
        $ImgType = _GDIPlus_ImageGetType($hBitmap)          ;<-- 10 or 11 is not image
        $ImgTypeError = @error                              ;<-- -1 is fails
        
        ; show image format
        $aFormat = _GDIPlus_ImageGetRawFormat($hBitmap)
        
        ; image dimension
        $aDim = _GDIPlus_ImageGetDimension($hBitmap)
        If IsArray($aDim) Then
          $sDim = $aDim[0] &' x '& $aDim[1]
        Else 
          $sDim = ''
        EndIf
        
        
        ; disp info
        $info =   'Image type: '    &@TAB& $ImgType         &@CRLF& _ 
                  'Image @error: '  &@TAB& $ImgTypeError    &@CRLF& _
                  '' &@CRLF& _
                  'Image format: '  &@TAB& $aFormat[1]      &@CRLF& _
                  'Image GUID: '    &@TAB& $aFormat[0]      &@CRLF& _
                  'Image Dim: '     &@TAB& $sDim            &@CRLF& _
                  '' &@CRLF& _
                  'Image binary: '  &@TAB& $bString2         &@CRLF& _
                  'Image base64: '  &@TAB& $x64String       &@CRLF

        Switch $sw
            Case 'msgbox'
                MsgBox(0, @ScriptLineNumber &': '& $sTitle, $info)
            Case 'tooltip'
                ToolTip($info)
        EndSwitch

        ; save cursor to file if cursor is success
        If $ImgType > 0 Or $ImgTypeError < 10  Then
            $imgFile = @ScriptDir & '\' &$sTitle& '.png'
;~             _GDIPlus_ImageSaveToFile($hBitmap, $imgFile)
        EndIf
    EndIf

;~     _GDIPlus_Shutdown()
EndFunc


#-------------------------------------------------------#
#                #
#-------------------------------------------------------#
Func Image2BinaryString($hBitmap) ; based on UEZ code
  Local $sImgCLSID, $tGUID, $tParams, $tData

  $sImgCLSID = _GDIPlus_EncodersGetCLSID("PNG")
  $tGUID = _WinAPI_GUIDFromString($sImgCLSID)

  Local $hStream = _WinAPI_CreateStreamOnHGlobal()
  _GDIPlus_ImageSaveToStream($hBitmap, $hStream, DllStructGetPtr($tGUID))
  Local $hMemory = _WinAPI_GetHGlobalFromStream($hStream)
  Local $iMemSize = _MemGlobalSize($hMemory)
  Local $pMem = _MemGlobalLock($hMemory)
  $tData = DllStructCreate("byte[" & $iMemSize & "]", $pMem)
  Local $bData = DllStructGetData($tData, 1)
  _WinAPI_ReleaseStream($hStream)
  _MemGlobalFree($hMemory)
  Return $bData

EndFunc


Func Esc()
   Exit
EndFunc

 

this alternative does save 'I-beam' to file but because it redrawn cursor to gui then screencapture of hwnd, it loses the transparency background

Edited by zeenmakr
Link to comment
Share on other sites

Your code worked for me, i can get all cursors. Only needed to do a tweak to the file saving line, so that it doesn't overwrite the same file over and over.

And i dont get any error, the image type is always 1.

Edited by careca
Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

thanks for the feedback, even with the ibeam while mouse hover SciTe editor?

;~  $hCursor = _WinAPI_CopyIcon(_WinAPI_LoadCursor (0, $IDC_IBEAM))    ; get default 'Ibeam' pointer cursor (cant get I-Beam)

what about with this line independently enabled and disable  the dynamic line above it.

Quote

Only needed to do a tweak to the file saving line, so that it doesn't overwrite the same file over and over.

i understand it just for testing purposes, press ESC to end script to preserve the saved cursor. 

Also would it possible if you could upload the saved Ibeam png from the script

 

to set IBeam cursor via regex which is currently on my win8 system. see attached for screenshot & beam_r.cur zipped

Local $hcu = 'HKEY_CURRENT_USER\Control Panel\Cursors'
Local $cursorKey = 'IBeam'
RegWrite($hcu, $cursorKey, 'REG_EXPAND_SZ', '%SystemRoot%\cursors\beam_r.cur')
DllCall('user32.dll', 'int', 'SystemParametersInfo', 'int', 0x57, 'int', 0, 'int', 0, 'int', 0)

;~ alt set cursor via _WinAPI_SetSystemCursor()

 

win8_IBeam_r.png

beam_r.zip

Link to comment
Share on other sites

15 minutes ago, zeenmakr said:

thanks for the feedback, even with the ibeam while mouse hover SciTe editor?

what about with this line independently enabled and disable  the dynamic line above it.

Yes.

With that line instead of the other, all i get is error = 1 No cursor.

Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

When i took the screenshots, the cursors are hidden, but yes.

Both show the information.

Spoiler

The code exactly as im using

#include <GUIConstantsEx.au3>
#include <ScreenCapture.au3>
#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
#include <WinAPI.au3>
#include <WinAPIRes.au3>
#include <Memory.au3>
#include <Array.au3>


Global $_CURSOR_BINARY
HotKeySet("{ESC}","Esc")

;~ run('paint.exe')

While 1
  Sleep(250)
  _GetActiveCursor()
WEnd

#-------------------------------------------------------#
#                #
#-------------------------------------------------------#
Func _GetActiveCursor()

    Local $hCursor, $hBitmap
    _GDIPlus_Startup()

    ; get current mouse cursor as image
    $hCursor = _WinAPI_CopyIcon(_WinAPI_GetCursorInfo()[2])                ; get Active Cursor (cant IBeam in SciTE)
   ;$hCursor = _WinAPI_CopyIcon(_WinAPI_LoadCursor (0, $IDC_IBEAM))    ; get default 'Ibeam' pointer cursor (cant get I-Beam)

    Local $hBitmap = _GDIPlus_BitmapCreateFromHICON32($hCursor)

    _BitmapInfoAndSaveToPng($hBitmap, 'BitmapInfo - Active Cursor')

    _WinAPI_DestroyIcon($hCursor)
    _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_Shutdown()
EndFunc

#-------------------------------------------------------#
#                #
#-------------------------------------------------------#
Func _BitmapInfoAndSaveToPng($hBitmap, $sTitle='BitmapInfo', $sw='tooltip')

    Local $bString, $bString2, $x64String, $info, $sDim

;~     _GDIPlus_Startup()

    ; show image binary string
    $bString = Image2BinaryString($hBitmap)
;~     $x64String = _ConvertToBase64($bString)


    If StringCompare($bString, $_CURSOR_BINARY) Then

        $_CURSOR_BINARY = $bString

        ; show image type
        $ImgType = _GDIPlus_ImageGetType($hBitmap)          ;<-- 10 or 11 is not image
        $ImgTypeError = @error                              ;<-- -1 is fails

        ; show image format
        $aFormat = _GDIPlus_ImageGetRawFormat($hBitmap)

        ; image dimension
        $aDim = _GDIPlus_ImageGetDimension($hBitmap)
        If IsArray($aDim) Then
          $sDim = $aDim[0] &' x '& $aDim[1]
        Else
          $sDim = ''
        EndIf


        ; disp info
        $info =   'Image type: '    &@TAB& $ImgType         &@CRLF& _
                  'Image @error: '  &@TAB& $ImgTypeError    &@CRLF& _
                  '' &@CRLF& _
                  'Image format: '  &@TAB& $aFormat[1]      &@CRLF& _
                  'Image GUID: '    &@TAB& $aFormat[0]      &@CRLF& _
                  'Image Dim: '     &@TAB& $sDim            &@CRLF& _
                  '' &@CRLF& _
                  'Image binary: '  &@TAB& $bString2         &@CRLF& _
                  'Image base64: '  &@TAB& $x64String       &@CRLF

        Switch $sw
            Case 'msgbox'
                MsgBox(0, @ScriptLineNumber &': '& $sTitle, $info)
            Case 'tooltip'
                ToolTip($info)
        EndSwitch

        ; save cursor to file if cursor is success
        If $ImgType > 0 Or $ImgTypeError < 10  Then
            $imgFile = @ScriptDir & '\SavedCursors\' &$sTitle&@SEC& '.png'
            ConsoleWrite($imgFile&@CRLF)
            DirCreate(@ScriptDir & '\SavedCursors')
            _GDIPlus_ImageSaveToFile($hBitmap, $imgFile)
        EndIf
    EndIf

;~     _GDIPlus_Shutdown()
EndFunc


#-------------------------------------------------------#
#                #
#-------------------------------------------------------#
Func Image2BinaryString($hBitmap) ; based on UEZ code
  Local $sImgCLSID, $tGUID, $tParams, $tData

  $sImgCLSID = _GDIPlus_EncodersGetCLSID("PNG")
  $tGUID = _WinAPI_GUIDFromString($sImgCLSID)

  Local $hStream = _WinAPI_CreateStreamOnHGlobal()
  _GDIPlus_ImageSaveToStream($hBitmap, $hStream, DllStructGetPtr($tGUID))
  Local $hMemory = _WinAPI_GetHGlobalFromStream($hStream)
  Local $iMemSize = _MemGlobalSize($hMemory)
  Local $pMem = _MemGlobalLock($hMemory)
  $tData = DllStructCreate("byte[" & $iMemSize & "]", $pMem)
  Local $bData = DllStructGetData($tData, 1)
  _WinAPI_ReleaseStream($hStream)
  _MemGlobalFree($hMemory)
  Return $bData

EndFunc


Func Esc()
   Exit
EndFunc

 

screenshot - 19062020-0710.jpg

screenshot - 19062020-0731.jpg

Edited by careca
Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

@careca thanks for troubleshooting, now i'm left with the mystery as to why my system is unable to capture Ibeam curssor.

...the cursors are hidden...

I use lightweight screenshot 'greenshot' 

 

offtopic: i noticed your signature tagged 'IUIAtomation', could i pm on this as am struggle with it.

Link to comment
Share on other sites

Im running the Autoit beta, and windows 10 if that is of interest.

 

It's really a good idea to create another topic with whatever issue you're struggling with.

Im not anything close to an expert on it, even though i find it useful at times, i tend to avoid it if possible, way too slow for me, never got it working as fast as i wanted.

And if you create a post there will be more people who can help.

Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...