3

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?

3
  • Do you have a short app that you could post that we could use as a test bed. I think I could put together a ctypes based CHARFORMAT in short time, but I can't really test it without building an app that has a rich edit, message pump etc. Commented Aug 1, 2011 at 19:59
  • Unfortunately, I do not own the application that has the rich edit box and I can not send it or view its source to make a smaller app. However, I checked WordPad (on windows) and I found that it uses a rich edit also. I hope this helps. Commented Aug 1, 2011 at 20:06
  • I had a go and got 99% of the way there, but can't seem to get over the final hurdle! Grr! Commented Aug 1, 2011 at 21:07

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Booo!! Thanks for the help though!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.