1

I have a C program that has a textbox component. I want to have a python GUI write text to this text box. Currently, I can write to the textbox HWND using:

def winFunc(hwnd, lparam):
    s = win32gui.GetWindowText(hwnd)
    if s == "":
        win32gui.SendMessage(hwnd, win32con.WM_SETTEXT, 0, lparam)

hwnd = win32gui.FindWindow("Graph Program", None)
win32gui.EnumChildWindows(hwnd, winFunc, text)

This code will write text to the textbox but it cannot append text on a newline to the textbox. Is it possible to read in the text that is currently in the textbox or is it possible to add text to a textbox? I am new to using the windows API. Also, Is it possible to write text as a different color? The text box class is RICHEDIT20A.

1 Answer 1

1

Appending text

  1. Send an EM_SETSEL message to move the selection to the end of the rich edit control. Use WM_GETTEXTLENGTH to find out how many characters are in the edit control.
  2. Send an EM_REPLACESEL message to replace the selection. If the selection point is at the end of the control, then replacing is the same as appending.

Don't attempt to use WM_GETTEXT & WM_SETTEXT since it rapidly becomes inefficient, not to mention the fact that the formatting is not preserved.

Formatting text

Use the EM_SETCHARFORMAT to format text. More details over at MSDN.

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

5 Comments

I still can not find a way to change the text color so that each appended line is a new color in python. I was not able to find a way to call EM_SETCHARFORMAT using python.
I was thinking win32gui.SendMessage(hwnd, EM_SETCHARFORMAT, SCF_SELECTION, format) but I also do not know how to (in python) specify a Charformat structure.
I think you may need to use ctypes structure classes to replicate CHARFORMAT. I would also point out that you are meant to ask one question at a time. You asked about appending text, and formatting it.
I have posted a new question to focus on color formatting. stackoverflow.com/questions/6903692/…
@gh4x Good, I think that's exactly what to do.

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.