Search code examples
wxpythonwxtextctrl

How to get chars that are actually displayed in wx.TextCtrl?


I am trying to underline some specific words in single line wx.textctrl. I'm doing it by calculating position of a word in the control using GetTextExtent and drawing lines using wx.ClintDC.

My problem begins when text in TextCtrl is longer then size of the control, for example if user inputs ~100 chars he can see only 20 last characters if the control is not long enough.

My question is: how can I get a string which is actually displayed in the single line control? or how can I calculate the width of the text which is not displayed (on the left side) in pixels?

Single line text control has no scroll bar. The GetInsertionPoint returns position of caret in text, but it's impossible to translate it to the actual distance in pixels from the left border of control.

I don't want to use TE_RICH or TE_RICH2 styles not StyledTextCtrl class because they don't support right-to-left text.


Solution

  • Ok, the solution I've found is:

    from win32api import SendMessage
    result = SendMessage(self.GetHandle(), EM_POSFROMCHAR, 0, 0)
    

    result is client coordinates of the first character in the control. The value is negative if the specified character is not displayed in the edit control's client area.