python 取得特定窗口,并激活 Gdlg.print_control_identifiers()

import win32con
import win32gui
from pywinauto.application import Application
from cWindow import cWindow
import time
import pyautogui

window_title = u'GIDSAuthTool v2.24'
try:
    regex = ".*GIDSAuthTool.*"
    cW = cWindow()
    cW.find_window_regex(regex)
    cW.Maximize()
    cW.SetAsForegroundWindow()
    app = Application(backend="uia").connect(path=r"C:\\Users\\PENG CAN\Desktop\\授权工具v2.24\\GIDSAuthTool.exe")
    # 3.查看一个窗体含有的控件,子窗体,菜单
    # print(app[window_title].print_control_identifiers())
    # app.window(class_name = window_title).draw_outline(colour ='red')
    Gdlg = app.window(title_re=window_title)
    # Gdlg.wait("exists ready", timeout=1, retry_interval=3)
except:
    app = Application(backend="uia").start("C:\\Users\\PENG CAN\Desktop\\授权工具v2.24\\GIDSAuthTool.exe")
    time.sleep(1)
    Gdlg = app.window(title_re=window_title)
    Gdlg.wait("exists ready", timeout=1, retry_interval=3)
##########################################密码窗口 1stGAC2GAC2019

#print(app.window(title=u"GIDSAuthTool v2.24", auto_id="StartupForm", control_type="Window").exists())
#while(1):
#print(app.window_(title=u"GIDSAuthTool v2.24", class_name='WindowsForms10.Window.8.app.0.141b42a_r9_ad1').exists())
# while(1):
#     ()
if (app.window(title=u"GIDSAuthTool v2.24", auto_id="StartupForm", control_type="Window").exists()):
    window_title='GIDSAuthTool v2.24'
    #3.查看一个窗体含有的控件,子窗体,菜单
    #print(app[window_title].print_control_identifiers())
    #app.window(class_name = window_title).draw_outline(colour ='red')
    Gdlg = app.window(title_re=window_title)
    Gdlg.draw_outline(colour="red")
    Gdlg.print_control_identifiers()
    pyautogui.typewrite(message='GAC2019',interval=0)
    Gdlg.print_control_identifiers()
    pyautogui.press('enter')
 #print(Gdlg.child_window(title="GIDSAuthTool v2.24", auto_id="StartupForm", control_type="Window").exists())
######################2nd 窗口,主界面,#################
if (app.window(title=u"GIDSAuthTool v2.24", auto_id="PinAuthForm", control_type="Window").exists()):
    window_title='GIDSAuthTool v2.24'
    #3.查看一个窗体含有的控件,子窗体,菜单
    #print(app[window_title].print_control_identifiers())
    #app.window(class_name = window_title).draw_outline(colour ='red')
    Gdlg = app.window_(title_re=window_title)
    # Gdlg.wait("exists ready", timeout = 2, retry_interval = 3)
    Gdlg.draw_outline(colour="red")
    # Gdlg.print_control_identifiers()
    #Gdlg.child_window(title="申请码:", auto_id="textBoxApply", control_type="Edit")

    Gdlg.child_window(title="申请码:", auto_id="textBoxApply", control_type="Edit").set_edit_text('TJJJJJJJJJJJJJJJJJ4TAVJQIOKPJII6')
    #app[window_title].child_window(title="申请码",control_type="Edit").set_edit_text('TJJJJJJJJJJJJJJJJJ4TAVJQIOKPJII6')
    #time.sleep(2)
    #
    # Gdlg.child_window(title="退出(X)",auto_id="btnExit", control_type="Button").click()
    Gdlg.child_window(title="清空(C)", auto_id="btnClear", control_type="Button").click()
    Gdlg.child_window(title="最小化", control_type="Button").click()

一个将窗口激活的类

import win32gui, win32con
import re, traceback
from time import sleep


class cWindow:
    def __init__(self):
        self._hwnd = None

    def SetAsForegroundWindow(self):
        # First, make sure all (other) always-on-top windows are hidden.
        # self.hide_always_on_top_windows()
        win32gui.SetForegroundWindow(self._hwnd)

    def Maximize(self):
        win32gui.ShowWindow(self._hwnd, win32con.SW_MAXIMIZE)

    def _window_enum_callback(self, hwnd, regex):
        '''Pass to win32gui.EnumWindows() to check all open windows'''
        if self._hwnd is None and re.match(regex, str(win32gui.GetWindowText(hwnd))) is not None:
            self._hwnd = hwnd

    def find_window_regex(self, regex):
        self._hwnd = None
        win32gui.EnumWindows(self._window_enum_callback, regex)

    def hide_always_on_top_windows(self):
        win32gui.EnumWindows(self._window_enum_callback_hide, None)

    def _window_enum_callback_hide(self, hwnd, unused):
        if hwnd != self._hwnd:  # ignore self
            # Is the window visible and marked as an always-on-top (topmost) window?
            if win32gui.IsWindowVisible(hwnd) and win32gui.GetWindowLong(hwnd,
                                                                         win32con.GWL_EXSTYLE) & win32con.WS_EX_TOPMOST:
                # Ignore windows of class 'Button' (the Start button overlay) and
                # 'Shell_TrayWnd' (the Task Bar).
                className = win32gui.GetClassName(hwnd)
                if not (className == 'Button' or className == 'Shell_TrayWnd'):
                    # Force-minimize the window.
                    # Fortunately, this seems to work even with windows that
                    # have no Minimize button.
                    # Note that if we tried to hide the window with SW_HIDE,
                    # it would disappear from the Task Bar as well.
                    win32gui.ShowWindow(hwnd, win32con.SW_FORCEMINIMIZE)


# def main():
#     sleep(5)
#     try:
#         regex = ".*无标题.*"
#         cW = cWindow()
#         cW.find_window_regex(regex)
#         cW.Maximize()
#         cW.SetAsForegroundWindow()
#     except:
#         f = open("log.txt", "w")
#         f.write(traceback.format_exc())
#         print(traceback.format_exc())


# main()
  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值