首页 > 编程语言 > 详细

Python窗体操作函数

时间:2019-06-21 17:58:30      阅读:199      评论:0      收藏:0      [点我收藏+]

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

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


    def bindActiveWindow(self):
        """
        函数功能:获取当前焦点所在窗口
        """
        self.win_hd = GetForegroundWindow()


    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

        # 根据窗口句柄获取窗口的设备上下文
        hwndDC = win32gui.GetWindowDC(self.win_hd)
        # 根据窗口的DC获取memDC
        memDC = win32ui.CreateDCFromHandle(hwndDC)
        # memDC创建可兼容的DC
        saveDC = memDC.CreateCompatibleDC()
        # 创建bigmap准备保存图片
        saveBitMap = win32ui.CreateBitmap()
        # 为bitmap开辟空间
        saveBitMap.CreateCompatibleBitmap(memDC, w, h)
        # 高度saveDC,将截图保存到saveBitmap中
        saveDC.SelectObject(saveBitMap)
        # 截取从左上角(0,0)长宽为(w,h)的图片
        saveDC.BitBlt((0, 0), (w, h), memDC, (x, y), win32con.SRCCOPY)
        saveBitMap.SaveBitmapFile(saveDC, path)

测试代码:

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(rc:\1.bmp,0,0,200,200)

 

Python窗体操作函数

原文:https://www.cnblogs.com/WushiShengFei/p/11065778.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!