html tool

2014年3月14日星期五

目的:从windows的聊天窗口中读取内容。计划测试qq和gtalk (1)

目的:从windows的聊天窗口中读取内容。计划测试qq和gtalk
过程:
1.在打开窗口中找到聊天窗口---is ok
2.在被找到的窗口中读取内容

内容记录:
[popexizhi]1.py中的win32gui,win32api的使用
下载了三个版本的安装包如下:
pywin32-213.win32-py2.6.exe
pywin32-214.win32-py2.6.exe
pywin32-216.win32-py2.6.exe
但安装时都提示register中没有python,无法安装。郁闷我安装py2.6在D盘是绿色版本的。google了一下,要手动在注册表中注册一下,代码如下,

ok了:
--------------------------------------------------------------------------------------------------------
import sys
 
from _winreg import *
 
# tweak as necessary
version = sys.version[:3]
installpath = sys.prefix
 
regpath = "SOFTWARE\\Python\\Pythoncore\\%s\\" % (version)
installkey = "InstallPath"
pythonkey = "PythonPath"
pythonpath = "%s;%s\\Lib\\;%s\\DLLs\\" % (
    installpath, installpath, installpath
)
 
def RegisterPy():
    try:
        reg = OpenKey(HKEY_CURRENT_USER, regpath)
    except EnvironmentError as e:
        try:
            reg = CreateKey(HKEY_CURRENT_USER, regpath)
            SetValue(reg, installkey, REG_SZ, installpath)
            SetValue(reg, pythonkey, REG_SZ, pythonpath)
            CloseKey(reg)
        except:
            print "*** Unable to register!"
            return
        print "--- Python", version, "is now registered!"
        return
    if (QueryValue(reg, installkey) == installpath and
        QueryValue(reg, pythonkey) == pythonpath):
        CloseKey(reg)
        print "=== Python", version, "is already registered!"
        return
    CloseKey(reg)
    print "*** Unable to register!"
    print "*** You probably have another Python installation!"
 
if __name__ == "__main__":
    RegisterPy()
----------------------------------------------------------------------------------------------------------------
但是之后的安装pywin32-213.win32-py2.6.exe提示有系统动态库问题,自己的是xp sp3,在下载这几个安装文件时pope就郁闷很久这个的差异是什

么,试一试运气pywin32-214.win32-py2.6.exe安装成功了,想来这个213,214,216是针对不同操作系统版本吧:)只是猜想没有查。
PS:安装完成后帮助文档在Lib\site-packages下

2.关于查找窗口句柄
小插曲:查找窗口名称使用re.match是不可以的,要使用re.search.因为窗口名称中多包含地址,而包含地址的内容有\/:等特殊字符影响正则表达

的处理,re.search想必是对这些字符做转移处理了:)
help中如下解释
re.search(pattern, string[, flags])
Scan through string looking for a location where the regular expression pattern produces a match, and return a corresponding

MatchObject instance. Return None if no position in the string matches the pattern; note that this is different from finding a

zero-length match at some point in the string.
re.match(pattern, string[, flags])
If zero or more characters at the beginning of string match the regular expression pattern, return a corresponding MatchObject

instance. Return None if the string does not match the pattern; note that this is different from a zero-length match.

Note
If you want to locate a match anywhere in string, use search() instead

pope使用win32gui自己写了一个查找包含窗口名称的类如下,但没有写testcase:)留到下面再不上吧
-----------------------------------------------------------------------------------------------------------
# -*- coding=utf-8 -*-
# 从当前打开的全部窗口中查找标题包含指定内容的窗口句柄
import re,time
try:
import win32gui
import win32api
except:
win32gui=None

class winSeach():
def __init__(self,strcon):
"""查找包含strcon内容的窗口 """
self.__strcon=strcon
self.__hwnlist=[] #包含查找结果的tuple

def doseach(self,hwnd,ctx):
if win32gui.IsWindowVisible(hwnd):
name=win32gui.GetWindowText(hwnd)
#print name
if None == re.search(self.__strcon,name) :
pass
#print name,"is not in",self.__strcon
else:
self.__hwnlist.append(hwnd)
#print self.__hwnlist,"is else"

def getres(self):
win32gui.EnumWindows(self.doseach,None)
return self.__hwnlist

if __name__=="__main__":
test=winSeach("py")
for x in test.getres():
print x,win32gui.GetWindowText(x)
#win32gui.SetForegroundWindow(x) #将查找到的窗口设置到最前面

-------------------------------------------------------------------------------------------------------------

没有评论:

发表评论