November 22nd, 2018

How can I make a dialog box right-to-left at runtime?

A customer had a program that decided at runtime what language to use for its user interface. The customer was having trouble getting the program’s dialog boxes to show up with right-to-left layout when necessary.

We are setting the WS_EX_LAYOUT­RTL extended style in the dialog box’s WM_INIT­DIALOG message handler, but what we’re seeing is that although the style applies successfully, and the dialog itself renders RTL, its child elements don’t.

× emaN a esoohC
Name:
John Smith
OK

The actual screen shot provided by the customer involved Arabic text, but I used English text so you can see which parts are running left-to-right and which parts are right-to-left. Also, because I don’t know Arabic.

The title bar is running right-to-left, as desired, but the contents of the dialog are still left-to-right. What’s going on?

As I’ve noted before, the WS_EX_LAYOUT­RTL extended style is inherited by child windows (unless blocked by the WS_EX_NO­INHERIT­LAYOUT extended style). What I didn’t call out is that this inheritance occurs at the point the child window is created. The child window takes a snapshot of its parent window’s layout; future changes to the parent window’s layout have no effect.

The next piece of the puzzle is realizing that the WM_INIT­DIALOG message is sent after the child windows have been created. I called this out explicitly in an earlier discussion of dialog boxes, but you already know this, because your WM_INIT­DIALOG message handler calls Get­Dlg­Item to obtain handles to dialog child windows in order to initialize and configure them.

So you need to get the WS_EX_LAYOUT­RTL onto the window before child windows are created. One way of doing this is to edit the dialog template and add (or remove) the WS_EX_LAYOUT­RTL extended style from the dwExStyle member of the 32-bit extended header before you call Create­Dialog­Indirect or one of its relatives.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.