python - 将文本编码为 win32api.SendMessage() 的 WPARAM

标签 python windows winapi pywin32 sendmessage

我正在尝试调用 win32api.SendMessage()通过我的 Python 应用程序(即发送应用程序)。

接收应用程序的 API 声明消息的格式为:::SendMessage(<app_name>, <msg_name>, (WPARAM) <value> )

然而,value实际上是 3 到 4 个字符(无空格)的字符串。

我的问题

win32api.SendMessage的正确使用方法是什么? ,特别是关于 value

我可以简单地输入字符串吗,如: win32api.SendMessage(<app_name>, <msg_name>, "ABC")

或者我是否需要将字符串转换为 WPARAM 类型(如果需要,我该怎么做)?

我一直在 Linux Python 中进行开发,对 Windows 和 C++ 的经验很少。非常感谢任何帮助。

提前致谢!

附言根据评论,接收应用程序实际上是 AmiBroker,API 文档中给出的实际消息格式是: ::SendMessage( g_hAmiBrokerWnd, WM_USER_STREAMING_UPDATE, (WPARAM) Ticker, (LPARAM) &recentInfoStructureForGivenTicker ); 我前面提到的'string'是'Ticker',作者说是一个string (char*) .我最初没有包含它,因为我认为实际的消息格式并不重要。

研究:我读自 this WPARAM 本质上是一个整数类型,并且 this带我到win32api .在我阅读的众多文章中;他们都没有帮助回答我上面的问题(或者至少我认为是这样)。

最佳答案

[Github]: mhammond/pywin32 - Python for Windows (pywin32) ExtensionsWINAPIPython 包装器,因此设计为 Python 友好

[ActiveState.Docs]: win32api.SendMessage (我能找到的最好的文档),是 [MS.Docs]: SendMessage function 的包装器.

lParam(最后一个)参数是一个LONG_PTR,这意味着它拥有一个可以指向任何内容的内存地址。通常这是用来传递字符串等数据的。

因为我不知道你想发什么消息,我花了一些时间才找到[MS.Docs]: EM_REPLACESEL message .

code0.py:

#!/usr/bin/env python3

import sys
import win32api
import win32gui
import win32con


is_py2 = sys.version_info.major < 3

if is_py2:
    _input = input
    input = raw_input


def main():
    np_wnd = win32gui.FindWindow(None, "Untitled - Notepad")
    if not np_wnd:
        print("Cound not get Notepad window")
        return
    np_edit_wnd = win32gui.GetWindow(np_wnd, win32con.GW_CHILD)
    if not np_edit_wnd:
        print("Cound not get Notepad child window")
        return
    heading = "After pressing ENTER, "
    #'''
    input("{:s}all text in Notepad will be selected ... ".format(heading))
    # HERE's when the 1st screenshot was taken
    win32api.SendMessage(np_edit_wnd, win32con.EM_SETSEL, 0, -1)
    replaced_text0 = "Replaced\nmultiline text."
    input("{:s}Notepad text will be set (via EM_REPLACESEL) to: \n\"\"\"\n{:s}\n\"\"\" ... ".format(heading, replaced_text0))
    win32api.SendMessage(np_edit_wnd, win32con.EM_REPLACESEL, 0, replaced_text0)  # Regular string
    # HERE's when the 2nd screenshot was taken. It was at the end of the program (at that time), but some stuff was added
    replaced_text1 = "Other\nreplaced\n\nnmultiline text."
    input("\n{:s}Notepad text will be set (via WM_SETTEXT) to: \n\"\"\"\n{:s}\n\"\"\" ... ".format(heading, replaced_text1))
    win32api.SendMessage(np_edit_wnd, win32con.WM_SETTEXT, 0, replaced_text1)
    if not is_py2:
        return
    #'''
    print("\nFor Python 2, also get the text back from Notepad")
    buf_size = 255
    buf = win32gui.PyMakeBuffer(buf_size)
    text_len = win32api.SendMessage(np_edit_wnd, win32con.WM_GETTEXT, buf_size, buf)
    print("    Original text length: {:d}\n    Retrieved text length: {:d}\n    Text: \"\"\"\n{:s}\n    \"\"\"".format(len(replaced_text1), text_len, buf[:text_len]))


if __name__ == "__main__":
    print("Python {:s} on {:s}\n".format(sys.version, sys.platform))
    main()
    print("\nDone.")

结果:

  • 初始状态:

    Img0

  • 最终状态:

    Img1

输出:

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q056331657]> "e:\Work\Dev\VEnvs\py_064_02.07.15_test0\Scripts\python.exe" code0.py
Python 2.7.15 (v2.7.15:ca079a3ea3, Apr 30 2018, 16:30:26) [MSC v.1500 64 bit (AMD64)] on win32

After pressing ENTER, all text in Notepad will be selected ...
After pressing ENTER, Notepad text will be set (via EM_REPLACESEL) to:
"""
Replaced
multiline text.
""" ...

After pressing ENTER, Notepad text will be set (via WM_SETTEXT) to:
"""
Other
replaced

nmultiline text.
""" ...

For Python 2, also get the text from Notepad
    Original text length: 32
    Retrieved text length: 32
    Text: """
Other
replaced

nmultiline text.
    """

Done.

正如所见,它适用于普通的 Python 字符串。

注意:我的 Win 用户拥有“ super ”管理权限。对于普通用户,某些功能可能无法按预期工作。

你也可以看看 [SO]: Keyboard event not sent to window with pywin32 (@CristiFati's answer)用于处理消息之类的WM_CHAR更重要的是:如何处理子窗口

@EDIT0:

添加:

  • WM_SETTEXT
  • WM_GETTEXT(仅限 Python 2)- 展示如何从 SendMessage
  • 获取字符串

但是由于 WM_USER_STREAMING_UPDATE 超出了 WM_USER(顺便说一句,我没有看到它的任何文档),事情可能/不会起作用(根据@IInspectable 的评论以及SendMessage 的文档),因此需要额外的工作(数据编码)。

@EDIT1:

我已经注意到您正在尝试使用 AmiBroker(通过 Googleing WM_USER_STREAMING_UPDATE)。
但是,我找不到该消息的任何(官方)文档,它会揭示 WPARAMLPARAM 参数应该包含的内容(例如: [MS.Docs]: WM_SETTEXT message)。
您是在尝试编写一个插件(意味着您与 AmiBroker 在同一个进程中),还是只是想向它发送消息(就像我在示例中所做的那样:Python -> 记事本)?

关于python - 将文本编码为 win32api.SendMessage() 的 WPARAM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56331657/

相关文章:

python - 使用 matplotlib 绘制带有纪元时间 x 轴的图表

c++ - Windows 上 C++ 的声音处理 - 朝着正确方向轻推

windows - 从 SVN 迁移到 GIT 时的外部

Python 不会运行或保存特定脚本?

Python。如何从 JSON 获取目录路径?

python - 在没有任何 python 子包的情况下创建 pip 轮

winapi - CreateProcess 和奇怪的 nslookup 错误

c++ - 仅在需要时加载 DLL

c - <windows.h> 函数触发 Pelles C 中的 POLINK 错误

windows - 命名管道高效异步设计