實現了一個window下對窗體操作的類,實現的功能如:移動窗體、獲取窗體位置和大小、截取窗體圖片、坐標轉換等。
直接上代碼:
# coding=utf-8 import win32con import win32api import win32gui import win32ui from ctypes import * from ctypes import wintypes GetForegroundWindow = windll.user32.GetForegroundWindow GetWindowRect = windll.user32.GetWindowRect SetForegroundWindow = windll.user32.SetForegroundWindow GetWindowText = windll.user32.GetWindowTextA MoveWindow = windll.user32.MoveWindow EnumWindows = windll.user32.EnumWindows class RECT(Structure): _fields_ = [ ('left', c_long), ('top', c_long), ('right', c_long), ('bottom', c_long) ] class POINT(Structure): _fields_ = [ ('x', c_long), ('y', c_long), ] class FormControl(object): def __init__(self): self.win_hd = None self.win_title = '' def bindActiveWindow(self): """ 函數功能:獲取當前焦點所在窗口 """ self.win_hd = GetForegroundWindow() def bindWindowByName(self, win_name): """ 函數功能:根據窗體名獲取窗體句柄 """ self.win_title = win_name pro_fun_type = CFUNCTYPE(c_bool, c_int, c_long) pro_fun_p = pro_fun_type(self.EnumWindowsProc) EnumWindows(pro_fun_p, None) def getWinRect(self): """ 函數功能:獲取窗體的位置和大小 """ if self.win_hd is None: return None rect=RECT() GetWindowRect(self.win_hd,byref(rect)) return rect def toScreenPos(self, x,y): """ 函數功能:將窗體內部坐標轉換為相對於顯示屏的絕對坐標 """ #未指定窗口,則結束函數 if self.win_hd is None: return None rect=self.getWinRect() #指定的坐標不在窗體內,則結束函數 if x < 0 or y < 0 or x > rect.right or y > rect.bottom: return None pos = POINT() pos.x = x + rect.left pos.y = y + rect.top return pos def toWindowPos(self,x,y): """ 函數功能:將絕對坐標轉換成相對於窗體內部坐標 """ if self.win_hd is None: return None rect = self.getWinRect() pos = POINT() pos.x = x - rect.left pos.y = y - rect.top # 指定的坐標不在窗體內,則結束函數 if pos.x < 0 or pos.y < 0 or pos.x > rect.right or pos.y > rect.bottom: return None return pos def WindowActive(self): """ 函數功能:將窗體置前 """ if self.win_hd is None: return None SetForegroundWindow(self.win_hd) def getHWND(self): return self.win_hd def getWinTitle(self): """ 函數功能:獲取窗體的標題 """ if self.win_hd is None: return None buffer = create_string_buffer(255,'\0') GetWindowText(self.win_hd,buffer,255) value=buffer.value.decode('gbk') return value def MoveTo(self,x,y): """ 函數功能:移動窗體到指定坐標位置 """ if self.win_hd is None: return None rect = self.getWinRect() MoveWindow(self.win_hd,x,y,rect.right-rect.left,rect.bottom-rect.top,True) def WinCapture(self,path,x,y,w,h): """ 函數功能:抓取窗體截圖,並保存到文件 參 數:path 保存路徑 x 截取起始x坐標(窗體內相對坐標) y 截取起始y坐標(窗體內相對坐標) w 截取寬度,為0則取窗體寬度 h 截取長度,為0則取窗體高度 """ if self.win_hd is None: return None rect = self.getWinRect() if w == 0: w = rect.right - rect.left if h == 0: h = rect.bottom - rect.top if x < 0 or y < 0 or (x+w) > rect.right or (y+h) > rect.bottom: return None self.Capture(self.win_hd,path,x,y,w,h,0) def WinCapture_Mem(self,x,y,w,h): """ 函數功能:抓取窗體截圖,並返回圖像內存數據 參 數: x 截取起始x坐標(窗體內相對坐標) y 截取起始y坐標(窗體內相對坐標) w 截取寬度,為0則取窗體寬度 h 截取長度,為0則取窗體高度 """ if self.win_hd is None: return None rect = self.getWinRect() if w == 0: w = rect.right - rect.left if h == 0: h = rect.bottom - rect.top if x < 0 or y < 0 or (x+w) > rect.right or (y+h) > rect.bottom: return None return self.Capture(self.win_hd,'',x,y,w,h,1) def Capture(self, hd, path, x, y, w, h, mode): """ 函數功能:截圖 參 數:hd 截取的窗口句柄 path 保存路徑 x 截取起始x坐標(窗體內相對坐標) y 截取起始y坐標(窗體內相對坐標) w 截取寬度,為0則取窗體寬度 h 截取長度,為0則取窗體高度 mode 保存模式 0:保存為圖片,1:返回圖像字節數據 """ # 根據窗口句柄獲取窗口的設備上下文 hwndDC = win32gui.GetWindowDC(self.win_hd) # 根據窗口的DC獲取memDC srcdc = win32ui.CreateDCFromHandle(hwndDC) # memDC創建可兼容的DC saveDC = srcdc.CreateCompatibleDC() # 創建bigmap准備保存圖片 saveBitMap = win32ui.CreateBitmap() # 為bitmap開辟空間 saveBitMap.CreateCompatibleBitmap(srcdc, w, h) # 高度saveDC,將截圖保存到saveBitmap中 saveDC.SelectObject(saveBitMap) # 截取從左上角(0,0)長寬為(w,h)的圖片 saveDC.BitBlt((0, 0), (w, h), srcdc, (x, y), win32con.SRCCOPY) if mode == 0: saveBitMap.SaveBitmapFile(saveDC, path) else: signedIntsArray = saveBitMap.GetBitmapBits(True) return signedIntsArray # 釋放內存 srcdc.DeleteDC() saveDC.DeleteDC() win32gui.ReleaseDC(self.win_hd,hwndDC) win32gui.DeleteObject(saveBitMap.GetHandle()) def EnumWindowsProc(self,hwnd, lParam): buffer = create_string_buffer(255,'\0') GetWindowText(hwnd,buffer,255) value=buffer.value.decode('gbk') if value == self.win_title: self.win_hd = hwnd print(self.win_hd) return False return True
測試代碼:
import FormApi import time if __name__== '__main__': time.sleep(2) form=FormApi.FormControl() form.bindActiveWindow() rect=form.getWinRect() print("坐標:(x:%d,y:%d),大小:(width:%d,height:%d)" % (rect.left,rect.top,rect.right-rect.left,rect.bottom-rect.top)) time.sleep(2) form.WinCapture(r'c:\1.bmp',0,0,200,200)