Search code examples
windowswinapicomboboxwtl

Mirrored text when changing a combobox RTL style


I'm trying to create a dynamic dialog, which can be made RTL depending on the language. But I have the following issue: whenever I change the RTL style of the combo box, the text comes up reversed. I tried using functions such as InvalidateRect, RedrawWindow, etc., but couldn't make it work correctly.

Relevant code (WinAPI with WTL):

CComboBox combo = hWndCtl;
if(combo.GetCurSel() == 0)
    combo.ModifyStyleEx(WS_EX_LAYOUTRTL, 0);
else
    combo.ModifyStyleEx(0, WS_EX_LAYOUTRTL);

A demo project: here.

A demonstration of the issue:

enter image description here


Solution

  • It seems you are responding to CBN_SELCHANGE notification. This is notification is sent after combobox sets the text in its editbox.

    You should respond to CBN_SELENDOK instead. CBN_SELENDOK is sent before CBN_SELCHANGE, this gives you time to modify the style before combobox sets the text.

    switch (HIWORD(wParam))
    {
    case CBN_SELENDOK:// CBN_SELCHANGE:
        if (SendMessage(hComboBox, CB_GETCURSEL, 0, 0) == 0)
            ModifyStyleEx(hComboBox, WS_EX_LAYOUTRTL, 0);
        else
            ModifyStyleEx(hComboBox, 0, WS_EX_LAYOUTRTL);
        break;
    default:break;
    }
    



    Edit: Windows 10 has fade in/out effect. If you change the combo selection with keyboard, while the color is fading, the text is still goes backward.

    ComboBox has an edit control which might be causing this problem. It's better to use WS_EX_RIGHT | WS_EX_RTLREADING instead of WS_EX_LAYOUTRTL. This will also work with CBN_SELCHANGE.

    case CBN_SELENDOK: //(or CBN_SELCHANGE)
        if (SendMessage(hComboBox, CB_GETCURSEL, 0, 0) == 0)
            ModifyStyleEx(hComboBox, WS_EX_RIGHT | WS_EX_RTLREADING, 0);
        else
            ModifyStyleEx(hComboBox, 0, WS_EX_RIGHT | WS_EX_RTLREADING);
        break;