private void SetInputType(IntPtr hMonitor, InputType inputType) { // get number of physical displays (assume only one for simplicity) var success = GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, out uint physicalMonitorCount); var physicalMonitorArray = new Physical_Monitor[physicalMonitorCount]; //count will be 1 for extended displays and 2(or more) for mirrored displays success = GetPhysicalMonitorsFromHMONITOR(hMonitor, physicalMonitorCount, physicalMonitorArray); var physicalMonitor = physicalMonitorArray[physicalMonitorArray.Length - 1]; //if count > 1 then we assume the laptop screen is 1st in array and the mirrored monitor is 2nd success = SetVCPFeature(physicalMonitor.hPhysicalMonitor, INPUT_SELECT, (int)inputType); success = DestroyPhysicalMonitors(physicalMonitorCount, physicalMonitorArray); }
private static bool GetPhysicalMonitorsFromHMONITOR(IntPtr hMonitor, out Physical_Monitor[] physicalMonitors) { // Allocate unmanaged memory. uint physicalMonitorCount = 0; GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, out physicalMonitorCount); int sizeofPhysicalMonitor = Marshal.SizeOf(typeof(Physical_Monitor)); IntPtr pUnmanagedMonitorArray = Marshal.AllocHGlobal(sizeofPhysicalMonitor * (int)physicalMonitorCount); // Fetch data. bool fetchSuccess = GetPhysicalMonitorsFromHMONITOR(hMonitor, physicalMonitorCount, pUnmanagedMonitorArray); // Copy data. physicalMonitors = new Physical_Monitor[physicalMonitorCount]; if (fetchSuccess) { IntPtr pUnmanagedMonitorElement = new IntPtr(pUnmanagedMonitorArray.ToInt64()); for (int i = 0; i < physicalMonitors.Length; ++i) { physicalMonitors[i] = (Physical_Monitor)Marshal.PtrToStructure(pUnmanagedMonitorElement, typeof(Physical_Monitor)); pUnmanagedMonitorElement += sizeofPhysicalMonitor; } } // Free unmanaged memory. Marshal.FreeHGlobal(pUnmanagedMonitorArray); return fetchSuccess; }