Using the NonClientMetrics Class

Using the simple properties in the NonClientMetrics class is just like using properties in any other class. First, you create a new instance of the object, and then you set and retrieve its properties. For example, to add five pixels to the width of the scrollbars, you could use code like this:

Dim ncm As New NonClientMetrics
ncm.ScrollWidth = ncm.ScrollWidth + 5

To test out most of the simple properties, the code in Listing 9.10 exercises most of the simple NonClientMetrics properties. Figure 9.14 shows a simple window before the code is called, and Figure 9.15 shows the same window after the code has done its work. As you can see, the NonClientMetrics class provides properties to control most of the visual aspects of the window border.

Figure 9.14: A simple window before FixNCM is called

Figure 9.15: The same window after FixNCM is called

Listing 9.10: Modify Most of the Simple NonClientMetrics Properties

Sub FixNCM()
    Dim ncm As New NonClientMetrics
    ncm.BorderWidth = ncm.BorderWidth * 2
    ncm.CaptionHeight = ncm.CaptionHeight * 2
    ncm.CaptionWidth = ncm.CaptionWidth * 2
    ncm.MenuHeight = ncm.MenuHeight * 2
    ncm.MenuWidth = ncm.MenuWidth * 2
    ncm.ScrollHeight = ncm.ScrollHeight * 2
    ncm.ScrollWidth = ncm.ScrollWidth * 2
End Sub

Setting all these properties is a slow process. If you intend to change multiple NonClientMetrics properties often, consider removing the calls to SaveSettings in each property’s code and explicitly calling the SaveSettings method yourself once you’ve made all the changes. That way, Windows won’t attempt to change all the settings individually, and your code will run faster. To make these properties work like all simple properties, we wrote the code so that each Property Let procedure saves its changes. You’re welcome to change this behavior if you like.

Using the NonClientMetrics Font Properties

In addition to its simple properties, the NonClientMetrics class provides five font properties. Each of these properties is a reference to a separate object, a Font object (FONT.CLS). Each Font object provides a set of properties describing a font used by a NonClientMetrics object, such as the font of the caption bar or the font used by the menus. In addition, the NonClientMetrics object maintains the font used by the form popped up by the MsgBox function. That’s right: from your own applications, you can control the font used by the standard MsgBox function.

To use the font properties, treat them as objects contained within the NonClientMetrics object. For example, to retrieve the name of the font used by the MsgBox form, you could write code like this:

Dim ncm As New NonClientMetrics
Debug.Print ncm.MessageFont.FaceName

The real difference between using the simple properties and using the Font object properties is that changes to the fonts aren’t saved until you explicitly call the SaveSettings method of the NonClientMetrics object. That is, once you’ve made all the font changes you need to make, you must use code like this:

Call ncm.SaveSettings

to cause the object to save all the new settings. You needn’t call this method at all if you’re only retrieving values or if you’re working only with the simple properties. If you want to modify font values, however, your changes won’t take effect until you call this method.

If you want to work extensively with the Font object, you could use the With statement, treating it like the real object it is. For example, the code in Listing 9.11 retrieves the current MsgBox font settings, modifies them, pops up a test message box, and then resets the values.

Listing 9.11: Sample Procedure That Modifies the MessageFont Object

Sub FixMsgBox()
    Dim ncm As New NonClientMetrics
    Dim strOldFont As String
    Dim intOldSize As Integer
    With ncm
        With .MessageFont
            strOldFont = .FaceName
            intOldSize = .Size
            .FaceName = "Tahoma"
            .Size = 12
        End With
        Call .SaveSettings
        MsgBox "This is a test"
        With .MessageFont
            .FaceName = strOldFont
            .Size = intOldSize
        End With
        Call .SaveSettings
        MsgBox "This is a test"
    End With
End Sub

As another example, the code in the following fragment sets the standard menu font to be two point sizes larger than it was originally and causes the font to be italic:

Dim ncm As New NonClientMetrics
With ncm.MenuFont
    .Size = .Size + 2
    .Italic = True
End With
Call ncm.SaveSettings

Some programs, including all of Microsoft’s Office 97 suite, eschew standard menus, using, instead, menus of their own creation. Changes you make to standard Windows menus will have no effect in these applications.

© 1997 by SYBEX Inc. All rights reserved.