We help IT Professionals succeed at work.

right-to-left style in a CEdit box

cramer091498
cramer091498 asked
on
I have created a CEdit box with no special styles applied. Its in a dialog.
I then, using SubclassDlgItem, case the CEdit into a CMyEdit.
I want to then change the CEdit so it uses the style WS_EX_RIGHT.
But when I apply the style (using ModifyStyleEx) I cannot see any change.
How do I change this style?

thanks
cel
Comment
Watch Question

BRONZE EXPERT

Commented:
Hi cramer,

first I need to say that WS_EX_RIGHT 'style has an effect only if the shell language is Hebrew, Arabic, or
another language that supports reading-order alignment; otherwise, the style is ignored.' (quote from
MSDN). I hope you know this.

Further, maybe (and I can't test coz I don't have environment where this style has an effect) the
problem is that in windows some styles only affect the creation of a control but don't have any
effect when changed after the object has been created (i.e. ES_MULTILINE). So, you can try
to create the edit-control at runtime using CWnd::CreateEx, i.e.:

CEdit* pEdit = new CEdit;
// even add WS_EX_CLIENTEDGE to give the edit-control the 3D-look
pEdit->CreateEx( WS_EX_CLIENTEDGE|WS_EX_RIGHT, "EDIT", NULL, WS_CHILD|WS_VISIBLE|WS_TABSTOP|WS_BORDER, CRect(100,100,200,125),
this, 1001);
// BTW, to set the edit control's font to the same as used in the dialog do this:
pEdit->SetFont( GetFont() );

hope that helps,

ZOPPO
ekc

Commented:
What are you passing as the third parameter of the ModifyStyleEx() function.
If you are not passing nothing, the default is 0. Try with
SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE

Author

Commented:
thanks for the comments.
Yes, I can create the CEdit using CreateEx and the style works ok. But I don't want to. I have an application with loads of dialog boxes in resource files and I can't change that.
WS_EX_RIGHT works with any font as far as I can see. It just right justifies text in the CEdit box.

Trying SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE makes no difference.

The first thing I did was read the online help. And I am aware that WS_EX_RIGHT may not be settable at runtime. But I don't know.
Thats why I'm here.

cel
BRONZE EXPERT

Commented:
You can simply check the 'Right aligned text' check box in edit-control's
property-tab 'Extended styles'?

Author

Commented:
thanks for that.
But thats not want I want to do. If I could get away with that I would.
My application creates a dialog box.
If then works out if the customer is Hebrew or English. If Hebrew, I want to dynamically change the CEdit.
BRONZE EXPERT
Commented:
well, you can try to implement a function which at runtime destroys and re-creates
a control given by its ID with or without WS_EX_RIGHT (I think that should be
possible, but I don't think it's really easy). I.e.:

void
ModifyEditExStyle( CDialog* pDlg, UINT idRes, DWORD iStyleRemove, DWORD iStyleAdd )
{
     CWnd* pControl = pDlg->GetDlgItem( idRes );

     if ( NULL == pControl )
     {
          ASSERT( 0 );
          return;
     }

     CRect rect;
     pControl->GetWindowRect( rect );
     pDlg->ScreenToClient( rect );
     DWORD dwStyle = pControl->GetStyle();
     DWORD dwExStyle = pControl->GetExStyle();

     dwExStyle &= ~iStyleRemove;
     dwExStyle |= iStyleAdd;

     CWnd* pParent = pControl->GetParent();
     CString szText;

     pControl->GetWindowText( szText );
     pControl->DestroyWindow();
     pControl->Detach();
     pControl = new CEdit;
     pControl->CreateEx( dwExStyle, "EDIT", NULL, dwStyle, rect, pParent, idRes );
     pControl->SetWindowText( szText );
     pControl->SetFont( pDlg->GetFont() );
}

and then call this function for each edit control like this:
// i.e. in a member function of the dialog
ModifyEditExStyle( this, IDC_EDIT1, 0, WS_EX_RIGHT );


hope that helps,

ZOPPO

Author

Commented:
thanks zoppo.
I'll give that a try

cel
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered by: Zoppo

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Roshan Davis
EE Cleanup Volunteer

Explore More ContentExplore courses, solutions, and other research materials related to this topic.