Click here to Skip to main content
Licence Zlib
First Posted 16 Nov 2003
Views 130,534
Bookmarked 29 times

Fight the dialog units, DPI and Large Fonts

By | 16 Nov 2003 | Article
Guarantees pixel-to-pixel matching appearance of resource-based dialogs for different font DPIs

Introduction

Resource-based dialogs, made with a dialog editor do have a potential problem of being scaled improperly for larger Windows font resolutions, no matter whether MFC, ATL, WTL or bare Win32 is used.

The given class allows to pin down and fix a particular DPI mode for a particular dialog resource. Furthermore, DPI resolution/scaling of a resource-based dialog can be dynamically changed in run-time.

Background

For example, static bitmaps on the dialogs normally do not get resized, thus if one would want to make some fancy bitmap background or matched illustration, one will obviously run into problems with "Large Fonts" Windows mode. This is especially useful for wizard-style and login dialogs.

Some of the users tend to have "Large size (120 DPI)" font mode set, which is problematic for the developers, as triple checks are to be made to find out whether dialogs designed look properly in 120 DPI mode.

If the program interface is mostly bitmap based, the best way is to lock the resolution down to 96 DPI and disallow any further dialog scaling. Well, unfortunately, Windows does not seem to have an easy way of turning off DPI-dependent dialog scaling and "dialog units". I've been looking through the network and so far found no easy solutions for the given problem.

Therefore, I have written a class. Once Attach method is invoked in the WM_INITDIALOG handler before the dialog is shown, the dialog will be resized and adjusted in run-time to match the specified resolution.

The supplied code contains a subroutine to re-parse of the dialog resource and to re-calculate DPI-related values such as control positions and sizes. Resolution in DPI is specified as the parameter to the Attach method, and the standard Windows resolution is 96 DPI.

Using the code

The code is tested and functional within MFC, ATL/WTL and Win32 frameworks.

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;
   }

Caveats

I have looked for an easier way, but so far I have found none. The parser will only work with DIALOGEX structures and will not work with obsolete DIALOG structures.

Also you have to explicitly specify the dialog font. For proper sizing, you need to use Microsoft Sans Serif or Tahoma (and NOT MS Sans Serif or MS Shell Dlg). Tahoma has exactly the same metrics as Microsoft Sans Serif. You can use any other TrueType/OpenType font, avoid using bitmap fonts, as they will not scale well.

Due to obvious reasons, the size of a checkbox square is not affected, though it still will get proper placement and align.

License

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

About the Author

George Yohng

Web Developer

Austria Austria

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionWhy don't effect on multiline edit and checkbox? Pinmembernhchmg23:10 21 Jul '11  
GeneralHelp for use in Visual C Pinmembersymons656:10 27 Jun '10  
QuestionHow can use it in SDI or MDI type application? Pinmember"_$h@nky_"23:53 26 May '09  
Generalscaling factor for dialog resource [modified] Pinmemberathish12:23 2 Apr '07  
QuestionPropertySheet Tab title size is still too big PinmemberLuckymmkay6:02 1 Dec '06  
AnswerRe: PropertySheet Tab title size is still too big PinmemberGeorge Yohng6:37 1 Dec '06  
GeneralRe: PropertySheet Tab title size is still too big PinmemberLuckymmkay8:40 1 Dec '06  
Questionan issue when using it in my dialogbar Pinmemberaisonbert16:49 4 Apr '06  
AnswerRe: an issue when using it in my dialogbar Pinmemberaisonbert16:01 6 Apr '06  
Generalthe issue using MSLU in windows 98/me Pinmemberssliao20:50 21 Feb '06  
after I added the MSLU in my project, compiling with unicode option, then running under windows98, in an button click handle, I call the mmioOpen function, but it return NULL always. when I compile my project whithout unicode option, then it run good.
 
Who can tell me why? Thanks!
 
Believe myself, I can do better!
GeneralRe: the issue using MSLU in windows 98/me Pinmemberssliao22:06 23 Feb '06  
GeneralOh no! PinmemberAlexander Gräf0:46 26 Sep '05  
GeneralRe: Oh no! PinmemberGeorge Yohng0:55 26 Sep '05  
GeneralRe: Oh no! PinmemberNic Wilson18:17 3 Apr '06  
GeneralRe: Oh no! PinmemberCodeSafe Hawk4:22 12 Mar '09  
GeneralExcellent job Pinmemberiberg0:09 4 Jul '05  
GeneralMFC and IMAKEINTRESOURCE PinmemberGeorge Yohng2:47 10 May '05  
GeneralRe: MFC and IMAKEINTRESOURCE Pinmemberbensabat4:27 10 May '05  
GeneralRe: MFC and IMAKEINTRESOURCE PinmemberGeorge Yohng3:41 15 May '05  
GeneralMFC example PinsussAnonymous0:53 1 Feb '05  
GeneralRe: MFC example PinmemberGeorge Yohng5:21 12 Feb '05  
GeneralRe: MFC example Pinmemberbensabat2:28 10 May '05  
GeneralRe: MFC example PinmemberGeorge Yohng3:49 15 May '05  
GeneralUse it in Windows98 Pinmemberhansworscht4:23 16 Jun '04  
GeneralRe: Use it in Windows98 Pinmemberpma11:15 10 Sep '04  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120529.1 | Last Updated 17 Nov 2003
Article Copyright 2003 by George Yohng
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid