实现了一个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)

Python窗体操作函数的更多相关文章

  1. Python文件操作函数os.open、io.open、内置函数open之间的关系

    Python提供了多种文件操作方式,这里简单介绍os.open.io.open.内置函数open之间的关系: 一.内置函数open和io.open实际上是同一个函数,后者是前者的别名: 二.os.op ...

  2. Python基础操作-函数

    本节内容 1. 函数基本语法及特性2. 参数与局部变量3. 返回值4.递归5.高阶函数 1.函数基本语法及特性函数是什么? 函数一词来源于数学,但编程中的「函数」概念,与数学中的函数是有很大不同的,具 ...

  3. Python 文件操作函数

    这个博客是 Building powerful image classification models using very little data 的前期准备,用于把图片数据按照教程指示放到规定的文 ...

  4. python——列表操作函数和方法

    1.添加新元素 1.1 append()函数 描述:append() 方法用于在列表末尾添加新的对象. 语法:list.append(obj) 参数:obj -- 添加到列表末尾的对象. 返回值:该方 ...

  5. Python 字符串操作函数一

    #-*- coding:utf-8 -*- strword = "i will fly with you , fly on the sky ." #find print(strwo ...

  6. python——字符串操作函数

    字符串 join() map() split() rsplit() splitlines() partiton() rpartition() upper() lower() swapcase() ca ...

  7. Python 字符串操作函数二

    #-*- coding:utf-8 -*- line = "l want watch movie with you ." print(line.center(50)) print( ...

  8. Python文件操作与函数目录

    文件操作 python文件操作 函数 Python函数学习——初步认识 Python函数学习——作用域与嵌套函数 Python函数学习——匿名函数 python内置函数 Python函数学习——递归 ...

  9. python 集合、函数和文件操作

    1.set集合 set集合是一个无序.不可重复.可嵌套的序列,基本功能是进行成员关系测试和删除重复元素,可以使用大括号({})或者 set()函数创建集合,注意:创建一个空集合必须用 set() 而不 ...

随机推荐

  1. Intellij IDEA如何生成JavaDoc

    JavaDoc是一种将注释生成HTML文档的技术. 1.使用javadoc命令生成文档 首先了解一下javadoc指令的用法 用法: javadoc [options] [packagenames] ...

  2. 实时查看mysql当前连接数

    如何实时查看mysql当前连接数? 1.查看当前所有连接的详细资料:./mysqladmin -uadmin -p -h10.140.1.1 processlist 2.只查看当前连接数(Thread ...

  3. 一个.java文件中是否可以有多个类

    前段时间,有个同事问到我这个问题:一个.java文件中是否可以有多个类? 答案:可以有多个类,但最多只能有一个被public修饰的class. 且若这个.java文件中有一个public类型的clas ...

  4. UFUN函数 UF_ASSEM UF_PART函数(UF_ASSEM_ask_work_part,UF_PART_ask_part_name)

    UF_initialize(); tag_t work_part_tag=NULL_TAG; ]=""; //获取当前工作部件的tag work_part_tag=UF_ASSEM ...

  5. UFUN函数 UF_CSYS函数 UF_MTX函数(如何创建坐标系);

    // (题目不够长,写在这了) // 函数有 // UF_MTX3_initialize,UF_CSYS_create_matrix,UF_CSYS_create_csys,UF_CSYS_ask_c ...

  6. ent 基本使用十一 sql.DB 集成

    这个功能是github中大家提的比较多的一个,所以官方也暴露了相关的api 配置sql.DB 一种方式 package main ​ import ( "time" ​ " ...

  7. benchmarkdotnet dotnet 基准测试类库试用(一)

    使用基准测试对于我们应用的性能优化是比较好的方式,可以快速看出优化的结果同时可以给出报告结果 benchmarkdotnet 是dotnet 版本的一个工具,以下是一个简单的试用 环境准备 我使用的是 ...

  8. 洛谷 P2040 打开所有的灯 题解

    这个题写其他题解的dalao们已经解释了 一个灯最多开一次(如果实在不知道为什么看评论区) 这个题一共就9个灯嘛,对吧 递归好想不好写(对于我这种蒟蒻) 所以我写了一个所有题解中最暴力的 直接枚举9个 ...

  9. python爬虫出现ProxyError: HTTPSConnectionPool错误

    在今天刚刚打开pycharm运行爬虫时,发现所有的爬虫都不能运行,会出现如下的错误: 错误出现的主要原因是;代理错误(其实自己根本没有设置代理) 解决方法: 在网上查阅了许多类似的错误解决方法,试过后 ...

  10. WIFI万能钥匙面试引出上线注意事项

    WEB应用上线程序员注意事项: 单元测试 前后端联调 界面和用户体验 DEBUG 性能 SEO 安全性