Click here to Skip to main content
15,892,517 members
Articles / Desktop Programming / WTL

Fight the dialog units, DPI and Large Fonts

Rate me:
Please Sign up or sign in to vote.
4.95/5 (25 votes)
16 Nov 2003Zlib2 min read 247K   5.1K   44  
Guarantees pixel-to-pixel matching appearance of resource-based dialogs for different font DPIs
/****************************************************

   Set dialog DPI helper class

   Use this class to make your dialog-editor based 
   dialogs look the same under any Windows DPI 
   settings (Large font mode).
   
   This is important if your dialogs have bitmaps on 
   them. You can specify current DPI if you want 
   bitmaps to get resized to current DPI (normally
   they don't get resized).

   Copyright (C)2003 by George Yohng 
   http://www.yohng.com

   LICENSE AGREEMENT:

      You agree to go to http://www.yohng.com/music.html
      and listen completely (without skips) to the very
      first track on the list.

 ****************************************************/

/*  

 *************************************************
 * Warning: for proper sizing with large fonts,  *
 *          set dialog font explicitly to        *
 *                                               *
 *         Microsoft Sans Serif (as opposed to   *
 *                                MS Sans Serif) *
 *          or Tahoma                            *
 *                                               *
 *          (they have the same sizes)           *
 *************************************************

   // -------------------------------------------------------------------
   // MFC Example:
   // -------------------------------------------------------------------

   ...

   BOOL CMyDlg::OnInitDialog()
   {
       CDialog::OnInitDialog();

       dpi.Attach(AfxFindResourceHandle(IMAKEINTRESOURCE(IDD), RT_DIALOG),
                  m_hWnd,IDD,96.0); // 96 is the DPI

       // The rest of your initialization code goes here

       return TRUE;
   }

   // -------------------------------------------------------------------
   // ATL/WTL Example:
   // -------------------------------------------------------------------

   ...

   BOOL CMyDlg::OnInitDialog()
   {
       CDialog::OnInitDialog();

       dpi.Attach(_AtlBaseModule.GetResourceInstance(),m_hWnd,IDD,96.0);
       //                                                         ^^^^ DPI

       // The rest of your initialization code goes here

       return TRUE;
   }

*/

#ifndef SETDPI_INCLUDED
#define SETDPI_INCLUDED

class CSetDPI
{
public:
    int IDD;
    HINSTANCE inst;
    HWND hwnd;
    HFONT font,oldfont;

    CSetDPI();

    void Attach(HINSTANCE inst,HWND dlg,int IDD,double dpi);

    void Detach();

    ~CSetDPI();
};

#endif

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The zlib/libpng License


Written By
Web Developer
Austria Austria
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions