Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / MFC

Setting Write Direction and Alignment

5.00/5 (4 votes)
4 Jul 2020CPOL 7.2K   56  
How to set write direction and alignment
Here is a tip how to set the writing direction (RTL or LTR) along with the alignment of a CEdit control.

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:

C++
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:

Image 1

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

Image 2

History

  • 4th July, 2020: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)