Python series basis to explain - automatic control windows desktop

Original link: https://zhuanlan.zhihu.com/p/73001806

When the main way to interact with the PC using a PC is to look at the screen, listening to the sound, click the mouse and keyboarding and so on. In the office automation trend, tedious work can let the program automatically. For example, automated testing, automated order entry transactions. In addition to a lot of software GUI can also be operated with a CLI interface operation, but when some software does not provide CLI, how should we do it? We can also use the program to control the desktop window to simulate a mouse click or by pressing the keyboard action to release themselves.

pywin32 is a Python library that provides access to the Windows API to extend Python, provides a full range of windows constants, interfaces, threads and COM mechanisms, etc., after installation comes with a pythonwin the IDE. Next describes how to operate under the windows desktop software via Python.

1, open the software or documentation, such as opening a Google browser, or open a word document, as follows:

win32api.ShellExecute(1, 'open',
 r'C:Program Files (x86)GoogleChromeApplicationchrome.exe',
 '', '', 1)
win32api.ShellExecute(1, 'open',
 r'C:UsersJayDesktopEnvironment Guider.docx',
 '', '', 1)

win32api.ShellExecute () parameters include:

HWND: Specifies the parent window handle

Operation: specified action, such as "edit", "explore", "open", "find", "print", "NULL"

FileName: Specifies the file or program you want to open

Specify the required parameters open the program: Parameters

Directory: The default directory

ShowCmd: open options, optional value:

  • • SW_HIDE = 0; {hidden window, to make a window active state}
  • • SW_SHOWNORMAL = 1; {with the latest display window size and position, so that it enters the active state while}
  • • SW_NORMAL = 1; {display a window with the current size and position of the active window is not changed}
  • • SW_SHOWMINIMIZED = 2; {minimize the window, and activate it}
  • • SW_SHOWMAXIMIZED = 3; {maximized window, and activate it}
  • •SW_MAXIMIZE = 3; {同 SW_SHOWMAXIMIZED}
  • • SW_SHOWNOACTIVATE = 4; {display a window with the size and location of the most recent, the active window is not changed}
  • • SW_SHOW = 5; {display a window with the current size and position, so that it enters the active state}
  • • SW_MINIMIZE = 6; {minimized window is not activated}
  • •SW_SHOWMINNOACTIVE = 7; {同 SW_MINIMIZE}
  • • SW_SHOWNA = 8; {display a window with the current size and position of the active window is not changed}
  • •SW_RESTORE = 9; {同 SW_SHOWNORMAL}
  • •SW_SHOWDEFAULT = 10; {同 SW_SHOWNORMAL}
  • •SW_MAX = 10; {同 SW_SHOWNORMAL}

Successful application execution will return a handle, if the return value <= 32, then execution error. The return value Possible errors are:

Insufficient memory 0-- {}

2-- {filename} error

3-- pathname {error}

11-- {EXE file is invalid}

{Error} shared 26--

27-- {filename} incomplete or invalid

28-- timeout {}

29-- {DDE transaction failures}

30-- {DDE other transaction is being processed can not complete the transaction DDE}

31-- {no application associated}

2, find the handle to the form. Win32 programming in the world, including all controls are windows into a text box form, all forms have separate handle. To operate any of the form, you need to find the handle to the form. A handle is a 32-bit integer, for marking objects in windows. Such as finding and New Text Document.txt Snipping Tool handle, as shown below:

para_hld = win32gui.FindWindow(None, "Snipping Tool")# 1836416
para_hld = win32gui.FindWindow(None, "New Text Document.txt - Notepad")# 591410

win32gui.FindWindow () belong win32gui module that from the top-level window (that is, the desktop) to start matching the search criteria form, and returns a handle to the form. This function can only find the main window, and therefore can not search child windows, not case-sensitive, not found it returns 0.

win32gui.FindWindow () parameters include (lpClassName = None, lpWindowName = None):

• lpClassName: character, the form of the class name, can be found in Spy ++

• lpWindowName: character, window name, that title is the title bar can see.

 

 

3, looking for a handler class name and title. Such as the class name and find the corresponding title and by the New Text Document.txt Snipping Tool handle, as shown below:

title = win32gui.GetWindowText(1836416)
classname = win32gui.GetClassName(1836416)
print "windows handler:{0}; title:{1}; classname:{2}".format(1836416, title, classname)

Print display as follows:

windows handler:1836416; title:Snipping Tool; classname:Microsoft-Windows-Tablet-SnipperToolbar
title = win32gui.GetWindowText(591410)
classname = win32gui.GetClassName(591410)
print "windows handler:{0}; title:{1}; classname:{2}".format(591410, title, classname)

Print display as follows:

windows handler:591410; title:New Text Document.txt - Notepad; classname:Notepad

4, call win32gui.EnumWindows () enumerates all window handle until the last top-level window is enumerated stops the enumeration process. As follows:

hWndList = []
win32gui.EnumWindows(lambda hWnd, param: param.append(hWnd), hWndList)
print hWndList
for hwnd in hWndList:
 title = win32gui.GetWindowText(hwnd)
 print title

Print display as follows:

[852802L, 65946L, 65928L, 65930L, 65900L, 65920L, 65924L, 65922L, 65944L, 65892L, 65886L, 6817870L, 65960L, 6031410L, …… 66052L, 65734L]
……
New Text Document.txt - Notepad
Snipping Tool
DDE Server Window
OfficePowerManagerWindow
OfficePowerManagerWindow
DDE Server Window
GDI+ Window
Global Internet Access
……

5, win32gui.SetForegroundWindow () function is provided to specify the form the topmost layer, and the window active.

Constructor: win32gui.SetWindowPos (HWN hWnd, HWND hWndlnsertAfter, int X, int Y, int cx, int cy, UNIT.Flags)

Problem error on win32gui.SetForegroundWindow (para_hld):

pywintypes.error: (0, ‘SetForegroundWindow’, ‘No error message is available’)

In fact, call SetForegroundWindow () there will be a lot of restrictions, explained with reference to the official website:

 

 

It is necessary to see the currently running when you call SetForegroundWindow () whether the conditions meet the above requirements, here calling SetForegroundWindow () in advance before sending a keyboard event to resolve the problem.

Routine is shown below:

win32api.keybd_event(13, 0, 0, 0) #
win32gui.SetForegroundWindow(para_hld)

6, win32api.keybd_event () Analog keyboard input.

The constructor is as follows:

win32api.keybd_event (bVk, bScan, dwFlags, dwExtraInfo)

• bVk: virtual key code (keyboard key code table in Appendix);

• bScan: hardware scan code, can usually be set to 0;

• dwFlags: a flag bit operation function, if the value KEYEVENTF_EXTENDEDKEY the key is pressed, can also be set to 0, if the value KEYEVENTF_KEYUP the key is released;

• dwExtraInfo: define additional 32-bit value associated with the keystroke, typically set to 0..

Press the enter key to lift the following routine:

win32api.keybd_event(13,0,0,0) # enter
win32api.keybd_event(13,0,win32con.KEYEVENTF_KEYUP,0) #释放按键

7, simulating mouse input. Direct analysis routine, as shown below:

# 获取鼠标当前位置的坐标
print win32api.GetCursorPos()
# 将鼠标移动到坐标处
win32api.SetCursorPos((100, 100))
# 左点击
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 100, 100, 0, 0)
time.sleep(2)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 100, 100, 0, 0)

8, the operation on the mouse and keyboard can also be used PyUserInput library. PyUserInput is operating a mouse and keyboard module using python cross-platform, easy to use. Supported platforms and depend on the following:

•Linux - Xlib

•Mac - Quartz, AppKit

•Windows - pywin32, pyHook

Examples of objects of a mouse and keyboard, as follows:

from pymouse import PyMouse
from pykeyboard import PyKeyboard
m = PyMouse()
k = PyKeyboard()
操作鼠标和键盘,如下所示:
m.click(190,70,1)#移动并且在xy位置点击
time.sleep(2)
m.click(190, 200, 1)#移动并且在xy位置点击
time.sleep(2)
k.tap_key(k.function_keys[5])#–点击功能键F5

Guess you like

Origin www.cnblogs.com/zx-coder/p/11993017.html