i'm trying getting the richedit caret positions. seems that i can't use the caret functions.

LONG firstCharIndex(HWND hwnd)
    {
        POINT pt;
        pt.x=0;
        pt.y=0;
        LONG n = SendMessage(hwnd, EM_CHARFROMPOS, 0, (LPARAM)&pt);
        return LOWORD(n);
    }

   COORD GetCaretPos( )
        {
            COORD pos;
            //getting the line from caret position:
            pos.Y=(LONG) SendMessage(consoleedit, EM_LINEFROMCHAR,(WPARAM)-1,0);
            CHARRANGE cr;
            cr.cpMin = 0;
            cr.cpMax = 0;
            SendMessage(consoleedit, EM_EXGETSEL, 0, (LPARAM)&cr);
            pos.X=cr.cpMin - firstCharIndex(consoleedit);
            DebugText("min: " + to_string(cr.cpMin) + " first: " + to_string(firstCharIndex(consoleedit)) );
            return pos;
        }

i get the Y correctly, but i don't understand how can i get the X.. can anyone advice me?

Edited 1 Year Ago by cambalinho

the GetCaretPos() don't works on richedit :(
unless, first, i need recreate it?

Sorry about that. I was guessing you are on a win32/64 app. Let's hear a little more detail. The user32.dll should work but of course not on non-windows.

nullptr: that gives me the character position on where is the caret. but give me the text position and not the line position.(the coordenates are gived by entire text, and not line position). i can't divide with number of lines because the lines don't have the same lenght deppending on it's font

finally it's working greate:

void SetCaretPos(POINT pos)
    {
        LONG characterIndexLine =0;
        characterIndexLine=(LONG) SendMessage(consoleedit, EM_LINEINDEX, (WPARAM) pos.y-1,0);
        characterIndexLine+=(LONG)pos.x;
        CHARRANGE cr;
        cr.cpMin = characterIndexLine;
        cr.cpMax = characterIndexLine;
        SendMessage(consoleedit, EM_EXSETSEL, 0, (LPARAM)&cr);
    }

    COORD GetCaretPos( )
    {
        COORD pos;
        //getting the line where is the caret(WPARAM is -1 for the caret position):
        pos.Y=(LONG) SendMessage(consoleedit, EM_LINEFROMCHAR,(WPARAM)-1,0)+1;

        //getting the caret on text position:
        CHARRANGE cr;
        cr.cpMin = 0;
        cr.cpMax = 0;
        SendMessage(consoleedit, EM_EXGETSEL, 0, (LPARAM)&cr);

        //getting the 1st charater line position(is gived on text position). for use the caret position, we can use -1:
        LONG firstcharacter=SendMessage(consoleedit, EM_LINEINDEX, (WPARAM)-1,0);

        //the caret text position minus the 1st character on that line, will give me the x:
        pos.X=cr.cpMin-firstcharacter;
        return pos;
    }

why on set '-1' and on get '+1'?
because the lines are indexed by zero.
thanks for all
PS: readers when we need use 1 function name igual of the win32 function name, we must use '::' before the win32 functions name ;)

nullptr: you tryied tell me something. but with more search i get it.. thanks

Edited 1 Year Ago by cambalinho

This question has already been answered. Start a new discussion instead.