# -*- coding:utf-8 -*-
import  win32api
import win32gui,win32ui
import time




while True :
    time.sleep(1)#Wait for a second
    ci = win32gui.GetCursorInfo() #Getting cursor information

    print 'Contains cursor type, handle, coordinates'+str(ci) #Contains cursor type, handle, coordinates
    print 'GetCursor Getting handle'+str(win32gui.GetCursor())#win32gui.GetCursor()To get cursor handle but and GetCursorInfo The handle is not the same. It is not clear why.?

    if(ci [1]==0):#Sometimes the cursor will be hidden by the game or program, so the error will be reported.
        print "cursor off"
        continue

    #-----------------------------------------------To void
    #e=win32gui.SetCursor(ci[1])#Change the cursor to return the old cursor handle type
    #print type(e)
    #if (e == 0):
        #print "cursor off"
        #continue
    #---------------------------------------------To void
    ii=win32gui.GetIconInfo(ci[1])#Return the image information of the cursor. Note that the parameters are not allowed win32gui.GetCursor()Get the handle, otherwise the hotspot read error, why?
    print 'Cursor parameter'+str(ii) #Cursor type, hotspot coordinates x,y,Black-and-white bitmap, color bitmap
    #I want to use color bitmap to derive bmp Pictures don't succeed. c++Put bitmaps in Cimg.Attach It's easy inside. save Settle
    #Then I don't know. python Is there any method available? pil It's not found inside.
    bm = win32gui.GetObject(ii[3])#Return PyBITMAP Type can get cursor size. Note that black and white bitmaps are preferred here, and color bitmaps may cause monochrome cursor errors.
    print 'high' + str(bm.bmHeight)
    print 'wide' + str(bm.bmWidth)

    gdc=win32gui.GetDC(0)#To extract a handle from the specified context 0, remember that releasing 0 should also represent the entire screen
    hdc = win32ui.CreateDCFromHandle(gdc)#Create one based on its handle DC object
    hbmp = win32ui.CreateBitmap()#Create a new bitmap
    hbmp.CreateCompatibleBitmap(hdc,bm.bmWidth, bm.bmHeight)#Set bitmaps to be context-compatible and image size
    hdc = hdc.CreateCompatibleDC()#Create a screen compatible DC
    # CreateCompatibleDC It is equivalent to opening a space in memory, copying the screen or window in, and then operating on it, until the operation is completed.
    #Copy back to the screen to refresh the screen.
    hdc.SelectObject(hbmp)#By putting bitmaps in context, you can edit bitmaps.

    win32gui.DrawIconEx(hdc.GetHandleOutput(), 0, 0, ci[1], bm.bmWidth, bm.bmHeight, 0, None,2)#Icon size
    #DrawIconEx Draw bitmaps into the specified context
    #hdc.GetHandleOutput()Return context handle
    #Parameters (contextual handles that need to be put in, x Coordinates y Coordinates, cursor handle to be put in, cursor height, cursor width, frame of animation cursor, background brush (can be empty), drawing type int)
    bitmapbits = hbmp.GetBitmapBits(True)#Convert the picture to a string

    print bitmapbits
    hbmp.SaveBitmapFile(hdc, 'scre99t.bmp')#Save the bitmap as a picture. Note that you can only put it here. dc

    #Resource release
    win32gui.ReleaseDC(0, gdc)#Release context parameters (window handle, context handle)
    hdc.DeleteDC()
    win32gui.DeleteObject(hbmp.GetHandle())

 




Really tired python is still not suitable for c + + work and pywin32 is too few examples on the Internet, or go abroad to find an example, vague is to understand the context, handle relationship.

But here are a few questions.

1, win32gui.GetIconInfo contains black-and-white bitmaps and color bitmaps. Why do they keep changing?

2. Why is the handle acquired by win32gui.GetCursor() different from GetCursorInfo(), and the handle of GetCursor() can't show hot spots?

3. Whether the bitmap of win32gui.GetIconInfo can be saved as bmp directly, there are methods in c++, but python can not find them.