如何使用 python 和 win32 api 将彩色文本写入文本框?
基本上我想使用 python 将彩色文本写入另一个应用程序的文本框窗口。
总体思路是:
win32gui.SendMessage( hwnd, EM_SETCHARFORMAT, SCF_SELECTION, format);
其中格式是 CHARFORMAT。
我的问题是 EM_SETCHARFORMAT 和 SCF_SELECTION 未包含在 win32con 库中(我认为),并且我不确定如何创建 CHARFORMAT 对象。
这在Python中可能吗?
Basically I want to write colored text to a textbox window of another application using python.
The general idea is to:
win32gui.SendMessage( hwnd, EM_SETCHARFORMAT, SCF_SELECTION, format);
where format is a CHARFORMAT.
My problem is that EM_SETCHARFORMAT and SCF_SELECTION are not included in the win32con library (I think) and I am unsure how to create a CHARFORMAT object.
Is this possible in python?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明,这是很难实现的。问题在于
EM_SETCHARFORMAT
通过引用传递结构。EM_SETCHARFORMAT
不是常见的 Windows 消息之一,它位于WM_USER
范围内。lParam
指向的内存不会跨进程边界进行编组。消息的接收者收到一个指向内存的指针,该指针仅在发送者的进程中有意义。这意味着您唯一的解决方案是使用
WriteProcessMemory
将CHARFORMAT
缓冲区写入目标进程中分配的内存块中。这都是可能的,但相当麻烦,尤其是在 Python 中。如果我是你,我会考虑使用替代解决方案来解决你的问题。
It turns out that this is quite difficult to achieve. The problem is that the
EM_SETCHARFORMAT
passes a structure by reference. TheEM_SETCHARFORMAT
is not one of the common Windows messages, it's in theWM_USER
range. The memory pointed to bylParam
is not marshalled across the process boundary. The receiver of the message receives a pointer to memory that is only meaningful in the sender's process.This means that your only solution will be to use
WriteProcessMemory
to write theCHARFORMAT
buffer into a block of memory allocated in the target process. This is all possible but quite cumbersome, especially in Python.If I were you, I would consider an alternative solution to your problem.