This is a mirror of official site: http://jasper-net.blogspot.com/

How to create a display switcher for Windows XP

| Friday, April 8, 2011
Introduction

Windows 7 comes with a built-in display switcher for multiple displays which can be activated using the (Windows + P) shortcut key. This article shows how to implement a similar utility to switch between multiple displays in older Operating Systems such as Windows XP and below.

Background

I had to frequently switch between multiple displays when I using Windows XP. Every time I wanted to switch display, I had to go to Display Properties and fiddle with the UI to extend/shift the display to the second display. I found this to be extremely annoying and decided to do it the API way. So I wrote a program to switch displays with two clicks. You can use the source code to create your own utility.

Using the code

This code is written in C# using the Win32 Managed API. I have divided the article into two easy parts:

Part 1: The native structures and function declarations needed for the program to work.
Part 2: How to use the APIs to switch the display.

Part 1

These variables hold the device information for the active and the inactive displays. The enumeration is required to tell the API to get display-device information from the Registry.

//Member variables
DEVMODE ddActive = new DEVMODE();
DEVMODE ddInactive = new DEVMODE();
string szActiveDeviceName = string.Empty;
string szInactiveDeviceName = string.Empty;
const int ENUM_REGISTRY_SETTINGS = -2;        

Next are the DllImports and these are the stars of our show. EnumDisplayDevices gets all the devices connected to your system. EnumDisplaySettings gets the detailed information of the selected device obtained from EnumDisplayDevices. This information can then be used to pass to ChangeDisplaySettingsEx to manipulate the display devices.

[DllImport("user32.dll")]
static extern bool EnumDisplayDevices(string lpDevice, 
       uint iDevNum, ref DISPLAY_DEVICE lpDisplayDevice, uint dwFlags);

[DllImport("user32.dll")]
public static extern bool EnumDisplaySettings(string deviceName, 
       int modeNum, ref DEVMODE devMode);

[DllImport("user32.dll")]
static extern DISP_CHANGE ChangeDisplaySettingsEx(string lpszDeviceName, 
       ref DEVMODE lpDevMode, IntPtr hwnd, uint dwflags, IntPtr lParam);

The last part of declarations are the enums which are used by the API and we need to construct them for the APIs to work properly.

[StructLayout(LayoutKind.Explicit, CharSet = CharSet.Ansi)]
public struct DEVMODE
{
    public const int CCHDEVICENAME = 32;
    public const int CCHFORMNAME = 32;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHDEVICENAME)]
    [System.Runtime.InteropServices.FieldOffset(0)]
    public string dmDeviceName;
    [System.Runtime.InteropServices.FieldOffset(32)]
    public Int16 dmSpecVersion;
    [System.Runtime.InteropServices.FieldOffset(34)]
    public Int16 dmDriverVersion;
    [System.Runtime.InteropServices.FieldOffset(36)]
    public Int16 dmSize;
    [System.Runtime.InteropServices.FieldOffset(38)]
    public Int16 dmDriverExtra;
    [System.Runtime.InteropServices.FieldOffset(40)]
    public DM dmFields;
    [System.Runtime.InteropServices.FieldOffset(44)]
    Int16 dmOrientation;
    [System.Runtime.InteropServices.FieldOffset(46)]
    Int16 dmPaperSize;
    [System.Runtime.InteropServices.FieldOffset(48)]
    Int16 dmPaperLength;
    [System.Runtime.InteropServices.FieldOffset(50)]
    Int16 dmPaperWidth;
    [System.Runtime.InteropServices.FieldOffset(52)]
    Int16 dmScale;
    [System.Runtime.InteropServices.FieldOffset(54)]
    Int16 dmCopies;
    [System.Runtime.InteropServices.FieldOffset(56)]
    Int16 dmDefaultSource;
    [System.Runtime.InteropServices.FieldOffset(58)]
    Int16 dmPrintQuality;

Read more: Codeproject

Posted via email from Jasper-net

0 comments: