65.9K
CodeProject is changing. Read more.
Home

Setting Write Direction and Alignment

starIconstarIconstarIconstarIconstarIcon

5.00/5 (4 votes)

Jul 4, 2020

CPOL
viewsIcon

7200

downloadIcon

56

How to set write direction and alignment

Introduction

Sometimes, you want your CEdit control to be set to the language used, which can be Left to Right (default) or Right to Left.

  • Arabic
  • Aramaic
  • Azeri
  • Dhivehi/Maldivian
  • Hebrew
  • Kurdish (Sorani)
  • Persian/Farsi
  • Urdu

See this article for further reading.

Using the Code

Assuming your control name is m_MyEdit, here is the code:

void MyDialog::SetLangDirection(bool RTL)
{
    wprintf(L"Setting language direction to %s", (RTL) ? L"right to left" : L"left to right");
    DWORD w_dwStyle;

    w_dwStyle = GetWindowLong(m_MyEdit.GetSafeHwnd(), GWL_EXSTYLE);

    if (RTL)
    {
        w_dwStyle -= WS_EX_LEFT | WS_EX_LTRREADING;
        w_dwStyle |= WS_EX_RIGHT | WS_EX_RTLREADING;
    }
    else
    {
        w_dwStyle -= WS_EX_RIGHT | WS_EX_RTLREADING;
        w_dwStyle |= WS_EX_LEFT | WS_EX_LTRREADING;
    }

    SetWindowLong(m_MyEdit.GetSafeHwnd(), GWL_EXSTYLE, w_dwStyle);
}

When you press the RTL button, you should be able to see text aligned to the right, with RTL writing direction:

If you press the LTR, the text will be aligned to the left with LTR writing direction:

History

  • 4th July, 2020: Initial version