Solved

Alignment & Font Size

Posted on 2000-02-16
12
341 Views
Last Modified: 2013-12-03
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
Comment
Question by:LeeOgley
12 Comments
 
LVL 8

Accepted Solution

by:
VinExpert earned 20 total points
ID: 2526443
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
 
LVL 30

Expert Comment

by:Zoppo
ID: 2526448
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
 

Author Comment

by:LeeOgley
ID: 2526787
Thanks
0
 
LVL 30

Expert Comment

by:Zoppo
ID: 2526838
:(

VinExpert told nothing about setting a font...
0
 

Author Comment

by:LeeOgley
ID: 2526854
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
 

Author Comment

by:LeeOgley
ID: 2526940
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 30

Expert Comment

by:Zoppo
ID: 2526966
Sorry, my failure. Use

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

hope that's it,

ZOPPO
0
 

Author Comment

by:LeeOgley
ID: 2527051
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
 
LVL 30

Expert Comment

by:Zoppo
ID: 2527160
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
 

Author Comment

by:LeeOgley
ID: 2527232
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
 

Expert Comment

by:binarydream
ID: 2533757
>>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
 
LVL 30

Expert Comment

by:Zoppo
ID: 2534209
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

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Universal fonts 2 138
Process filename extension 3 156
Smart Camera scanning and reading information 3 86
fizzArray  challenge 1 47
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now