1.需求分析
在使用Python编程的时候,通常会遇到一个问题,当前用户打开很多任务,QQ,Wechat,chome浏览器,Firefox浏览器,以及文件夹。那么如何在众多任务中选择并且激活指定的窗口呢。
2. 解决方法分析
因为这些窗口是window平台的,因此我们可以想到的是调用window的库。因此需要导入一些Python的windows库,也就是pywin32,然后查找相关函数,并调用以此达到效果
3.解决步骤
1.安装pytho2.7
2.安装pywin32
4.代码写作与分析:
from win32gui import *
import time
import os
titles = set()
def EnumWindowsProc (hwnd,mouse):
if IsWindow(hwnd) and IsWindowEnabled(hwnd) and IsWindowVisible(hwnd) and GetWindowText(hwnd) != "":
text = GetWindowText(hwnd)
titles.add(text)
classname = GetClassName(hwnd)
print "the window's text = %s \nhwnd = %d classname = %s" % (text,hwnd,classname)
SetForegroundWindow(hwnd)
print ShowWindow(hwnd,SW_SHOW)
time.sleep(3)
def PrintSort_ALL_visable_window():
EnumWindows(EnumWindowsProc, 1)
lt = [t for t in titles if t]
lt.sort()
print lt
for t in lt:
print t
def GetCurForWin():
for i in range(1,2):
time.sleep(1)
hw = win32gui.GetForegroundWindow()
text = GetWindowText(hw)
desk = win32gui.GetDesktopWindow()
print "Current window's hw = %d, desk = %d, text = %s" % (hw,desk,text)
class WindowFinder:
"Class to find and make focus on a particular Native OS dialog/Window "
def __init__ (self):
self._handle = 527588
def find_window(self, class_name, window_name = None):
"Pass a window class name & window name directly if known to get the window"
self._handle = win32gui.FindWindow(class_name, window_name)
def _window_enum_callback(self, hwnd, wildcard):
if re.match(wildcard, str(win32gui.GetWindowText(hwnd))) != None:
self._handle = hwnd
def find_window_wildcard(self, wildcard):
"This function takes a string as input and calls EnumWindows to enumerate through all open windows"
self._handle = None
win32gui.EnumWindows(self._window_enum_callback, wildcard)
def set_foreground(self):
"Get the focus on the desired open window"
win32gui.SetForegroundWindow(self._handle)
def SetForWin(hwnd):
SetForegroundWindow(hwnd)
SetWindowPos(hwnd,HWND_TOP,0,0,0,0,win32con.SWP_SHOWWINDOW)
def GetWindByTitile():
hwnd = FindWindow(None, "RIDE - Win Operate")
text = GetWindowText(hwnd)
print text
SetForegroundWindow(hwnd)
ShowWindow(hwnd,SW_RESTORE)
if __name__ == '__main__':
print dir(win32api)
PrintSort_ALL_visable_window()
print GetDesktopWindow()
GetWindByTitile()
代码块语法遵循标准markdown代码,例如:
@requires_authorization
def somefunc(param1='', param2=0):
'''A docstring'''
if param1 > param2:
print 'Greater'
return (param2 - param1 + 1) or None
class SomeClass:
>>> message = '''interpreter
... prompt'''
代码分析,首先应该获取所有的进程窗口,并且进行过滤,过滤的条件为可视的,而且它的title不能为空
然后通过回调函数获取其hwnd和title
然后根据要激活置顶的窗口名字,查找其hwnd。接着调用两个很重要的函数
SetForegroundWindow(hwnd) ----设置其为最前,但设置之后并不能让它显示出来,接着就要显示它
ShowWindow(hwnd,SW_SHOW)
显示的这个函数的第二个参数比较重要,它决定着窗口如何显示
ParametershWnd [in]Type: HWND
A handle to the window.
nCmdShow [in]Type: int
Controls how the window is to be shown. This parameter is ignored the first time an application calls ShowWindow, if the program that launched the application provides a STARTUPINFO structure. Otherwise, the first time ShowWindow is called, the value should be the value obtained by the WinMain function in its nCmdShow parameter. In subsequent calls, this parameter can be one of the following values.
ValueMeaningSW_FORCEMINIMIZE11Minimizes a window, even if the thread that owns the window is not responding. This flag should only be used when minimizing windows from a different thread.
SW_HIDE0Hides the window and activates another window.
SW_MAXIMIZE3Maximizes the specified window.
SW_MINIMIZE6Minimizes the specified window and activates the next top-level window in the Z order.
SW_RESTORE9Activates and displays the window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when restoring a minimized window.
SW_SHOW5Activates the window and displays it in its current size and position.
SW_SHOWDEFAULT10Sets the show state based on the SW_ value specified in the STARTUPINFO structure passed to the CreateProcess function by the program that started the application.
SW_SHOWMAXIMIZED3Activates the window and displays it as a maximized window.
SW_SHOWMINIMIZED2Activates the window and displays it as a minimized window.
SW_SHOWMINNOACTIVE
Displays the window as a minimized window. This value is similar to SW_SHOWMINIMIZED, except the window is not activated.
SW_SHOWNA8Displays the window in its current size and position. This value is similar to SW_SHOW, except that the window is not activated.
SW_SHOWNOACTIVATE4Displays a window in its most recent size and position. This value is similar to SW_SHOWNORMAL, except that the window is not activated.
SW_SHOWNORMAL1Activates and displays a window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when displaying the window for the first
Return value
Type:
Type: BOOL
If the window was previously visible, the return value is nonzero.
If the window was previously hidden, the return value is zero.
1.需求分析 在使用Python编程的时候,通常会遇到一个问题,当前用户打开很多任务,QQ,Wechat,chome浏览器,Firefox浏览器,以及文件夹。那么如何在众多任务中选择并且激活指定的窗口呢。 2. 解决方法分析 因为这些窗口是window平台的,因此我们可以想到的是调用window的库。因此需要导入一些Python的windows库,也就是pywin32,然后查找
第一章 程序 1
1.1 程序 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 什么是调试 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 调试 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.4 程序语言和自然语言 . . . . . . . . . . . . . . . . . . . . . . . 4
1.5 第一个程序 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
第二章 变量,表达式和语句 6
def
window
EnumerationHandler(hwnd,
window
list):
window
list.append((hwnd, win32gui.Get
Window
Text(hwnd)))
#通过枚举获取所有
窗口
的句柄和标题
window
list = []
win32gui.Enum
Windows
(
window
EnumerationHandler,
window
list)
#遍历所有
窗口
,指定要
操作
的
窗口
的标题的关键词,比如“记事本”
二、定义应用程序类的一个对象 -- app = wx.App()
三、创建一个顶层
窗口
的wx.Frame类的对象 --
window
= wx.Frame(父
窗口
, title="...", size=(宽, 高))
四、定义Panel对象 -- panel = wx.Panel(
window
)
五、添加一个静态文本对象 --lable = wx.StaticText(panel, label="xxx", pos=(x, y))
hwnd = win32gui.Find
Window
(lpClassName=None, lp
Window
Name=None) # 查找
窗口
,不找子
窗口
,返回值为0表示未找到
窗口
hwnd = win32gui.Find
Window
Ex(hwndParent=0, hwndChildAfter=0, lpszClass=None, lpsz
Window
=None) # 查找子
窗口
,返回值为0表示未找到子
窗口