在上篇文章中,有部分功能写在了另外的python文件中,便于广大粉丝的要求,我对部分代码就行public
感谢广大网友粉丝们的喜爱:
import psutil,re
import win32gui,win32com.client
from pymouse import PyMouse
import time
import win32clipboard as w
import pythoncom
#-----------------进程是否运行
def proc_exist(process_name):
pl = psutil.pids()
for pid in pl:
if psutil.Process(pid).name() == process_name:
return pid
pass
def check_proc_ex(process_name):
if isinstance(proc_exist(process_name),int):#'chrome.exe'
print('chrome.exe is running')
return 1
else:
print('no such process...')
return 0
pass
#-----------------查找进程,并且将其显示,#这个函数不会查找子窗口
#param: @lpClassName
#指向一个以null结尾的、用来指定类名的字符串或一个可以确定类名字符串的原子。
#如果这个参数是一个原子,那么它必须是一个在调用此函数前已经通过GlobalAddAtom函数创建好的全局原子。
#这个原子(一个16bit的值),必须被放置在lpClassName的低位字节中,lpClassName的高位字节置零。
#@lpWindowName
#指向一个以null结尾的、用来指定窗口名(即窗口标题)的字符串。如果此参数为NULL,则匹配所有窗口名
def show_exe():
hwnd = win32gui.FindWindow(None, u'企业微信')
print(hwnd)
if hwnd != 0:
if win32gui.IsIconic(hwnd):
win32gui.ShowWindow(hwnd,True)
else:
win32gui.ShowWindow(hwnd,False)
win32gui.SetForegroundWindow(hwnd) # 设置前置窗口
pass
class WindowMgr:
"""Encapsulates some calls to the winapi for window management"""
def __init__ (self):
"""Constructor"""
self._handle = None
def find_window(self, class_name, window_name=None):
"""基于类名来查找窗口"""
self._handle = win32gui.FindWindow(class_name, window_name)
return self._handle
def _window_enum_callback(self, hwnd, class_name_wildcard_list):
"""传递给win32gui.EnumWindows(),检查所有打开的顶级窗口"""
class_name,wildcard=class_name_wildcard_list
#if win32gui.GetClassName(hwnd)==class_name and re.match(wildcard, str(win32gui.GetWindowText(hwnd))) is not None:
if re.match(wildcard, str(win32gui.GetWindowText(hwnd))) is not None:
self._handle = hwnd
def find_window_wildcard(self, class_name,wildcard):
"""根据类名,查找一个顶级窗口,确保其类名相符,且标题可以用正则表达式匹配对应的通配符"""
self._handle = None
win32gui.EnumWindows(self._window_enum_callback,[class_name, wildcard])
return self._handle
def set_foreground(self):
"""put the window in the foreground"""
win32gui.SetForegroundWindow(self._handle)
def get_hwnd(self):
"""return hwnd for further use"""
return self._handle
#调用一下键盘输入事件
def other_keys_input(self,s_str):
shell = win32com.client.Dispatch("WScript.Shell")
shell.SendKeys(s_str)
pass
def show_hwnd(self,fore=None):
left = None
top = None
right = None
bottom = None
if self._handle is not None:
#不允许有覆盖的情况,否则无法判断
if win32gui.IsIconic(self._handle):#是否是最小化
win32gui.ShowWindow(self._handle,True)
#print('show top top top')
left, top, right, bottom = win32gui.GetWindowRect(self._handle)
return left,top,right,bottom
else:
print('SetForeground Window ')
#win32gui.ShowWindow(self._handle,False)#已经显示
pythoncom.CoInitialize()
self.other_keys_input('%')
if self._handle is None:
return left,top,right,bottom
if fore is True:
win32gui.SetForegroundWindow(self._handle) #设置前置窗口
#打印窗口 (在屏幕的位置)
#win32gui.SetFocus(self._handle)
left, top, right, bottom = win32gui.GetWindowRect(self._handle)
return left,top,right,bottom
else:
#print("find hwnd failed")
return left,top,right,bottom