V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
toono
V2EX  ›  Python

pywin32 怎么获取 windows 的窗体内文本框的内容?

  •  
  •   toono ·
    ToonoW · 2016-12-13 12:55:12 +08:00 · 14624 次点击
    这是一个创建于 2688 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我已经用 spy++去确认我找到了文本框的句柄了。

    用函数 win32gui.SendMessage 获取不了文本框的文本内容,用 str 类型的参数接收获取的内容的话没有获取到东西,而用 PyBuffer 类型去获取则得到类似于 16 进制的东西。

    希望能找到解决方案。 以下是代码:

    from win32gui import *
    from win32api import *
    from win32process import *
    import win32con
    
    import time
    
    time.sleep(3)
    
    # 获取窗体句柄
    hWnd = GetForegroundWindow()
    print('hownd: ', hWnd)
    
    FormThreadID = GetCurrentThreadId()
    print('FormThreadID: ', FormThreadID)
    
    CWndThreadID = GetWindowThreadProcessId(hWnd)
    print('CWndThreadID: ', CWndThreadID)
    
    AttachThreadInput(CWndThreadID[0], FormThreadID, True)
    
    # 获取光标所在文本框句柄
    hWnd = GetFocus()
    print('hWnd: ', hWnd)
    
    AttachThreadInput(CWndThreadID[0], FormThreadID, False)
    
    # SendMessage(hWnd, win32con.WM_SETTEXT, 0, "mextb1860 第一个文本框")
    
    # 文本框内容长度
    length = SendMessage(hWnd, win32con.WM_GETTEXTLENGTH)+1
    print('Length: ', length)
    
    buf = '0'*length
    # 生成buffer对象
    # buf = PyMakeBuffer(length)
    
    # 获取文本框内容
    print('get: ', SendMessage(hWnd, win32con.WM_GETTEXT, length, buf))
    
    print('text: ', buf)
    
    8 条回复    2016-12-27 14:51:48 +08:00
    kanchi240
        1
    kanchi240  
       2016-12-13 13:30:38 +08:00
    鼠标双击,复制到剪贴板
    lishunan246
        2
    lishunan246  
       2016-12-13 14:57:13 +08:00
    # 生成 buffer 对象
    buf = PyMakeBuffer(length)
    print('get: ', SendMessage(hWnd, win32con.WM_GETTEXT, length, buf))
    address, length = PyGetBufferAddressAndLen(buf)
    text = PyGetString(address, length)
    print('text: ', text)
    toono
        3
    toono  
    OP
       2016-12-13 15:42:28 +08:00
    @lishunan246 👍关于 buffer 的讨论搜索到的好少
    toono
        4
    toono  
    OP
       2016-12-13 15:48:15 +08:00
    关于字符转换的另一个解决方案

    https://www.v2ex.com/t/327324
    phrack
        5
    phrack  
       2016-12-13 20:09:37 +08:00 via Android
    不要使用 Python 做这个工作
    toono
        6
    toono  
    OP
       2016-12-14 09:28:45 +08:00
    @phrack 公司的客户端就是 python 写的,要实现这个需求。。。
    phrack
        7
    phrack  
       2016-12-25 20:40:16 +08:00 via Android
    @toono 你完全可以用另外的语言写一个模块甚至是编译个 exe 也可以,然后 Python 调用这个模块就好了。
    toono
        8
    toono  
    OP
       2016-12-27 14:51:48 +08:00
    @phrack 你说这是个不错的办法~
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3282 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 13:07 · PVG 21:07 · LAX 06:07 · JFK 09:07
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.