0

I am trying to develop a custom Text Editor Control and wish to use EM_CHARFROMPOS to find the character under the mouse cursor. This would be easy if I was inheriting from the RichTextBox or TextBox Controls however I wish to do all myself, not even trying to inherit from TextBoxBase.

Is there any way to do this, what data does this message operate on and can I use it? Sorry no code, at the moment using Panel as my base.

Thanks Danny.

dannyhut
  • 179
  • 11
  • You can look at the `TextBox` and `RichTextBox` implementations at https://github.com/dotnet/winforms/ – RussKie Jan 26 '20 at 03:02
  • @RussKie. Thanks for the reply, have been searching through both TextBox and RichTextBox sourcecode and can find nothing. Might be me. – dannyhut Jan 26 '20 at 03:18
  • see: [GetCharFromPosition](https://referencesource.microsoft.com/System.Windows.Forms/R/40a330df1eb1c563.html) – TnTinMn Jan 26 '20 at 03:24
  • @TnTinMn. Have looked at GetCharFromPosition, wont work here as I don't use the underling data the EM_CHARFROMPOS does. Am happy to use it if I can. – dannyhut Jan 26 '20 at 03:53
  • In the latest Windows Forms codebase the constant is renamed. https://github.com/dotnet/winforms/search?q=CHARFROMPOS&unscoped_q=CHARFROMPOS – RussKie Jan 26 '20 at 04:22
  • @RussKie. I'm using the latest codebase, direct from Microsofts website I believe.What constant is renamed? – dannyhut Jan 26 '20 at 04:38
  • Please follow the link I posted above. – RussKie Jan 26 '20 at 06:27
  • @RussLie I have followed your link and as I stated above it will not work for me. What is the code that handles the message, I can send it but the Panel Control will not handle it. – dannyhut Jan 26 '20 at 07:21
  • That message is directed to an Edit control. An Edit control already buffers these position and returns a value it already knows of or uses internal, existing, methods to calculate it. A Panel doesn't have any of this, of course, and implementing that logic is way more complex than you probably think. In a more *trivial* context, you can use [`TextRenderer.MeasureText`](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.textrenderer.measuretext) and/or [`MeasureCharacterRanges`](https://learn.microsoft.com/en-us/dotnet/api/system.drawing.graphics.measurecharacterranges) – Jimi Jan 26 '20 at 07:22
  • This all implies that you need to do all the math and all the buffering yourself. – Jimi Jan 26 '20 at 07:26
  • @Jim I agree that it seems I'll have to do all the calculations myself, I was just hoping to avoid this by accessing the data TextBox and RichTexBox uses to do its thing or getting some hints by veiwing the code that does in these Controls. – dannyhut Jan 26 '20 at 07:34
  • 1
    The code that performs the heavy lifting, *connects* to the text rendering sub-system and [Uniscribe](https://learn.microsoft.com/en-us/windows/win32/intl/uniscribe), is not public. The .Net controls just invoke the native controls' functionalities (a subset of those, actually). – Jimi Jan 26 '20 at 07:46
  • Btw, I have implemented the methods provided by a TextBoxBase class (`GetFirstCharIndexFromLine`, `GetLineFromCharIndex`, `GetPositionFromCharIndex` etc.) for a Label control [here](https://stackoverflow.com/a/48615379/7444103). But, as you can see, it's for a limited environment and a Label control already manages text rendering functionality on its own. A Panel doesn't. – Jimi Jan 26 '20 at 07:55

0 Answers0