Microsoft KB Archive/191126

= PRB: Font Changes as ActiveX Control Resizes =

Article ID: 191126

Article Last Modified on 11/21/2006

-

APPLIES TO

 Microsoft Foundation Class Library 4.2, when used with:  Microsoft Visual C++ 5.0 Enterprise Edition

 Microsoft Visual C++ 5.0 Professional Edition

 Microsoft Visual C++ 6.0 Enterprise Edition

 Microsoft Visual C++ 6.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Standard Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q191126

<div class="symptoms_section">

SYMPTOMS
When you call SelectStockFont to select a Stock font property to display text in an ActiveX control, the font of the text changes as the size of the control changes with SetWindowPos or MoveWindow.

<div class="cause_section">

CAUSE
SelectStockFont selects a scaled font into a device context. This causes the font size to change as the control resizes. However, the natural extent is not changed (m_rcBounds is bigger but m_cyExtent is not). As a result, the stock font doesn't scale perfectly with respect to the size of the control.

<div class="resolution_section">

RESOLUTION
Here are two possible workarounds for this problem:

<ol> Use SetControlSize instead of SetWindowPos or MoveWindow to change the size of the control.</li>  Replace SelectStockFont with the following code: CFontHolder& fntHolder =InternalGetFont; HFONT internalhFont = fntHolder.GetFontHandle;

HFONT hOldFont = (HFONT)::SelectObject(pdc->m_hDC,internalhFont); pdc->TextOut(20, 20, "Hello", 5); ::SelectObject(pdc->m_hDC,hOldFont); </li></ol>

<div class="status_section">

STATUS
This behavior is by design.

<div class="moreinformation_section">

Steps to Reproduce Behavior
<ol> Generate an MFC ActiveX control with the Control Wizard.</li> Add a Stock font property to the control.</li>  Add a method called SizeControl to the control. Call SetWindowPos in this method to change the size of the control. For example: void CFontCtrl::SizeControl {        CRect r;         GetWindowRect(&r); SetWindowPos(NULL, 0, 0,           r.Width + 100, r.Height + 100, SWP_NOMOVE); }                   </li>  Display a text string in the control's OnDraw function with the Stock font property as shown here: void CFontCtrl::OnDraw(CDC* pdc, const CRect& rcBounds,        const CRect& rcInvalid) {        pdc->FillRect(rcBounds,            CBrush::FromHandle((HBRUSH)GetStockObject(WHITE_BRUSH))); pdc->Ellipse(rcBounds); CFont* def_font = SelectStockFont(pdc); pdc->TextOut(20, 20, "Hello", 5); pdc->SelectObject(def_font); }                   </li> Build the control project, and then insert the control to the ActiveX Control Test Container.</li> Invoke the SizeControl method in the ActiveX Control Test Container. The font of the text changes.</li></ol>

Additional query words: ocx

Keywords: kbprb KB191126

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.