I am writing a MFC application and would like to change the font size of some of the static text. So I use the following.

CFont m_font;
	m_font.CreatePointFont(120,"Times New Roman");
	m_Test.SetFont(&m_font);
	m_Test.SetWindowText(strTest);

Now my question is, how to I get the layout editor in Microsoft Visual C++ to look the same so I know how big to make my static text box? (Other than trial and error)

Dani AI

Generated

As noted, the mismatch comes from the fact that the resource editor renders controls using the dialog template font while the code is creating a different font at runtime. As noted, many runtime-only changes do not appear in the Visual C++ dialog editor, so a small change in workflow is the most reliable fix.

The simplest reliable approach is to set the dialog template to use the same face and point size that the app will use at runtime. The dialog editor draws controls with that template font, so sizing and placement become WYSIWYG. Changing the dialog font affects dialog units (DLUs), so layouts will shift if the template font differs from the original — plan for that when reflowing controls.

When per-control fonts or runtime-only changes are required, measure and convert sizes in code so placement matches what was designed. Two useful snippets (new; not duplicated from earlier posts) are:

CRect rDLU(0,0,120,30); // width/height in DLUs
MapDialogRect(&rDLU);   // inside dialog: converts to pixels
myStatic.MoveWindow(rDLU);
CClientDC dc(this);
CFont* pOld = dc.SelectObject(&myFont);
CSize sz = dc.GetTextExtent(_T("Sample text"));
dc.SelectObject(pOld);
// use sz.cx / sz.cy to size or center the static control

Recommended workflow: pick the final font first, set the dialog template font in the resource editor, lay out controls, and then ensure the runtime font creation matches the template. If a custom static control (such as the one suggested by ) is needed for runtime features, accept that the resource editor may not preview that control and use the measurement/conversion techniques above or a quick runtime preview dialog to verify final sizing.

Recommended Answers

All 3 Replies

Check out MFC control:)

Now is there any way to get that to show up while editing the layout in visual C++?

No, there are many things you can't see at design time. You might google for another resource editor which might have what you want, but I doubt that you will find one.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.