Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How can I change the font size of a edit box?

Posted on 2003-12-10
5
Medium Priority
?
3,164 Views
Last Modified: 2013-12-03
Hi,
I'm trying to change the font size of the edit box I create in the child area of my main window. I use the following code to create this window:

////////////////////////////////////
int CChildView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CWnd ::OnCreate(lpCreateStruct) == -1)
      return -1;
      
// CEdit* m_pEdit -->declared in .h file
      m_pEdit = new CEdit();
      m_pEdit->Create(WS_CHILD|WS_VISIBLE|WS_BORDER,
            CRect(0, 0, 100, 100), this, ID_TERMSCREEN);

      CFont m_Font;
      m_Font.CreateFont(28,20,0,0,FW_BOLD,FALSE,FALSE,0,DEFAULT_CHARSET,
            OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS,DEFAULT_QUALITY, DEFAULT_PITCH,NULL);

      m_pEdit->SetFont(&m_Font, TRUE);

      UpdateData(FALSE);

      return 0;
}
///////////////////////////////////////////

Unfortunately the font size never changes. Only the cursor size increases, but when I write something in the edit box it uses the default font. What should I do more or where should I call SetFont ?
0
Comment
Question by:selinmetin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 9911973
have      CFont m_Font; declared in the header file so it is a member variable for the class
0
 

Author Comment

by:selinmetin
ID: 9913346
Well, thanks. I managed this. I don't need to make m_Font a member of the class. Instead, I should have made m_Font a pointer to the created font and use this pointer when calling SetFont. ie:

CFont* m_Font = new CFont();
m_Font.CreateFont(28,20,0,0,FW_BOLD,FALSE,FALSE,0,DEFAULT_CHARSET,
          OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS,DEFAULT_QUALITY, DEFAULT_PITCH,NULL);

     m_pEdit->SetFont(m_Font);

// no need to call UpdateData();

But now I have problem with the background color. I'm actually using Windows CE .net 4.1. In Win32 I can set the background automatically by the following in OnCtlColor function:

      if (nCtlColor == CTLCOLOR_STATIC) {
            pDC->SetBkColor(RGB(0, 0, 0));
            pDC->SetTextColor(RGB(0, 255, 0));
            return (HBRUSH)m_termBrush;
      }
// m_termBrush is a class member formerly created.

But the background is not painted instantly, instead with every letter processed the background of the character is painted. Is there something specific with Windows CE?

0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 9913472
You had originally set it as the pointer by using &m_Font so I don't see why it should make a difference (unless it is Win CE that is).

0
 
LVL 11

Accepted Solution

by:
KurtVon earned 60 total points
ID: 9914197
The difference is that when it wasn't a pointer, the m_Font was freed when the function exited and the font was deleted by teh system.  Now that it is a pointer the destructor is never called, so the font is still there.  Unfortunately, it is never freed and therefore uses up GDI resources and leaks memory.

The original suggestion of putting the font in teh header was the correct solution.  You need to keep the font alive while it is needed by the edit control, but just as importantly, the font must be freed when it is no longer needed or you will leak memory and GDI resources.

Also, SetBkColor sets the background color for text, not for the control.  To change the color of the control you need to return an HBRUSH with the color you wants elected into it.

Hope this helps.
0
 
LVL 11

Expert Comment

by:KurtVon
ID: 9914219
And if this is a multiline edit control OnCtlColor won't color the additional lines until text is put in them.  You need to subclass the control and override the OnEraseBkgnd function.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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…
Suggested Courses

664 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