简体   繁体   中英

Determine Current Presentation Display Mode

Taken from this SO question I have the following code that switches my presentation mode to "extend":

var proc = new Process { StartInfo = { FileName = "DisplaySwitch.exe", Arguments = "/extend" } };
proc.Start();

But I only want to run that snippet if the presentation display mode isn't already set to extend. Is there anyway to programmatically determine the current presentation display mode of a machine?

Note: The solution only needs to work for Windows 8 machines.

I don't know if you are still interested in a solution. You have to make use of CCD (Connecting and Configuring Displays) and you can only do that by calling windows functions with dllimport. Here is the Link to CCD https://msdn.microsoft.com/en-us/library/windows/hardware/ff539367(v=vs.85).aspx

For this, you have to make 2 calls. First you have to get the buffer size and then the display config.

[DllImport("User32.dll")]
    public static extern StatusCode GetDisplayConfigBufferSizes(
        QueryDisplayConfigFlags flags, 
        out int numPathArrayElements,
        out int numModeInfoArrayElements);

[Flags]
public enum QueryDisplayConfigFlags : uint
{
    QDC_ZERO = 0x0,
    QDC_ALL_PATHS = 0x00000001,
    QDC_ONLY_ACTIVE_PATHS = 0x00000002,
    QDC_DATABASE_CURRENT = 0x00000004
}

public enum StatusCode : uint
{
    Success = 0,
    InvalidParameter = 87,
    NotSupported = 50,
    AccessDenied = 5,
    GenFailure = 31,
    BadConfiguration = 1610,
    InSufficientBuffer = 122,
}

int numPathArrayElements;
int numModeInfoArrayElements;

var status = CCDWrapper.GetDisplayConfigBufferSizes(
             pathType,
             out numPathArrayElements,
             out numModeInfoArrayElements);

[DllImport("User32.dll")]
    public static extern StatusCode QueryDisplayConfig(
        QueryDisplayConfigFlags flags,
        ref int numPathArrayElements,
        [Out] DISPLAYCONFIG_PATH_INFO[] pathInfoArray,
        ref int modeInfoArrayElements,
        [Out] DisplayConfigModeInfo[] modeInfoArray,
        out DISPLAYCONFIG_TOPOLOGY_ID_Flags topologyId
    );

[Flags]
public enum DISPLAYCONFIG_TOPOLOGY_ID_Flags: uint
{
    DISPLAYCONFIG_TOPOLOGY_ZERO = 0x0,
    DISPLAYCONFIG_TOPOLOGY_INTERNAL = 0x00000001,
    DISPLAYCONFIG_TOPOLOGY_CLONE = 0x00000002,
    DISPLAYCONFIG_TOPOLOGY_EXTEND = 0x00000004,
    DISPLAYCONFIG_TOPOLOGY_EXTERNAL = 0x00000008,
    DISPLAYCONFIG_TOPOLOGY_FORCE_UINT32 = 0xFFFFFFFF
}

DISPLAYCONFIG_PATH_INFO and DisplayConfigModeInfo are also needed in the call. They cannot be null. But these two structures include several other types which is too much to paste here.

If you follow the following link you find a sample project on github. droid-autorotate@Github

From there you can copy the missing structures.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM