How can I write color text to a textbox using pyth

2019-05-11 23:50发布

问题:

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?

回答1:

It turns out that this is quite difficult to achieve. The problem is that the EM_SETCHARFORMAT passes a structure by reference. The EM_SETCHARFORMAT is not one of the common Windows messages, it's in the WM_USER range. The memory pointed to by lParam 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 the CHARFORMAT 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.