Sign in to follow this  
Followers 0
ofLight

WinAPI to grab cursor, GDIPlus to write to file ?

6 posts in this topic

Im trying to Capture the Mouse and create an image file with it rendered in there. I can currently do this via the method Smashly helped me with. AutoIt Forum link

However using this method i have to render the image into a GUI then take a screenshot then write the screenshot to a file. I would prefer to

be able to grab the cursor handle then just create a image file.

Obviously I am not understanding how all the different type of handles(bitmap object, device context) interact with each other and what kind is needed for what function.

#include <WinAPI.au3>
#include <GDIPlus.au3>

Sleep(2000)
_Main()

Func _Main()
    $MP2 = MouseGetPos()
    
    ; Initialize GDI+ library
    _GDIPlus_Startup ()

    ; Grab global cursor
    $aCur = _WinAPI_GetCursorInfo()
    If $aCur[0] And $aCur[1] Then

        ; Change cursor into bitmap
        $hImage2 = _GDIPlus_BitmapCreateFromHBITMAP ($aCur[2])
    EndIf
    
    ; Save Image
    _GDIPlus_ImageSaveToFile ($hImage2, @ScriptDir & "\GDIPlus_Image.jpg")  

    ; Clean up resources
    _GDIPlus_ImageDispose ($hImage2)

    ; Shut down GDI+ library
    _GDIPlus_ShutDown ()

EndFunc   ;==>_Main

There is always a butthead in the crowd, no matter how hard one tries to keep them out.......Volly

Share this post


Link to post
Share on other sites



So what do you ultimately want to do. Create a file from current cursor, or use ScreenCapture with cursor?


"be smart, drink your wine"

Share this post


Link to post
Share on other sites

So what do you ultimately want to do. Create a file from current cursor, or use ScreenCapture with cursor?

He wants a picture of the cursor.

But the problem is you are trying to create a BitMap from a handle. I tried using _GDIPlus_GraphicsCreateFromHWND but that did not work. I'm no expert with GDI or WinAPI xD so I can't really help you out in this case.

Share this post


Link to post
Share on other sites

He wants a picture of the cursor.

yes exactly. ty

I tried using _GDIPlus_GraphicsCreateFromHWND

I tried the same thing unsuccessfully as well :D.

There is always a butthead in the crowd, no matter how hard one tries to keep them out.......Volly

Share this post


Link to post
Share on other sites

I'm no expert with GDI or WinAPI xD so I can't really help you out in this case.

Anyone know who the experts are on GDI and WinAPI?

Share this post


Link to post
Share on other sites

I know this was an old thread but I just wanted to post the solution, as I have seen others wanting to be able to save the cursor to a bitmap file.

Thank you to everyone that helped.

;=================================   SaveCursor   ================================
; Function Name:    _SaveCursor
; Requires:         <ScreenCapture.au3>
; Description:    
; Parameters:       $SizeX          Total horizontal size of bitmap
;                   $SizeY          Total vertical size of bitmap
;                   $DrawLocX       horizontal position to draw cursor within bitmap
;                   $DrawLocY       vertical position to draw cursor within bitmap
;                   $Filename       bitmap save filename and full path
;                   
; Syntax:            _SaveCursor([$SizeX,$SizeY,$DrawLocX,$DrawLocY,$Filename])
; Author(s):        ofLight
; Returns:      
;============================================================================
Func _SaveCursor($SizeX = 50,$SizeY = 50,$DrawLocX = 15,$DrawLocY = 15,$Filename = ".\_Mouse.bmp")
    Local $aResult
    $MGP = MouseGetPos()
    If WinExists("DeviceContextSize")Then
        $hWnd = WinGetHandle("DeviceContextSize")
    ElsE
        $hWnd = GUICreate("DeviceContextSize",$SizeX,$SizeY,$MGP[0]-25, $MGP[1]-25,$WS_POPUP)
    EndIf
    $hDDC = _WinAPI_GetDC($hWnd)
    $hCDC = _WinAPI_CreateCompatibleDC($hDDC)
    $hBMP = _WinAPI_CreateCompatibleBitmap($hDDC, $SizeX, $SizeY)
    _WinAPI_SelectObject($hCDC, $hBMP)
    If @Error Then Return 0
    _WinAPI_BitBlt($hCDC, $MGP[0]-25, $MGP[1]-25, $SizeX, $SizeY, $hDDC, $MGP[0]-25, $MGP[1]-25, $SRCCOPY)
    $aCursor = _WinAPI_GetCursorInfo()
    If $aCursor[1] Then $aResult = DllCall("User32.dll", "int", "DrawIcon", "hwnd", $hCDC, "int", $DrawLocX, "int", $DrawLocY, "hwnd", $aCursor[2])
    _WinAPI_ReleaseDC($hWnd, $hDDC)
    _WinAPI_DeleteDC($hCDC)
    _ScreenCapture_SaveImage($Filename, $hBMP)
    _WinAPI_DeleteObject($hBMP)
EndFunc;==========================   SaveCursor   ================================

There is always a butthead in the crowd, no matter how hard one tries to keep them out.......Volly

Share this post


Link to post
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
Sign in to follow this  
Followers 0