Search code examples
c++winapicaretrichedit

Hiding caret in RichEdit winapi


I would like to hide a caret from a RichEdit(50W) with ES_READONLY style specified. It's pretty confusing for the user, when the caret is blinking and the user can't type.
I tried to hide the caret using HideCaret() function,
however it doesn't work for me with following code:

LRESULT CALLBACK ChatMessaegsSubclassProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) // Subclassed control
{
    LRESULT ret = CallWindowProc(WndProc_ChatMessages, hwnd, msg, wParam, lParam);
    switch(msg)
    {
    //Also tried with EN_SETFOCUS
    case WM_SETFOCUS:
    {
        ret = CallWindowProc(WndProc_ChatMessages, hwnd, msg, wParam, lParam);
        HideCaret(ChatMessages); //Returns 5 (Access denied.)
        break;
    }

    //According the documentation:
    //If your application calls HideCaret five times in a row, 
    //it must also call ShowCaret five times before the caret is displayed.
    case WM_KILLFOCUS: //The message is called when the RichEdit get focus, however nothing happens.
    {
        ret = CallWindowProc(WndProc_ChatMessages, hwnd, msg, wParam, lParam);
        ShowCaret(ChatMessages);
        break;
    }
    }
    return ret;
}

Solution

  • Here is the solution:

    LRESULT CALLBACK ChatMessaegsSubclassProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        LRESULT ret = CallWindowProc(WndProc_ChatMessages, hwnd, msg, wParam, lParam);
        switch(msg)
        {
        case WM_LBUTTONDOWN:
        {
            HideCaret(ChatMessages);
            break;
        }
        case WM_KILLFOCUS:
        {
            ShowCaret(ChatMessages);
            break;
        }
        }
        return ret;
    }
    

    NOTE this only works when user induces the focus with mouse. Therefore if anyone knows how to deal with it correctly, feel free to answer, I'll be glad.