banner



How To Change Api Settings

Introduction

Previously, we take talked about how to change screen resolution and color system via DirectX. Today, we are talking near how to change all display settings -not the resolution and color organization only- via API. We will change screen resolution (premises,) color organization (bit count,) rotation (orientation,) and refresh rate (frequency) via API with C# and the .NET Framework.

Overview

This lesson firstly discusses the required functions and structures. And then, it focuses on how to retrieve current brandish settings. Afterward that, it discusses how to get all modes supported by your display. As y'all already know, a fashion is a combination of may display settings including bounds, scrap count, orientation, and frequency; therefore, we will refer to display settings as display manner.

Finally, this lesson discusses how to alter the current display settings. Along with the discussion, you volition learn additional techniques similar how to PInvoke Win32 API functions, and to marshal unmanaged data types.

In addition, this lesson comes with a sample application used for irresolute the display settings.

Now, nosotros are going to discuss the required functions and construction and how to use them. After that, we will focus on the implementation code. Get ready.

EnumDisplaySettings() Function

This function resides in user32.dll. It is used to retrieve ane of the modes supported by a graphics device.

The definition of this function is as following:

  1. BOOL EnumDisplaySettings(
  2. LPCTSTR lpszDeviceName,
  3. DWORD iModeNum,
  4. [In, Out] LPDEVMODE lpDevMode
  5. );

This function accepts only three parameters:

  • lpszDeviceName:
    Specifies the brandish device name that volition be used to retrieve its modes. This parameter can be either NULL to signal the default device, or the name of the display device. You can enumerate display devices using the EnumDisplayDevices() function.
  • iModeNum:
    Specifies the type of information to retrieve. It could be either a style index or ane of these values:
    • ENUM_CURRENT_SETTINGS = -1
      Retrieves current display mode.

    • ENUM_REGISTRY_SETTINGS = -2
      Retrieves current display mode stored on the registry.

  • lpDevMode:
    A reference (In/Out) parameter represents the DEVMODE object encapsulates the retrieved display way information. The DEVMODE'due south dmSize member is used for input to represents the structure size, while other members are used for output.

Equally y'all might expect, to retrieve the current mode (settings) of the current display device, you will need to pass a Goose egg value as the lpszDeviceName parameter to point the current brandish, and the value -one to the iModeNum parameter to indicate the current mode.

Unfortunately, EnumDisplaySettings() can return only i style per telephone call, and that mode is encapsulated into the DEVMODE object. To recall all modes (or few) supported by a display device, you need to call EnumDisplaySettings() many times specifying iModeNum equally the mode index. Mode indexes kickoff from zero. Therefore, to retrieve all modes, you volition need to call the EnumDisplaySettings() function many times specifying 0 for iModeNum on the first time, and increment that alphabetize every telephone call until EnumDisplaySettings() returns Imitation, which indicates that the previous mode was the last mode supported.

If you desire to remember a mode (or all modes) supported by other brandish device, you lot will need to modify the lpszDeviceName to the name of that device. You can get a list of all devices connected using the EnumDisplayDevices() office.

Now, it is the time for the PInvoke method. We can PInvoke this function in C# as post-obit:

  1. [DllImport( "User32.dll" )]
  2. [return : MarshalAs(UnmanagedType.Bool)]
  3. public static extern  Boolean EnumDisplaySettings(
  4. [param: MarshalAs(UnmanagedType.LPTStr)]
  5. string  lpszDeviceName,
  6. [param: MarshalAs(UnmanagedType.U4)]
  7. int  iModeNum,
  8. [In, Out]
  9. ref  DEVMODE lpDevMode);

What is Platform Invocation (PInvoke)? You lot already know, there is no such compatibility between managed code (.Cyberspace) and unmanaged code (Win32 API in our instance.) Therefore, they cannot call each other direct. Rather, yous make use of the PInvoke service to telephone call unmanaged functions from the managed environment.

What is Marshaling? Marshaling is another service of the CLR. Again, there is no such compatibility betwixt managed code and unmanaged code. Therefore, they cannot communicate directly. To send data between the managed client and unmanaged server, and vice versa, you will need to use marshaling to allow sending and receiving of the data. Marshaling converts managed information types to unmanaged information and vice versa.

Equally you suggested, at present we are going to talk about the DEVMODE structure.

DEVMODE Construction

This structure encapsulates data about a printer or a display device.

This structure is fairly a complex construction, but we will try to break information technology downwards to be uncomplicated and easier to align.

The definition of this structure is as following:

  1. typedef struct  DEVMODE {
  2. BCHAR dmDeviceName[CCHDEVICENAME];
  3. WORD dmSpecVersion;
  4. WORD dmDriverVersion;
  5. Discussion dmSize;
  6. WORD dmDriverExtra;
  7. DWORD dmFields;
  8. matrimony {
  9. struct  {
  10. short  dmOrientation;
  11. brusk  dmPaperSize;
  12. brusk  dmPaperLength;
  13. curt  dmPaperWidth;
  14. short  dmScale;
  15. short  dmCopies;
  16. brusque  dmDefaultSource;
  17. short  dmPrintQuality;
  18. };
  19. POINTL dmPosition;
  20. DWORD dmDisplayOrientation;
  21. DWORD dmDisplayFixedOutput;
  22. };
  23. short  dmColor;
  24. short  dmDuplex;
  25. curt  dmYResolution;
  26. brusque  dmTTOption;
  27. brusk  dmCollate;
  28. BYTE dmFormName[CCHFORMNAME];
  29. Discussion dmLogPixels;
  30. DWORD dmBitsPerPel;
  31. DWORD dmPelsWidth;
  32. DWORD dmPelsHeight;
  33. wedlock {
  34. DWORD dmDisplayFlags;
  35. DWORD dmNup;
  36. }
  37. DWORD dmDisplayFrequency;
  38. #if(WINVER >= 0x0400)
  39. DWORD dmICMMethod;
  40. DWORD dmICMIntent;
  41. DWORD dmMediaType;
  42. DWORD dmDitherType;
  43. DWORD dmReserved1;
  44. DWORD dmReserved2;
  45. #if (WINVER >= 0x0500) || (_WIN32_WINNT >= 0x0400)
  46. DWORD dmPanningWidth;
  47. DWORD dmPanningHeight;

0 Response to "How To Change Api Settings"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel