• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 356
  • Last Modified:

Alignment & Font Size

Hi,

As you'll gues I'm new to MFC.  

Anyway I am trying to change alignment of edit boxes in code, and would also like to know how to change edit box font size.

For the alignment I thought the following would do it (but no luck) :-

CWnd *ptr = GetDlgItem(IDC_TIME);
CDC *dc = ptr->GetDC();

dc->SetTextAlign(TA_RIGHT);
ptr->ReleaseDC(dc);

where IDC_TIME is an edit box.

Also I've tried several ways to change the font size of certain edit boxes but all that seems to happen is that the edit box reverts to a default font (MS Sans Serif point 10 I think).

I would greatly appreciate any help/code examples.  

Lee

P.S. The reason I'm trying to change the alignment in code is that if I use the properties option to set edit box aligment all works fine in NT4, but in Win95 all my edit boxes revert to Left Justification.
0
LeeOgley
Asked:
LeeOgley
1 Solution
 
Vinayak KumbarSr Program ManagerCommented:
Hi,

I told u to change the alignment not like that.
But u can do it by using

GetDlgItem(IDC_TIME)->ModifyStyle(ES_LEFT, ES_CENTER, 0);

like that....

Try it out.
VinExpert
0
 
ZoppoCommented:
Hi LeeOgley,

you cannot change the behavior of drawing of controls just by getting a device context from it and change its attributes, because each window initializes its device context itself before it's drawn.

To change the alignment just modify the style, i.e.

CWnd* pWnd = GetDlgItem( IDC_TIME );
pWnd->ModifyStyle( 0, ES_RIGHT );

to change the font do something like following:
CWnd* pWnd = GetDlgItem( IDC_TIME );
CFont font;
font.CreatePointFont( 100, "Sans Serif" );
pWnd->SetFont( &font );
font.Detach();

hope that helps,

ZOPPO
0
 
LeeOgleyAuthor Commented:
Thanks
0
Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

 
ZoppoCommented:
:(

VinExpert told nothing about setting a font...
0
 
LeeOgleyAuthor Commented:
Zoppo,

Sorry, new to system, I thought I could accept both answers, happened to select VinExpert first.  I appreciate your answer...sorry won't happen again.

Lee.
0
 
LeeOgleyAuthor Commented:
VinExpert, Zoppo

I've tried both your examples, and even under Nt, the alignment is not changed..

Overview:
 I have an edit box (IDC_TIME), with the following properties checked :-

Visible,TabStop,AutoHScroll,Border, LEft Justification.

I tried both sets of code within OnInitDialog and also a TIMER which goes off every second...  The edit box ALWAYS remained left justified.

Help please...

Lee.
0
 
ZoppoCommented:
Sorry, my failure. Use

pWnd->ModifyStyle( ES_LEFT|ES_CENTER, ES_RIGHT );

hope that's it,

ZOPPO
0
 
LeeOgleyAuthor Commented:
Zoppo,

Thanks for the continued help, but still no joy, edit box still remains left justified under NT (not tired moving to 95, thought I'd better get it going in NT first.)

Lee.

P.S.  I dont know if this helps but the edit box is 'attached' to a CString variable, this is where I put the time, and then just call UpdateData
0
 
ZoppoCommented:
ok, seems we have a problem now. We both failed, VinExpert. I found following in MSDN:

-----------------------------------------------------------------------------------
ES_LEFT, ES_RIGHT, and ES_CENTER specify the alignment the text in an edit control should have. Single-line edit controls can only be left-aligned. ES_LEFT is the default for both single-line and multiple-line edit controls. Single-line edit controls ignore ES_RIGHT and ES_CENTER. Multiple-line edit controls can be right-aligned, left-aligned, or centered. Right-aligned and centered edit boxes cannot have horizontal scroll bars and cannot have the ES_AUTOHSCROLL style.

Although it's not possible to change the alignment style of an edit control dynamically, two techniques are commonly used to work around this restriction:

The first workaround is to create multiple edit controls, one for each style the application needs, and to switch between them as required.

The second technique is to create a new edit control with the desired style as it is needed and switch to the new control.

The Knowledge Base has article Q66942, which describes the details of these techniques. EDALIGN, a sample application, demonstrates the code required.
-----------------------------------------------------------------------------------

I've tried some time with this and found out that's not all correct, i.e. 'Single-line edit controls ignore ES_RIGHT and ES_CENTER' cannot be correct since you can create a single line right justified edit control through resource editor.

So, LeeOgley, the solution can be found in MSDN article Q66942 ...

ZOPPO
0
 
LeeOgleyAuthor Commented:
Zoppo,

I agree with you, if editor and set an edit  box to Centered, then it is (under NT), I only came across the problem when running the program under Win95.  Come to think of it I believe I've had similar problems with Visual Basic and single line edit controls.

I beleive ( with regards to VB anyway) that if I built the project on a WIn95 machine the problem was solved, might also be the case for VC, but currently not in a position to test that theory (only have access to NT machines at the moment)

Anyway many thanks for the continued time spent of helping me with this problem

Lee.
0
 
binarydreamCommented:
>>to change the font do something like following:
CWnd* pWnd = GetDlgItem( IDC_TIME );
CFont font;
font.CreatePointFont( 100, "Sans Serif" );
pWnd->SetFont( &font );
font.Detach();
>>

Hi,Zoppo, why you Datach the font?
Is it wrong or not? I think you'v select font into pWnd,so it's in using,ur?
0
 
ZoppoCommented:
If I don't detach the font's handle from the CFont object it'll be destroyed in CFont's destructor with ::DeleteObject. A cleaner way would be to make font as a member of the window's class, then this Detach() wouldn't be used. Further, if the font is not a member of the class, the font has to be destroyed when it's not used any more, i.e. in the window's OnDestroy().

ZOPPO
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now