Click here to Skip to main content
15,867,308 members
Articles / Desktop Programming / MFC
Tip/Trick

Setting Write Direction and Alignment

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
4 Jul 2020CPOL 6.3K   55   1   6
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)


Written By
CEO Secured Globe, Inc.
United States United States
Michael Haephrati is a music composer, an inventor and an expert specializes in software development and information security, who has built a unique perspective which combines technology and the end user experience. He is the author of a the book Learning C++ , which teaches C++ 20, and was published in August 2022.

He is the CEO of Secured Globe, Inc., and also active at Stack Overflow.

Read our Corporate blog or read my Personal blog.





Comments and Discussions

 
SuggestionPlease Add '.exe' file of this Pin
Mahdi Askarpoor20-Jan-22 9:42
Mahdi Askarpoor20-Jan-22 9:42 
AnswerRe: Please Add '.exe' file of this Pin
Michael Haephrati20-Jan-22 10:05
professionalMichael Haephrati20-Jan-22 10:05 
SuggestionPlease Add '.exe' file of this Pin
Mahdi Askarpoor20-Jan-22 9:53
Mahdi Askarpoor20-Jan-22 9:53 
AnswerRe: Please Add '.exe' file of this Pin
Michael Haephrati20-Jan-22 10:05
professionalMichael Haephrati20-Jan-22 10:05 
Questionhi Pin
Member 148839938-Jul-20 0:50
Member 148839938-Jul-20 0:50 
AnswerRe: hi Pin
Michael Haephrati20-Jan-22 10:06
professionalMichael Haephrati20-Jan-22 10:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.