Use the System Font
Site Map Feedback
Up Controls Files Moving Data
The proper way to draw text using the same font as the dialog box is:
  NONCLIENTMETRICS ncm;
  ncm.cbSize=sizeof(NONCLIENTMETRICS);
  SystemParametersInfo(SPI_GETNONCLIENTMETRICS,0,&ncm,0);

This provides a LOGFONT structure at ncm.lfStatusFont which you can modify (say by adding underline: ncm.lfStatusFont.lfUnderline=TRUE;) Then just use CreateFontIndirect(&ncm.lfStatusFont); to have the font ready for selecting into the device context. If you don't need to alter the font in any ways your code will be simpler using one of the following methods...

If you are drawing text on a control, the font to use is the dialog's font, which can be accessed like this: CGdiObject* OldFont=dc.SelectObject(GetParent()->GetFont()); then use dc.DrawText(...);and finish with dc.SelectObject(OldFont);

If you're just wanting to use the font unchanged use: CGdiObject* OldFont=dc.SelectStockObject(ANSI_VAR_FONT); then use: dc.DrawText(...); and finish with: dc.SelectObject(&OldFont); It is best to use a class to encapsulate the clean-up:

class CUseDialogFont {
  CGdiObject* OldFont;
  CDC* pDC;
public:
  CUseDialogFont(CDC* _pDC) {pDC=_pDC; OldFont=pDC->SelectStockObject(ANSI_VAR_FONT);}
 ~CUseDialogFont()          {try{pDC->SelectObject(&OldFont);}catch(...){}}
};

Then in your code just use: CUseDialogFont Font(&dc); That way you can 'return' at any time safe in the knowledge that the original font will be selected.


THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


 (1454) Last modified: Tue, 03 Feb 2009 18:29:31 +0000