Search code examples
c#newlineeditunmanagedrichedit

Is there a way to test if a control is Edit or RichEdit?


Basically, I have written a program in C# that grabs text and the current caret position from any window handle using user32.dll. After manipulating the text, the program sets the text of the control to the new string and also resets the caret position.

The issue I am running into is that EM_GETSEL returns an caret position for RichEdit controls based on the number of CR+LF proceeding the caret position.

Edit controls treat these as two characters, while RichEdit controls treat them as one "character".

Is there anyway that I can detect which of these two a control may be by windows messages?

Or is there a better way that I can detect caret position for unmanaged controls?


Solution

  • Alright everyone, thank you for your help! This explanation is a little off the cuff, so bear with me.

    I had a breakthrough today, and I was able to determine a simple way to tell the difference between an Edit or RichEdit control.

    Since we know that WM_GETTEXT returns the original string, and EM_GETSEL will be off by 1 for every newline, all we have to do is check for the following:

    1. Are we off of the the first line (line 0)?
    2. If we are greater than line zero, use EM_LINEINDEX (I use a loop here to increase wParam and I'll explain why in just a second) to get a substring from character 0 to EM_LINEINDEX
    3. Check if that substring contains a \r (If not, then you have a wordwrap somewhere. This is why I use the loop in step 2)
    4. Once you find a substring \r, does that substring also contain \n?

    Again, this works because even EM_LINEINDEX is off by 1 character in a RichEdit against WM_GETTEXT and therefore a substring from character 0 until the EM_LINEINDEX after your first newline will not contain a \n if your control is a RichEdit.