Changing Screen Resolution Programmatically via DirectX

هذه المقالة متوفرة أيضا باللغة العربية، اقرأها هنا.

Overview

This lesson focuses on how to change the screen resolution and color system programmatically via DirectX. It starts by an overview about how the Windows satisfies user’s need through the Display Settings window. Then, it digs into discussing how to retrieve these settings and to change these programmatically in the .NET environment.

Introduction

It is common to change the screen resolution when working with some applications. In addition, games automatically change the screen resolution (bounds) and color system (bit count) to accommodate performance issues.

Background

In Windows, you can change display settings from Display Settings window where you can change screen resolution and color system. Figure 1 shows the Display Settings window.

Display Settings
Figure 1. Display Settings Dialog

However, your screen might support more than these settings. For instance, it could support 8 bit color system which you cannot see in the colors list.

To list all modes that are supported by your screen, you can click Advanced Settings then List All Modes button to display all modes supported and change to the desired mode. Figure 2 shows the List All Modes dialog.

Display List All Modes
Figure 2. Listing All Display Modes Supported

What is a mode? A mode is a combination of four settings, resolution (width and height,) orientation (rotation,) bit count (color system,) and frequency (refresh rate.)

Accessing the DirectX Library

DirectX is the technology of choice when working with multimedia of any type. Here in this lesson, we will focus on how to use DirectX to retrieve screen settings and to change them in the .NET environment.

DirectX consists of many libraries of which every library is specialized in some processing. The library we are interested in is dx3j.dll (Direct 1.0 Type Library) which resides in the System32 folder. You can reference this library in your project by adding it from the Add Reference dialog from the COM tab. Because it is a COM component, you end up creating an interop assembly (DIRECTLIB.dll) for accessing the library. Figure 3 shows the Add Reference dialog.

Figure 3. Adding Direct 1.0 Type Library to References
Figure 3. Adding Direct 1.0 Type Library to References

Because there is no such compatibility between .NET and unmanaged code, you cannot call COM components directly. Instead, you may create a RCW (Runtime Callable Wrapper) assembly that acts as a proxy to the COM component. RCWs also called Interop Assemblies are created automatically when you try to reference a COM component in Visual Studio .NET. However, if you want to have more control over the creation process, you can create your RCW via the tool tlbimp.exe. This tool allows you to control the RCW at a granular level. For instance it allows you to sign the RCW, change its name and version, and to control the marshalling process of the unmanaged types. It is worth mentioning that ActiveX COM components are created with the tool aximp.exe not tlbimp.exe.

Retrieving Current Display Settings

After referencing the required DirectX library, you can now dig into the details of programmatically retrieving and changing display settings.

You can access display settings from the _dxj_DirectDrawClass that resides in our RCW assembly.

The getDisplayMode() function is used to retrieve current display mode information; it accepts a single output parameter that is of type DDSurfaceDesc which encapsulates the retrieved data.

Like other DirectX structures, DDSurfaceDesc is fairly big. However, here, we are interested in four members:

  • width and height:
    Represent the screen bounds (resolution.)
  • rgbBitCount:
    Represents the bit count (color system) of the screen.
  • refreshRate:
    Represents the screen refresh rate (monitor flicker.)

Now, it is the time for the code retrieves current display mode information:

// C# Code
static void Main()
{
    DIRECTLib._dxj_DirectDrawClass ddraw =
        new DIRECTLib._dxj_DirectDrawClass();
    DIRECTLib.DDSurfaceDesc desc;
    ddraw.getDisplayMode(out desc);
    Console.WriteLine("{0} by {1}, {2} bit, {3} Hertz",
        desc.width, desc.height,
        desc.rgbBitCount, desc.refreshRate);
}
' VB.NET Code
Sub Main()
    Dim ddraw As _
        New DIRECTLib._dxj_DirectDrawClass()
    Dim desc As DIRECTLib.DDSurfaceDesc
    ddraw.getDisplayMode(desc)
    Console.WriteLine("{0} by {1}, {2} bit, {3} Hertz", _
        desc.width, desc.height, _
        desc.rgbBitCount, desc.refreshRate)
End Sub

Changing Current Display Settings

Changing the current display settings is very easy. All you need is to provide the new settings to the setDisplayMode() function.

The setDisplayMode() function takes five parameters. However, we are interested in the first three parameters:

  • w:
    The screen width.
  • h:
    The screen height:
  • bpp:
    The bit count (color system.)

The following code sets the display bounds to 640 by 480, and sets the bit count to only 8. I think that feeling reminds you of the ancients Windows ME and its ascendants specially before installing the video driver.

    // C# Code
static void Main()
{
    DIRECTLib._dxj_DirectDrawClass ddraw =
        new DIRECTLib._dxj_DirectDrawClass();
    DIRECTLib.DDSurfaceDesc desc =
        new DIRECTLib.DDSurfaceDesc();
    ModeCallback callback = new ModeCallback();
    const uint DDEDM_REFRESHRATES = 3;
    string format = "{0} by {1}, {2} bit, {3} Hertz";
    ddraw.enumDisplayModes
        (DDEDM_REFRESHRATES, ref desc, format, callback);
}
class ModeCallback : DIRECTLib.IEnumModesCallback
{
    public void callbackEnumModes
        (ref DIRECTLib.DDSurfaceDesc surfDesc, object ctxt)
    {
        Console.WriteLine(ctxt.ToString(),
            surfDesc.width, surfDesc.height,
            surfDesc.rgbBitCount, surfDesc.refreshRate);
    }
}
    ' VB.NET Code
Sub Main()
    Dim ddraw As New DIRECTLib._dxj_DirectDrawClass()
    Dim desc As New DIRECTLib.DDSurfaceDesc()
    Dim callback As New ModeCallback()
    Const DDEDM_REFRESHRATES As UInt32 = 3
    Dim format As String = "{0} by {1}, {2} bit, {3} Hertz"
        ddraw.enumDisplayModes _
        (DDEDM_REFRESHRATES, desc, format, callback)
End Sub
Class ModeCallback
    Implements DIRECTLib.IEnumModesCallback
    Public Sub callbackEnumModes _
        (ByRef surfDesc As DIRECTLib.DDSurfaceDesc, _
        ByVal ctxt As Object) _
        Implements DIRECTLib.IEnumModesCallback.callbackEnumModes
        Console.WriteLine(ctxt.ToString(), _
        surfDesc.width, surfDesc.height, _
        surfDesc.rgbBitCount, surfDesc.refreshRate)
    End Sub
End Class

Notice that closing your application rolls everything back to its original state.
It is worth mentioning that, trying to change the display settings to a mode that is not supported by the display throws NotImplementedException. Despite this, you can enumerate all display modes by the enumDisplayModes() function.

The Last Word

You can use this technique to change the display settings at the beginning of the application and let the runtime returns it back for you when closing the application. For instance, you could add the code to the Main() function and everything will be returned back after the last line of Main() completes.

Similar Posts:

Random Posts:

Recent Posts: