Click here to Skip to main content
Click here to Skip to main content

Fight the dialog units, DPI and Large Fonts

, 16 Nov 2003 Zlib
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

George Yohng
Web Developer
Austria Austria
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 Pinmemberomindream6-Feb-13 22:05 
QuestionWhy don't effect on multiline edit and checkbox? Pinmembernhchmg21-Jul-11 23:10 
GeneralHelp for use in Visual C Pinmembersymons6527-Jun-10 6:10 
QuestionHow can use it in SDI or MDI type application? Pinmember"_$h@nky_"26-May-09 23:53 
Generalscaling factor for dialog resource [modified] Pinmemberathish12-Apr-07 2:23 
ur code snippet for DIALOGEX resource is useful
 
can u suggest me incase of DIALOG resource??
 
Frown | :(
-- modified at 3:07 Tuesday 3rd April, 2007
QuestionPropertySheet Tab title size is still too big PinmemberLuckymmkay1-Dec-06 6:02 
AnswerRe: PropertySheet Tab title size is still too big PinmemberGeorge Yohng1-Dec-06 6:37 
GeneralRe: PropertySheet Tab title size is still too big PinmemberLuckymmkay1-Dec-06 8:40 
Questionan issue when using it in my dialogbar Pinmemberaisonbert4-Apr-06 16:49 
AnswerRe: an issue when using it in my dialogbar Pinmemberaisonbert6-Apr-06 16:01 
Generalthe issue using MSLU in windows 98/me Pinmemberssliao21-Feb-06 20:50 
GeneralRe: the issue using MSLU in windows 98/me Pinmemberssliao23-Feb-06 22:06 
GeneralOh no! PinmemberAlexander Gräf26-Sep-05 0:46 
GeneralRe: Oh no! PinmemberGeorge Yohng26-Sep-05 0:55 
GeneralRe: Oh no! PinmemberNic Wilson3-Apr-06 18:17 
GeneralRe: Oh no! PinmemberCodeSafe Hawk12-Mar-09 4:22 
GeneralExcellent job Pinmemberiberg4-Jul-05 0:09 
GeneralMFC and IMAKEINTRESOURCE PinmemberGeorge Yohng10-May-05 2:47 
GeneralRe: MFC and IMAKEINTRESOURCE Pinmemberbensabat10-May-05 4:27 
GeneralRe: MFC and IMAKEINTRESOURCE PinmemberGeorge Yohng15-May-05 3:41 
GeneralMFC example PinsussAnonymous1-Feb-05 0:53 
GeneralRe: MFC example PinmemberGeorge Yohng12-Feb-05 5:21 
GeneralRe: MFC example Pinmemberbensabat10-May-05 2:28 
GeneralRe: MFC example PinmemberGeorge Yohng15-May-05 3:49 
GeneralUse it in Windows98 Pinmemberhansworscht16-Jun-04 4:23 
GeneralRe: Use it in Windows98 Pinmemberpma10-Sep-04 11:15 
GeneralProblem with ActiveX controls.. Pinmembervallikumar30-Apr-04 0:57 
Generalit's working only for main Dialog box in VC++6.0.. Pinsussdingudikka@yahoo.com1-Apr-04 20:48 
GeneralRe: it's working only for main Dialog box in VC++6.0.. PinmemberGeorge Yohng1-Apr-04 23:13 
GeneralRe: it's working only for main Dialog box in VC++6.0.. Pinmembervallikumar2-Apr-04 2:03 
GeneralRe: it's working only for main Dialog box in VC++6.0.. PinmemberGeorge Yohng5-Apr-04 11:20 
GeneralRe: it's working only for main Dialog box in VC++6.0.. Pinmembervallikumar7-Apr-04 19:09 
GeneralCould not compile in VC++ PinmemberKmAshif24-Mar-04 18:32 
GeneralI could not compile in vc++(vc6) PinmemberkmAshif24-Mar-04 18:30 
GeneralRe: I could not compile in vc++(vc6) PinmemberGeorge Yohng25-Mar-04 1:41 
GeneralDialog - Fullscreen Pinmemberpma1-Jan-04 23:48 
GeneralRe: Dialog - Fullscreen PinmemberGeorge Yohng2-Jan-04 1:04 
GeneralRe: Dialog - Fullscreen Pinmemberpma3-Jan-04 11:35 
GeneralWhy don't have effect in windows98 Pinmemberchendana7-Dec-03 18:58 
GeneralRe: Why don't have effect in windows98 PinmemberGeorge Yohng7-Dec-03 19:18 
GeneralRe: Why don't have effect in windows98 Pinmemberpma21-Dec-03 11:30 
GeneralRe: Why don't have effect in windows98 Pinmemberhansworscht15-Jun-04 3:14 
QuestionAny way to use your technique in Borland C++ 4/5 ? PinsussHeinrichJF27-Nov-03 0:30 
AnswerRe: Any way to use your technique in Borland C++ 4/5 ? PinmemberGeorge Yohng27-Nov-03 21:55 
GeneralDisplay Resolution PinmemberJerry III25-Nov-03 20:37 
GeneralRe: Display Resolution PinmemberGeorge Yohng27-Nov-03 22:05 
GeneralRe: Display Resolution PinmemberNic Wilson10-May-04 13:57 
GeneralRe: Display Resolution PinmemberRyan Beesley12-Aug-05 7:14 
GeneralFont metrics not identical PinsussAnonymous17-Nov-03 4:19 
GeneralRe: Font metrics not identical PinmemberAndrew Allen17-Nov-03 11:02 

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.

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