part of the operation handle python

. 1  `` `to python3
 2  #  through a window class name, acquires the control handle to the window title 
. 3 HWND = win32gui.FindWindow ( " ClassName " , " TITLENAME " )
 . 4  #  by setting the control handle to the content 
. 5 win32gui.SendMessage (HWND, 12 is, 0, ' text ' )
 6  #  modify window title 
. 7 the SetWindowText (HWND, " the title " )
 . 8  # acquisition sub-window handle through the handle 
. 9 ch_hwnd = win32gui.FindWindowEx (HWND, 0, " ClassName " , " TITLENAME ")
10 
11 # 获取所有子窗口
12 hwndChildList = []
13 win32gui.EnumChildWindows(hwnd, lambda hwnd, param: param.append(hwnd), hwndChildList)
14 print(hwndChildList)
15 # 模糊查询句柄
16 def FindWinHwnd(title, top=True):
17     titles = []
18 
19     def foo(hwnd, mouse):
20         if top:
21             if IsWindow(hwnd) and IsWindowEnabled(hwnd) and IsWindowVisible(hwnd):
22                 if title in GetWindowText(hwnd):
23                     titles.append(hwnd)
24         else:
25             if title in GetWindowText(hwnd):
26                 titles.append(hwnd)
27 
28     EnumWindows(foo, 0)
29     if titles:
30         return titles[0]
31     else:
32         return 0
33 
34 # 截图
35 def window_api_capture(file, hWnd=0):
36     if hWnd == 0:
37 [          MoniterDev = win32api.EnumDisplayMonitors (None, None)
 38 is          width = MoniterDev [0] [2] [2 ]
 39          height = MoniterDev [0] [2] [. 3 ]
 40      the else :
 41 is          left, Top, right, BOT = win32gui .GetWindowRect (hWnd)
 42          width = right - left
 43          height = BOT - Top
 44          # returns a handle to the device window environment, covering the entire window, including non-client area, title bar, menu, frame 
45      hWndDC = win32gui.GetWindowDC (hWnd)
 46      # create the device description table 
47      mfcDC = win32ui.CreateDCFromHandle (hWndDC)
48      # Create a memory device context 
49      SaveDC = mfcDC.CreateCompatibleDC ()
 50      # create bitmap objects ready to save the picture 
51 is      saveBitMap = win32ui.CreateBitmap ()
 52 is      # of open space bitmap 
53 is      saveBitMap.CreateCompatibleBitmap (mfcDC, width, height)
 54      # screenshot saved to the saveBitMap 
55      saveDC.SelectObject (saveBitMap)
 56 is      # save the bitmap memory device context 
57 is      saveDC.BitBlt ((0, 0), (width, height), mfcDC, (0, 0), win32con .SRCCOPY)
 58      the try :
 59          saveBitMap.SaveBitmapFile (SaveDC, File)
 60      the except:
61         pass
62     win32gui.DeleteObject(saveBitMap.GetHandle())
63     saveDC.DeleteDC()
64     mfcDC.DeleteDC()
65     win32gui.ReleaseDC(hWnd, hWndDC)

 

Guess you like

Origin www.cnblogs.com/yubs/p/11647323.html