Click here to Skip to main content
15,564,383 members
Articles / Desktop Programming / WTL
Article
Posted 16 Nov 2003

Stats

232K views
5K downloads
44 bookmarked

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


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

 
GeneralRe: Why don't have effect in windows98 Pin
pma21-Dec-03 12:30
pma21-Dec-03 12:30 
GeneralRe: Why don't have effect in windows98 Pin
hansworscht15-Jun-04 4:14
hansworscht15-Jun-04 4:14 
QuestionAny way to use your technique in Borland C++ 4/5 ? Pin
HeinrichJF27-Nov-03 1:30
HeinrichJF27-Nov-03 1:30 
AnswerRe: Any way to use your technique in Borland C++ 4/5 ? Pin
George Yohng27-Nov-03 22:55
George Yohng27-Nov-03 22:55 
GeneralDisplay Resolution Pin
Jerry III25-Nov-03 21:37
Jerry III25-Nov-03 21:37 
GeneralRe: Display Resolution Pin
George Yohng27-Nov-03 23:05
George Yohng27-Nov-03 23:05 
GeneralRe: Display Resolution Pin
Nic Wilson10-May-04 14:57
Nic Wilson10-May-04 14:57 
GeneralRe: Display Resolution Pin
Ryan Beesley12-Aug-05 8:14
Ryan Beesley12-Aug-05 8:14 
I happen to agree with the grandparent. As a former tester at Microsoft for Office, I feel that I have some ground to stand here with my position.

Yes, dialog layout is hard. We actually invented our own layout system in Office to address changing text size, not only with DPI related issues, but also for adjusting the text for different translations. Imagine trying to get 33 different languages to look right in a dialog while simultaneously supporting different DPI settings and font sizes.

As a developer myself, I can equally understand the frustration that other developers feel. My advice is, build all your dialogs to support 120 DPI/Large Fonts at a minimum. Things will scale down to fit generally better than they scale up.

The worst thing you can do is force the user to size a dialog so that it is fixed at a 96 DPI while the rest of their system is 120 DPI. Large fonts is a setting for the visually impared, and by following this, you are creating a program that breaks accessability. Similarly, high DPI is a setting released with Windows Me that further improves readability for high resolution/high density devices; specifically some laptops where their LCD screens have a surface that far exceeds the density of most CRTs.

I believe Avalon, or rather Windows Presentation Foundation[^] will help make this easier, especially as with XAML you can easily define flow panels to reflow text and other UI elements on the fly. In the mean time, users that select a high DPI setting expect some of their apps to act in odd ways. It's better they act odd and that the user can read them than they act sanely and are unreadable... although I think I could safely argue that an app that doesn't follow the system settings for DPI seems pretty bizzare to the end user too.
GeneralFont metrics not identical Pin
Anonymous17-Nov-03 5:19
Anonymous17-Nov-03 5:19 
GeneralRe: Font metrics not identical Pin
Andrew Allen17-Nov-03 12:02
Andrew Allen17-Nov-03 12:02 
GeneralRe: Font metrics not identical Pin
George Yohng17-Nov-03 12:20
George Yohng17-Nov-03 12:20 

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

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