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:
- BOOL EnumDisplaySettings(
- LPCTSTR lpszDeviceName,
- DWORD iModeNum,
- [In, Out] LPDEVMODE lpDevMode
- );
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:
- [DllImport( "User32.dll" )]
- [return : MarshalAs(UnmanagedType.Bool)]
- public static extern Boolean EnumDisplaySettings(
- [param: MarshalAs(UnmanagedType.LPTStr)]
- string lpszDeviceName,
- [param: MarshalAs(UnmanagedType.U4)]
- int iModeNum,
- [In, Out]
- 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:
- typedef struct DEVMODE {
- BCHAR dmDeviceName[CCHDEVICENAME];
- WORD dmSpecVersion;
- WORD dmDriverVersion;
- Discussion dmSize;
- WORD dmDriverExtra;
- DWORD dmFields;
- matrimony {
- struct {
- short dmOrientation;
- brusk dmPaperSize;
- brusk dmPaperLength;
- curt dmPaperWidth;
- short dmScale;
- short dmCopies;
- brusque dmDefaultSource;
- short dmPrintQuality;
- };
- POINTL dmPosition;
- DWORD dmDisplayOrientation;
- DWORD dmDisplayFixedOutput;
- };
- short dmColor;
- short dmDuplex;
- curt dmYResolution;
- brusque dmTTOption;
- brusk dmCollate;
- BYTE dmFormName[CCHFORMNAME];
- Discussion dmLogPixels;
- DWORD dmBitsPerPel;
- DWORD dmPelsWidth;
- DWORD dmPelsHeight;
- wedlock {
- DWORD dmDisplayFlags;
- DWORD dmNup;
- }
- DWORD dmDisplayFrequency;
- #if(WINVER >= 0x0400)
- DWORD dmICMMethod;
- DWORD dmICMIntent;
- DWORD dmMediaType;
- DWORD dmDitherType;
- DWORD dmReserved1;
- DWORD dmReserved2;
- #if (WINVER >= 0x0500) || (_WIN32_WINNT >= 0x0400)
- DWORD dmPanningWidth;
- DWORD dmPanningHeight;
0 Response to "How To Change Api Settings"
Post a Comment