How can I change the font of a CEdit control 
Author Message
 How can I change the font of a CEdit control

Hello all:

I want to change the font of the CEdit control, but the following code
doesn't work for me, could anyone tell me what's wrong?

Besides, how can I change the color of the text color?

Thanks in advance
Kevin

==>>
void CGreyEdit::OnSetFocus(CWnd* pOldWnd)
{

 // TODO: Add your message handler code here
 TRACE("OnSetFocus\n");
 LOGFONT lf;
 CFont fontItalic;

 memset(&lf, 0, sizeof(LOGFONT));
 lf.lfHeight = -1;
 lf.lfWeight = FW_NORMAL;
 strcpy(lf.lfFaceName, "MS Sans Serif"); //Font face doesn't take effect!
 lf.lfItalic = TRUE;          //The text in the control is still normal, not
italic!
 VERIFY(fontItalic.CreateFontIndirect(&lf));
 this->SetWindowText(this->m_strDefaultText);
 this->SetFont(&fontItalic, TRUE);

 CEdit::OnSetFocus(pOldWnd);
 Invalidate(FALSE);

Quote:
}

<<==


Fri, 17 Dec 2004 11:08:22 GMT  
 How can I change the font of a CEdit control
I find two problem in your code,

1. CFont fontItalic declare in OnSetFocus(), the fontItalic variable will
out of scope when you exist the OnSetFocus function. You must declare a
member variable.

2.lf.lfHeight =-1, why?  this is a pixel of font height.

If you want set the CEdit control 's font, you can do in OnInitDialog()

e.g

 declare CFont fontItalic in your .h file

//////////////////////////////////////////////
  LOGFONT lf;
  memset(&lf, 0, sizeof(LOGFONT));
  lf.lfHeight = 20;
  lf.lfWeight = FW_NORMAL;
  strcpy(lf.lfFaceName, "Symbol");
  lf.lfItalic = TRUE;

  //fontItalic is a member variable;
  VERIFY(fontItalic.CreateFontIndirect(&lf));

  //your edit box control id
  GetDlgItem(IDC_EDIT1)->SetFont(&fontItalic, TRUE);

Jimmy Leung (Hong Kong)


Quote:
> Hello all:

> I want to change the font of the CEdit control, but the following code
> doesn't work for me, could anyone tell me what's wrong?

> Besides, how can I change the color of the text color?

> Thanks in advance
> Kevin

> ==>>
> void CGreyEdit::OnSetFocus(CWnd* pOldWnd)
> {

>  // TODO: Add your message handler code here
>  TRACE("OnSetFocus\n");
>  LOGFONT lf;
>  CFont fontItalic;

>  memset(&lf, 0, sizeof(LOGFONT));
>  lf.lfHeight = -1;
>  lf.lfWeight = FW_NORMAL;
>  strcpy(lf.lfFaceName, "MS Sans Serif"); //Font face doesn't take effect!
>  lf.lfItalic = TRUE;          //The text in the control is still normal,
not
> italic!
>  VERIFY(fontItalic.CreateFontIndirect(&lf));
>  this->SetWindowText(this->m_strDefaultText);
>  this->SetFont(&fontItalic, TRUE);

>  CEdit::OnSetFocus(pOldWnd);
>  Invalidate(FALSE);
> }
> <<==



Sun, 12 Dec 2004 05:16:40 GMT  
 How can I change the font of a CEdit control
I find two problem in your code,

1. CFont fontItalic declare in OnSetFocus(), the fontItalic variable will
out of scope when you exist the OnSetFocus function. You must declare a
member variable.

2.lf.lfHeight =-1, why?  this is a pixel of font height.

If you want set the CEdit control 's font, you can do in OnInitDialog()

e.g

 declare CFont fontItalic in your .h file

//////////////////////////////////////////////
  LOGFONT lf;
  memset(&lf, 0, sizeof(LOGFONT));
  lf.lfHeight = 20;
  lf.lfWeight = FW_NORMAL;
  strcpy(lf.lfFaceName, "Symbol");
  lf.lfItalic = TRUE;

  //fontItalic is a member variable;
  VERIFY(fontItalic.CreateFontIndirect(&lf));

  //your edit box control id
  GetDlgItem(IDC_EDIT1)->SetFont(&fontItalic, TRUE);

Jimmy Leung (Hong Kong)


Quote:
> Hello all:

> I want to change the font of the CEdit control, but the following code
> doesn't work for me, could anyone tell me what's wrong?

> Besides, how can I change the color of the text color?

> Thanks in advance
> Kevin

> ==>>
> void CGreyEdit::OnSetFocus(CWnd* pOldWnd)
> {

>  // TODO: Add your message handler code here
>  TRACE("OnSetFocus\n");
>  LOGFONT lf;
>  CFont fontItalic;

>  memset(&lf, 0, sizeof(LOGFONT));
>  lf.lfHeight = -1;
>  lf.lfWeight = FW_NORMAL;
>  strcpy(lf.lfFaceName, "MS Sans Serif"); //Font face doesn't take effect!
>  lf.lfItalic = TRUE;          //The text in the control is still normal,
not
> italic!
>  VERIFY(fontItalic.CreateFontIndirect(&lf));
>  this->SetWindowText(this->m_strDefaultText);
>  this->SetFont(&fontItalic, TRUE);

>  CEdit::OnSetFocus(pOldWnd);
>  Invalidate(FALSE);
> }
> <<==



Sun, 12 Dec 2004 05:19:57 GMT  
 How can I change the font of a CEdit control
Why are you doing this in OnSetFocus? That's a well and truly weird place to do it. In any
case, you are doing a SetFont of a local variable, which is being destroyed immediately,
so by the time the drawing code in the OnPaint handler gets around to trying to draw, the
font is long gone and therefore irrelevant. Also, -1 is not an interesting height for a
font, since it creates a 1-pixel-high font, which is pretty hard to read.

Create variable for the font in the CGreyEdit control itself. Initialize it only once,
when the control is created (in MFC, this is best done in the PreSubclassWindow handler).
If you need to change it, then change it and set it someplace, but the OnFocus handler is
irrelevant here.

It seems odd that you are not setting the text until the user clicks in the window; this
is an odd sort of interface which would not be obvious to most users. If you have thought
carefully about this, fine, but in general it is a bad idea to do something like this.
Just set the text whenever you have it. This otherwise results in startling behavior and a
cognitive disconnect with expected GUI behavior.

Also, ::ZeroMemory is preferrable to memset because it is a bit more obvious.
                                joe

Quote:

>Hello all:

>I want to change the font of the CEdit control, but the following code
>doesn't work for me, could anyone tell me what's wrong?

>Besides, how can I change the color of the text color?

>Thanks in advance
>Kevin

>==>>
>void CGreyEdit::OnSetFocus(CWnd* pOldWnd)
>{

> // TODO: Add your message handler code here
> TRACE("OnSetFocus\n");
> LOGFONT lf;
> CFont fontItalic;

> memset(&lf, 0, sizeof(LOGFONT));
> lf.lfHeight = -1;
> lf.lfWeight = FW_NORMAL;
> strcpy(lf.lfFaceName, "MS Sans Serif"); //Font face doesn't take effect!
> lf.lfItalic = TRUE;          //The text in the control is still normal, not
>italic!
> VERIFY(fontItalic.CreateFontIndirect(&lf));
> this->SetWindowText(this->m_strDefaultText);
> this->SetFont(&fontItalic, TRUE);

> CEdit::OnSetFocus(pOldWnd);
> Invalidate(FALSE);
>}
><<==

Joseph M. Newcomer [MVP]

Web: http://www3.pgh.net/~newcomer
MVP Tips: http://www3.pgh.net/~newcomer/mvp_tips.htm


Sat, 18 Dec 2004 06:34:54 GMT  
 How can I change the font of a CEdit control
Thanks both of you!

I now declare a CFont object as a class member, initialize it in the
PreSubClass event, and set the font height to an appropriated value, it all
work fine now.

I am doing this in OnSetFocus event simply because I am trying the
feasibility of changing the font of a CEdit control. Surely I won't do this
in my real application.

Now I also know how to change the backgroup/foreground color of the CEdit
control, it should be in the OnCtlColor event, right?

Thanks again!
Kevin



Sat, 18 Dec 2004 07:38:52 GMT  
 How can I change the font of a CEdit control
You don't need to change the font in the SetFocus event.

I tend to use the =WM_CTLCOLOR event (reflected OnCtlColor) in the subclass which I create
for the edit control, rather than putting the color information in the parent. I believe
strongly that it belongs in the subclass.
                                joe

Quote:

>Thanks both of you!

>I now declare a CFont object as a class member, initialize it in the
>PreSubClass event, and set the font height to an appropriated value, it all
>work fine now.

>I am doing this in OnSetFocus event simply because I am trying the
>feasibility of changing the font of a CEdit control. Surely I won't do this
>in my real application.

>Now I also know how to change the backgroup/foreground color of the CEdit
>control, it should be in the OnCtlColor event, right?

>Thanks again!
>Kevin

Joseph M. Newcomer [MVP]

Web: http://www3.pgh.net/~newcomer
MVP Tips: http://www3.pgh.net/~newcomer/mvp_tips.htm


Mon, 20 Dec 2004 21:09:06 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Changing Font in CEdit control

2. Changing the font of a CEdit control (non dialog)

3. How to change the font size for a CEdit control

4. changing font on a CEdit control

5. How to change font of run-time CEdit controls

6. Changing Font in a CEdit control

7. CEdit control .. change Font selection

8. CEdit controls and changing the font color

9. Changing the font of a multiline CEdit Control

10. Changing font style in CEdit derived control.

11. Changing Fonts for a CEdit control?

12. changing font size in CEdit control

 

 
Powered by phpBB® Forum Software