1. Trang chủ
  2. » Cao đẳng - Đại học

Writing DPI Aware Applications _ www.bit.ly/taiho123

35 1,5K 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 35
Dung lượng 875,61 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

An application that is not aware but is running on a high DPI display setting can suffer from many visual artifacts, including incorrect scaling of UI elements, clipped text, and blurry

Trang 1

Summary: Explains how to make your Win32 applications DPI-aware and

why you should do so This paper shows how to use the high-DPI features in Windows XP and Windows Vista to make your UI more consistent, attractive, and readable It also explains how to identify and fix common DPI issues, and how to use the manifest to declare your application to be DPI Aware

Trang 2

The information contained in this document represents the current view of Microsoft Corporation on the issues discussed as

of the date of publication Because Microsoft must respond to changing market conditions, it should not be interpreted to

be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information presented after the date of publication

This White Paper is for informational purposes only MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR

STATUTORY, AS TO THE INFORMATION IN THIS DOCUMENT

Complying with all applicable copyright laws is the responsibility of the user Without limiting the rights under copyright, no part of this document may be reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any purpose, without the express written permission of Microsoft Corporation

Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights covering subject matter in this document Except as expressly provided in any written license agreement from Microsoft, the furnishing of this document does not give you any license to these patents, trademarks, copyrights, or other intellectual property

© 2008 Microsoft Corporation All rights reserved

Microsoft, MS-DOS, Windows, Windows NT, Windows Server, Windows Vista, Active Directory, ActiveSync, ActiveX,

Direct3D, DirectDraw, DirectInput, DirectMusic, DirectPlay, DirectShow, DirectSound, DirectX, Expression, FrontPage, HighMAT, Internet Explorer, JScript, Microsoft Press, MSN, Outlook, PowerPoint, SideShow, Silverlight, Visual Basic, Visual C++, Visual InterDev, Visual J++, Visual Studio, WebTV, Windows Media, Win32, Win32s, and Zune are either registered trademarks or trademarks of Microsoft Corporation in the U.S.A and/or other countries

The names of actual companies and products mentioned herein may be the trademarks of their respective owners

Trang 3

Introduction 1

High-DPI Features in Windows 1

Setting DPI by Using Control Panel 2

DPI Virtualization 2

DPI-Related APIs 2

Common High-DPI Issues 3

Clipped UI Elements or Text 3

Incorrect Font Size 3

Incorrect Layout 4

Blurred UI Elements 4

Pixelated Text 5

Drag and Drop and Other Input Issues 5

Partial Rendering of a Full-Screen Application 5

Incorrect Use of Effective Resolution 6

Assessing DPI Compatibility 6

High-DPI Issues Checklist 6

Testing DPI Compatibility 7

Step by Step Guide 7

Existing Win32 Applications 7

New Win32 Applications 8

Addressing High DPI Issues 8

Declaring DPI Awareness 8

Using an Application Manifest 9

SetProcessDPIAware Function 10

Getting System Information 11

GetDeviceCaps Function 11

GetSystemMetrics Function 12

SystemParametersInfo Function 12

Determining the DPI Scale Factor 12

Scaling Text 13

CreateFont Functions 13

GetTextExtent Functions 13

Selecting Fonts 14

Scaling Graphics 15

Multiple Resolution Support 15

Closest Fit 15

Icons 18

Scaling Layout 18

Layout and DPI Virtualization Enabled 20

Handling Minimum Effective Resolution 23

Conclusion 24

For More Information 24

Appendix A: Setting High DPI in Windows 25

Setting High DPI in Windows XP 25

Setting High DPI in Windows Vista 27

Appendix B: DPI Sample Code 30

Appendix C: Optimal DPI Configuration Examples 32

Trang 4

Introduction

This white paper is a practical guide to adding DPI awareness to existing or new Win32®

applications Writing a DPI-aware application is the key to making a user interface (UI) look

consistently good across a wide variety of high-DPI display settings An application that is not aware but is running on a high DPI display setting can suffer from many visual artifacts, including incorrect scaling of UI elements, clipped text, and blurry images By adding support in your

DPI-application for DPI-awareness, you guarantee that the presentation of your DPI-application’s UI is more predictable, making it more visually appealing to users

Since the introduction of Windows Vista®, users are more frequently following the recommended advice to change DPI settings as a way to enlarge the size of the text and UI elements on high DPI displays As more manufacturers ship greater numbers of high-resolution displays, the default 96 DPI setting can no longer be assumed by applications For a list of optimal DPI configuration

examples, see Appendix C To promote the best user experience, developers must ensure that their applications are DPI-aware

This paper explores DPI features and examines high-DPI issues in Windows® XP and

Windows Vista It also provides a set of guidelines for assessing DPI awareness and some solutions that can help you address DPI-awareness issues in your application This paper contains the

following sections:

 High-DPI Features in Windows

Provides an overview of high-DPI support in Windows XP and Windows Vista

 Common High-DPI Issues

Provides a list of the key visual artifacts and usability issues that impact applications at

high-DPI settings

 Assessing DPI Compatibility

Provides test matrix and test strategy recommendations for assessing DPI compatibility

 Addressing High DPI Issues

Provides coding recommendations for resolving issues in your application related to high

DPI

High-DPI Features in Windows

This section provides an overview of the high-DPI features supported in Windows® XP and

Windows Vista® The following table shows a list of high-DPI-related features that are supported

by each platform

Trang 5

Setting DPI by Using Control Panel

Windows XP and Windows Vista both support the ability to change high-DPI display settings For information about setting high DPI on Windows XP and Windows Vista, refer to Appendix A: Setting High DPI in Windows

When the DPI settings are changed, the system fonts and system UI elements change in size This

is the primary reason that applications need to consider the system DPI setting in their rendering and layout code Applications that are not DPI aware can potentially exhibit some visual artifacts such as mismatched font sizes, clipped text, or clipped UI elements

DPI Virtualization

Windows Vista introduces a feature called DPI virtualization which provides some level of automatic scaling support to applications which are not DPI aware Without this feature, the size of the text and UI elements of DPI unaware applications would typically be smaller on high DPI settings

compared to rest of the system, potentially causing usability and readability problems

This feature works by providing “virtualized” system metrics and UI elements to the application, as

if it were running at 96 DPI The application then renders to a 96-DPI off-screen surface and the Desktop Windows Manager then scales the resulting application window to match the DPI setting For example, if the DPI display setting is 144, DWM scales the application’s window by 150%, or 144/96 The type of scaling that DPI virtualization uses is based on pixel stretching As a result, blurring occurs due to the stretched pixels The following screenshot shows the type of visual artifact caused by stretched pixels due to DPI virtualization

The goal of the DPI virtualization feature is to increase the size of the text for non-DPI aware applications In some cases, however, virtualization infrastructure can cause significant

compatibility problems If your application has issues on Windows Vista that are caused by DPI virtualization, users can turn DPI virtualization off for your application without affecting other applications To disable DPI virtualization for a single application, right-click the name of the

application executable file, click Properties, click the Compatibility tab, and then select the box labeled Disable display scaling on high DPI settings

Note Users can disable DPI virtualization on a system-wide basis by selecting the Use

Windows XP style DPI scaling checkbox in the Windows Vista Custom DPI Setting

dialog box For more information, see Appendix A: Setting High DPI in Windows

For more information about DPI virtualization, see the High DPI Support in Windows Vista Aeroposting on Greg Schechter's Blog For more information on compatibility settings for

Windows Vista, see the Make older programs run in this version of Windows topic in Windows Help and How-to

DPI-Related APIs

The Win32® API provides functions for enabling DPI awareness in applications To learn how to declare your application to be DPI-aware, see Declaring DPI Awareness To learn how to retrieve system information and metrics related to DPI awareness, see Getting System Information

Trang 6

Common High-DPI Issues

Applications that do not check the system DPI setting and do not adjust for the larger font and UI sizes can raise various classes of issues This section describes the most common categories of issues and shows examples that illustrate them The categories of high DPI issues include:

 Clipped UI elements or text

 Incorrect font size

 Incorrect layout

 Blurred UI elements

 Pixelated text and bitmaps

 Misalignment of coordinate space affecting input

 Partial rendering of a full-screen application

 Incorrect use of virtual resolution

In the Assessing DPI Awareness section, you can find a summary of the high-DPI issues, the

potential root cause of each issue, and possible techniques for resolving each issue

Clipped UI Elements or Text

Applications that are not DPI-aware sometimes exhibit clipped UI elements or text In the following screen shot, notice that the text “Print this page” is clipped off at the bottom by the button

This is a result of text being resized while other UI elements containing the text, such as list box items and buttons, are not resized In this case the application has failed to scale the size of the UI layout in proportion to the larger text

Here are other scenarios that can result in clipped UI elements or text:

 UI elements no longer fit into the application window due to increased sizes of text and controls

 The application resizes partially in response to system metrics, but fails to resize completely For example, the application may resize buttons to fit the new text font, but fails to increase the size of the child window that contains the buttons

Incorrect Font Size

Applications that are not DPI-aware often display incorrect font sizes for text In the following screen shot, notice the inconsistent font sizes in the application’s UI

Trang 7

This type of artifact is typically caused when an application incorrectly creates and uses fonts There are many ways to create and use fonts in Windows applications Some font APIs enable your application’s text to resize dynamically in response to the change in DPI setting, while others do not To avoid the inconsistent display of font sizes in your application, ensure that you create and use fonts in a DPI-independent way For more information about creating and using fonts, see the Scaling Text section

Incorrect Layout

Applications that are not DPI-aware can sometimes exhibit incorrect layout, which can result in a number of visual artifacts, as well as problems interacting with the application In the following screen shot, notice that the UI layout does not scale, which results in clipped UI elements, some of which may appear off screen Also notice that some of the text word-wraps despite the fact that there is adequate screen real estate available

Incorrect layout is often caused by miscalculations of system metrics, such as screen size In some cases, system metrics are calculated correctly for certain UI elements, but not all UI elements Another common cause of incorrect layout is running at a low “effective resolution” The term

“effective resolution” refers to the resulting resolution which takes into account both the physical resolution and the DPI setting of the display It is very important that you use the correct effective screen resolution when calculating layout For more information about effective resolution, see Handling Minimum Effective Resolution

Blurred UI Elements

When an entire application appears blurred on Windows Vista®, it is typically the result of DPI virtualization In the following screen shot, the pixels have been stretched, which results in the blurred effect

Trang 8

To override the effect of DPI virtualization, enable your application to be DPI-aware You should follow the guidance in the Assessing DPI Awareness section to get an understanding of the amount

of work needed In addition, you should follow the testing guidelines to ensure that your

application behaves as expected at the most common DPI configurations

Pixelated Text

In certain instances, text appears pixelated but the application exhibits no other visual artifacts Pixelated text is most noticeable in the diagonal strokes of characters In the following screen shot, the text “Name” is pixelated, while the text “Advanced Search” is not

This type of visual artifact is most likely caused by your application using bitmapped fonts If you scale the text of a bitmapped font, the text is stretched rather than redrawn with a higher point-size font Stretched text results in pixelation To avoid pixelated text, use TrueType fonts in your DPI-aware application TrueType fonts are vector based, and scale appropriately as larger font sizes in response to changes in DPI display settings

For more information about fonts, see the MSDN Library topic, About Fonts For information about Win32® APIs for text and fonts, see the MSDN Library topic, Using the Font and Text Output Functions of GDI

Drag and Drop and Other Input Issues

One of the subtle side effects of DPI virtualization is that it can cause input issues For example, drag-and-drop operations may no longer work due to the misalignment of an application’s

coordinate space when it is remapped to the system’s coordinate space during scaling

The root of this problem is that when the Desktop Windows Manager scales the application to a larger size, the resulting transform changes the application’s coordinate space to be different from the rest of the system Translating drag-and-drop coordinates from one coordinate space to

another does not work because there is no way of knowing how the application is going to use the offset system coordinates

The DPI virtualization feature was introduced to ensure that users had a reasonably good

experience with applications which were not DPI-aware However, DPI virtualization is not able to solve all application issues, such as changes in coordinate space

Partial Rendering of a Full-Screen Application

Another side effect of DPI virtualization which sometimes occurs is the partial rendering of a screen application In most cases, this side effect is seen in full screen gaming applications,

full-because they commonly do programmatic screen resolution mode changes This issue affects only Windows Vista applications that do not declare themselves as DPI-aware The problem is that the

Trang 9

application may not be taking into account the fact that some of the system metrics are virtualized Since full screen applications like games are natively resolution-independent, in most cases this means they are also natively DPI independent The recommended solution for this class of

applications is to declare themselves DPI aware Note that some applications may have mode installers and uninstallers so it is important to do an application test pass according to the DPI testing guidelines to ensure that the whole application experience behaves properly at high DPI

windowed-Incorrect Use of Effective Resolution

Applications often require a minimum display resolution to run As an example, suppose an

application has a minimum required resolution of 1024x768 At run time, the application may

query the system to determine the current screen resolution If the application determines that the screen resolution is less than the required minimum, it prompts the user with a warning

Because high DPI settings cause the system to use larger fonts and larger UI elements, it also

means that applications take up more pixels when they draw The result is a lower effective

resolution because more pixels are required to render the same UI The formula to calculate the

effective resolution is: Effective Resolution = Real Resolution / (DPI/96)

For example, suppose the user has a display of 1200x900 with 144 DPI, the effective resolution is 800x600, because 144/96 = 150%—this reduces the effective screen real-estate by 50% This means that the size of the UI and text at this setting is roughly the same as if the user were

running at 96 DPI with an 800x600 screen resolution For more information about effective

resolution, see Handling Minimum Effective Resolution

Assessing DPI Compatibility

Assessing DPI compatibility requires the following three steps:

1 Familiarize yourself with the common high-DPI issues

2 Test your application at the recommended high-DPI display settings and resolutions

3 Address DPI issues by using the techniques described in the topic Addressing High DPI Issues

High-DPI Issues Checklist

The following table provides a list of the key DPI issues and their most common causes and

solutions

Clipped UI elements or

text UI elements are not resizing based on the GetTextExtent function Scaling text

Incorrect font size Fonts are being created with

Blurred UI elements Application is not declared

DPI-aware, and DPI virtualization is enabled

DPI virtualizationDeclaring DPI awareness

Trang 10

coordinate space

affecting input aware, and DPI virtualization is enabled Declaring DPI awareness

Partial rendering of a

full-screen application Application is not declared DPI-aware, and is using a mix of

virtualized and non-virtualized metrics

DPI virtualizationDeclaring DPI awarenessDetermine DPI scale factor

Incorrect use of virtual

resolution Application is not declared DPI-aware and is using virtualized

system metrics

DPI virtualization Declaring DPI awarenessDetermine DPI scale factor Handling minimum resolution

Testing DPI Compatibility

To assess your application’s DPI compatibility, you should test your application at a variety of resolutions with different high-DPI settings Also, it is recommended that you test on Vista as there are DPI-related features on Vista which are not present on XP

The following table provides a recommended set of DPI settings and minimum resolutions to

consider when testing

*Note Testing at a 192 DPI display setting is optional, but it enables you to determine

how “future proof” your application is For Windows Vista applications, we recommend that you resolve issues you find at least for configurations up to 144 DPI

When you log these issues into your bug tracking system, it is often useful to include the

following data points:

 Screen shot of the visual artifact

 DPI configuration, including whether DPI virtualization is enabled

 Screen resolution used to reproduce the issue

Having this information available to the developer helps identify the issues and to make the

corresponding fixes more easily

Step by Step Guide

The following sets of steps show how to fix DPI compatibility issues and declare your application to

be DPI aware

Existing Win32 Applications

To make your existing application DPI-aware, do the following:

Trang 11

1 Test your application at high DPI and write down all issues found

2 Search the source code for common DPI coding issues

3 Do an analysis on the cost of making the application fully DPI-aware

4 List all required high-DPI assets, such as toolbars, buttons, and icons

5 Fix issues found in step 1 by using the corresponding solution to the issue

6 Declare your application DPI-aware

7 Verify that all issues have been fixed If not, repeat the preceding steps

New Win32 Applications

To make your new application DPI-aware, do the following:

1 Declare your application DPI-aware

2 Familiarize yourself with the coding techniques

3 List all required high-DPI assets, such as toolbars, buttons, and icons

4 Write your application

5 Integrate the new assets from step 3

6 Test your application for DPI compatibility

7 Verify that all DPI issues have been resolved If not, repeat the preceding steps

Addressing High DPI Issues

There are several techniques you can use to resolve high DPI issues in your application These techniques include:

 Declaring DPI awareness

 Using system metrics information to calculate layout

 Determining the DPI scale factor

 Scaling text

 Scaling graphics

 Scaling layout

 Handling minimum effective resolution

Declaring DPI Awareness

When an application declares itself to be DPI-aware, it is a statement specifying that the

application behaves well at DPI settings up to 200 DPI In Windows® XP, DPI awareness has no impact on the application or the operating system It is only on Windows Vista® that DPI

awareness has meaning In Windows Vista, when DPI virtualization is enabled, applications that are not DPI-aware are scaled, and applications receive virtualized data from the system APIs, such as

the GetSystemMetric function

Note By default, the DPI virtualization feature is enabled only when the DPI display setting

is 144 or greater

While the Win32® API provides a function declaring an application as DPI-aware, its use is

discouraged, except in very specific circumstances For more information, see SetProcessDPIAware

Trang 12

Function In general, using an application manifest is the recommend process for declaring an application to be DPI-aware

Using an Application Manifest

To declare your application to be DPI-aware, add <dpiAware> to the application manifest

Here is an example of how to use the <dpiAware> element in an application manifest

<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"

Note If the <dpiAware> element appears in the assembly manifest of a DLL component,

the setting is ignored Only the assembly manifest for the application can enable DPI

awareness

You should save the manifest information in the preceding example to a file named

DeclareDPIAware.manifest and then use the MT.exe tool to bind it to your application

Alternatively, if you are using Visual Studio, you can add this to your project manifest file by going

to Project Properties > Configuration Properties > Manifest Tool > Input and Output, and then specifying the DeclareDPIAware.manifest file in the Additional Manifest Files text box, as shown in the following screenshot from Microsoft Visual Studio 2008:

Trang 13

For more information about the use of manifests, see the MSDN Library topic, Manifest Generation

in Visual Studio For more information about using assembly manifests, see the MSDN Library topic, Manifests [Side-by-Side Assemblies]

Note In some versions of Visual Studio, you might receive a manifest tool warning such

as: manifest authoring warning 81010002: Unrecognized Element "application" in namespace "urn:schemas-microsoft-com:asm.v3" This is due to a known bug in the

manifest compiler and you can safely ignore it

After you have added the manifest to your application, you can test your application by running it

at 144 DPI with DPI virtualization enabled If you have successfully declared your application as DPI-aware, you should not see the blurring of your application UI due to scaling via DPI

virtualization

SetProcessDPIAware Function

However, the use of the SetProcessDPIAware function is discouraged For example, if a DLL caches DPI settings during initialization, invoking SetProcessDPIAware in your application might

generate a possible race condition For this reason, we recommend that an application enable DPI

Trang 14

awareness by using the application’s assembly manifest rather than by calling

SetProcessDPIAware

By adding <dpiAware> element to your application’s assembly manifest, you mark your application

as being DPI aware The user32.dll module, which provides Windows user interface functionality, checks the application’s DPI awareness setting If an application is determined to be DPI aware, the user32.dll module calls SetProcessDPIAware on behalf of the application

Note A DLL component should respect the DPI-aware setting of an application and not call SetProcessDPIAware

The most common case for requiring the SetProcessDPIAware function is generic hosts that load

a DLL and execute from a specified entry point Three examples of generic hosts are the line utility program Rundll32, the DllHost process, and the Microsoft Management Console (MMC) When a DLL is loaded from a generic host, it is essentially the entry point of the application and any DLLs implicitly linked to by your DLL will be initialized before the application Therefore,

command-SetProcessDPIAware should always be called before any initialization to prevent any of those

DLLs from caching DPI-sensitive metrics

However, you should avoid generic hosts whenever possible when writing new code Instead, you should write a small executable containing the proper manifest entries Many control panel applets

in Windows Vista used this technique

Getting System Information

The following Win32 API functions are useful for retrieving information about the current display setting:

GetDeviceCaps Function

The GetDeviceCaps (http://msdn.microsoft.com/en-us/library/ms533266.aspx) function enables you to retrieve the number of pixels per logical inch along the screen width and height In a system with multiple display monitors, this value is the same for all monitors The following code example shows how to retrieve the horizontal and vertical DPI for the current display setting

// From CDPI::_Init()

HDC hdc = GetDC(NULL);

if (hdc)

{

_dpiX = GetDeviceCaps(hdc, LOGPIXELSX);

_dpiY = GetDeviceCaps(hdc, LOGPIXELSY);

ReleaseDC(NULL, hdc);

}

// Using CDPI example class

Trang 15

CDPI g_metrics;

int dpiX = g_metrics.GetDPIX();

int dpiY = g_metrics.GetDPIY();

GetSystemMetrics Function

The GetSystemMetrics (http://msdn.microsoft.com/en-us/library/ms724385.aspx) function enables you to retrieve the specified system metric or system configuration setting Note that all

dimensions retrieved by GetSystemMetrics are in pixels The following code example shows how

to retrieve the horizontal and vertical resolution for the current display setting

// Retrieve the horizontal and vertical resolution of the current display setting int cxScreen = GetSystemMetrics(SM_CXSCREEN);

int cyScreen = GetSystemMetrics(SM_CYSCREEN);

The CDPI example class listed in Appendix B (and shown here) offers methods to get the screen dimensions scaled based on DPI (known as relative pixels)

CDPI g_metrics;

int cxScreen = g_metrics.ScaledScreenWidth();

int cyScreen = g_metrics.ScaledScreenHeight();

SystemParametersInfo Function

The SystemParametersInfo (http://msdn.microsoft.com/en-us/library/ms724947.aspx) function enables you to retrieve or set the value of one of the system-wide parameters This function can also update the user profile while setting a parameter The following code example shows how to retrieve the number of lines to scroll when the vertical mouse wheel is moved

int g_ucScrollLines;

// Retrieves the number of lines to scroll when the vertical mouse wheel is moved SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, &g_ucScrollLines, 0);

Determining the DPI Scale Factor

If you define your application as a DPI-aware application, you will need to scale your application appropriately at high DPI settings To scale correctly, you must determine the relative DPI scale factor The DPI scale factor uses 96 DPI as the baseline setting for determining the value The following code example shows how to determine the DPI scale factor

int ScaleX(int x) { _Init(); return MulDiv(x, _dpiX, 96); }

int ScaleY(int y) { _Init(); return MulDiv(y, _dpiY, 96); }

Trang 16

// Example using CDPI class

int cxScaledWidth = g_metrics.ScaleX(100);

After you have determined the relative DPI scale factor, you should apply that scale factor when selecting fonts, loading images, and laying out UI elements in your application

 Custom fonts based on pixel size should apply the DPI scale factor

 Verify that UI layout and text will scale well at various high-DPI settings

Note If you are not sure whether your application uses a TrueType font (.ttf), look at the

font definition file in Control Panel Right-click the file, and check whether it is a ttf file

CreateFont Functions

To create a font use either the CreateFont

(http://msdn.microsoft.com/en-us/library/ms534214.aspx), CreateFontIndirect

(http://msdn.microsoft.com/en-us/library/ms534009.aspx), or CreatFontIndirectEx

(http://msdn.microsoft.com/en-us/library/ms534035.aspx) function You should apply the DPI scale factor when defining the characteristics of the font The following example shows how to apply the DPI scale factor to the

lfHeight member of the LOGFONT structure when using the CreateFontIndirect function

// From CDPI: convert a point size (1/72 of an inch) to raw pixels

int PointsToPixels(int pt) { return MulDiv(pt, _dpiY, 72); }

LOGFONT lf;

lf.lfHeight = -g_metrics.PointsToPixels(12);

// Fill in the rest of the structure

HFONT hfont = CreateFontIndirect(&lf);

GetTextExtent Functions

To determine the pixel dimensions of your scaled text string, use the GetTextExtent

(http://support.microsoft.com/kb/74298) family of functions, because the physical pixel size of the font is different depending on the DPI display setting The following code example shows how to determine the width and height of the specified text string by using the GetTextExtentPoint32

(http://msdn.microsoft.com/en-us/library/ms534223.aspx) function before drawing a rectangle around the string

Trang 17

static const TCHAR szString[] = TEXT("TEST");

// Retrieve width and height extents of specified string

SIZE size;

GetTextExtentPoint32(hdc, szString, lstrlen(szString), &size);

// Calculate scaled rect

RECT rcText;

SetRect(&rcText, 10, 10, 10 + size.cx, 10 + size.cy);

g_metrics.ScaleRect(&rcText);

// Calculate border based on text rect

RECT rcBorder = rcText;

InflateRect(&rcBorder, g_metrics.ScaleX(4), g_metrics.ScaleY(4));

// Draw rectangle (adjusted for DPI scaling factor) around string

Rectangle(hdc, rcBorder.left, rcBorder.top, rcBorder.right, rcBorder.bottom);

// Draw string inside rectangle

TextOut(hdc, rcText.left, rcText.top, szString, lstrlen(szString));

Selecting Fonts

attributes of a font If you use this function, ensure that you specify TrueType fonts as part of the

Flags member of the CHOOSEFONT structure The following example shows how to initialize the

ChooseFont function so that only TrueType fonts are enumerated in the Fonts dialog box

#include "commdlg.h"

CHOOSEFONT data = { sizeof(data) };

data.hwndOwner = hWnd;

// List only TrueType screen fonts in the Font dialog box

data.Flags = CF_TTONLY | CF_SCREENFONTS;

ChooseFont(&data);

The Windows Vista User Experience Guidelines

(http://go.microsoft.com/fwlink/?LinkId=128247&clcid=0x409) recommend that applications use standard visual styles where possible and that applications use a scalable TrueType font instead of

DC font

Ngày đăng: 30/10/2015, 17:53

TỪ KHÓA LIÊN QUAN

w