0

I'm using the MFC framework at work.

I have a CEdit control that I display status in.

How do I change the font size so that the text fits?
(IOW, I want the font size as large so that the text fits.)

Bonus points: How to center the text?

I'm not an expert with the MFC framework.

Here are some sample texts that will be displayed:

PASSED
FAILED
FAILED - SELF TEST
SELF TEST PASSED

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • 1
    If you make "FAILED - SELF TEST" the same width as "PASSED", by reducing font size, the font will be too small and not readable. I think your question is not worded correctly. To center, go to dialog editor, set "Align" option to center, or call `edit.ModifyStyle(0, ES_CENTER)` on `CEdit`. Or use `CStatic` to display read-only information. – Barmak Shemirani May 02 '19 at 02:00
  • 2
    Size encodes information. While a developer may want to efficiently use screen real estate by using an optimally-sized font, a UI designer will certainly object. The different font sizes not only make the UI look inconsistent, they are also perceived to convey additional information. With the information being *"text width per available screen area"*, this is not a metric the user is interested in. Sometimes the best code is code you do not write. – IInspectable May 02 '19 at 18:29

1 Answers1

0

First off, an edit-box is a UI item that allows the user to type text, but the sample strings you posted rather look like a status or result. Will the user have to type sth like "FAILED - SELF TEST"? I think you rather need a read-only control, like a static control (usually sunken, etched or static edge style), as @Barmak Shemirani suggested.

In any case, I would recommend the following:

  • First, consider enlarging the control instead, if there is space. Normally, all controls on a dialog use the same font (and this is by default). Having some controls using a different font may look weird (albeit possibly acceptable).
  • Second (if you wouldn't enlarge the control), experiment with the font size, before attempting to programmatically set the font size: set the text to FAILED - SELF TEST (longest), and "manually" set the font size to the largest possible (so that the text fits) and see if you get an acceptable result.

If so, you can programmatically set the font size. MFC is to some degree a "thin wrapper" of WinAPI, and all WinAPI items are accesible as public members of the MFC classes (eg m_hWnd is the handle of the WinAPI window of any CWnd-derived class), so you rather work at WinAPI level here. You should check/set the font size during initialization (WM_INITDIALOG message, mapped to the OnInitDialog() handler). These are the steps:

  • Check if the (default) font is OK: use the DrawText() function with the DT_CALCRECT flag set. This won't actually draw any text, instead it will return the size (rectangle) needed. Add some margin too (pls experiment, but it should be at least 4 pixels - two vertical lines for the frame and 1 pixel at each side). If this is less than or eaual to the control's size (alternatively you can test with the text width with no extra margin vs the control's client area, not total size), this means that the default font is OK, and no other action needed.
  • If not, get the control's font, and create a new font with the same parameters except for the size, which should be reduced by 1 (beware of the lfHeight parameter or member, if it's negative it should be instead "increased"). Check if with this font is OK now, and if not repeat this step until the text fits.
  • I wouldn't recommend trying to increase the font size, as this would require testing against the control's height as well, but if you are willing to...

Hope this helps, although I don't know if it's worth the effort.

Constantine Georgiou
  • 2,412
  • 1
  • 13
  • 17