private static void LogWin32Error(string message) { int err = Marshal.GetLastWin32Error(); if (err != 0) { var errDesc = ""; if (err == 1400) { errDesc = "Invalid window handle"; } var errMsg = "Win32 Error: " + err + " '" + errDesc + "' - " + message; if (EWFDebugging.Enabled) { Debug.LogError(errMsg); } EWFDebugging.LogError(errMsg, 1); } }
public static List <SystemDisplay> GetAllDisplays() { List <SystemDisplay> allDisplays = new List <SystemDisplay>(); IntPtr hMainWindowMonitor = IntPtr.Zero; IntPtr mainWindowHandle = IntPtr.Zero; EWFDebugging.LogLine("Getting all displays.", 0, 4); try { mainWindowHandle = GetProcessMainWindow(); if (mainWindowHandle != IntPtr.Zero) { var mainWindowMonitorInfoEx = MonitorInfoEx.CreateWithDefaults(); hMainWindowMonitor = MonitorFromWindow(mainWindowHandle, MONITOR_DEFAULTTONEAREST); LogWin32Error("Error finding main window monitor"); if (hMainWindowMonitor != IntPtr.Zero) { GetMonitorInfo(hMainWindowMonitor, ref mainWindowMonitorInfoEx); LogWin32Error("Error getting main window monitor info"); } } else { EWFDebugging.LogError("Could not find the process main window handle."); } } catch (Exception e) { string err = "Error finding the main window monitor. " + e.ToString(); Debug.LogError(err); EWFDebugging.LogError(err); } var deviceDisplayMonitorCount = new Dictionary <string, uint>(); EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, delegate(IntPtr hMonitor, IntPtr hdcMonitor, ref RectStruct lprcMonitor, IntPtr dwData) { try { //Get the monitor info var monitorInfoEx = MonitorInfoEx.CreateWithDefaults(); GetMonitorInfo(hMonitor, ref monitorInfoEx); LogWin32Error(); //Get the associated display device bool mirroringDriver = false; bool attachedToDesktop = false; string deviceName = monitorInfoEx.DeviceName; if (!deviceDisplayMonitorCount.ContainsKey(deviceName)) { deviceDisplayMonitorCount.Add(deviceName, 0); } deviceDisplayMonitorCount[deviceName] += 1; var displayDevice = Display_Device.CreateWithDefaults(); int displayMonitor = 0; for (uint id = 0; EnumDisplayDevices(deviceName, id, ref displayDevice, 0); id++) { attachedToDesktop = ((displayDevice.StateFlags & DisplayDeviceStateFlags.AttachedToDesktop) == DisplayDeviceStateFlags.AttachedToDesktop); if (attachedToDesktop) { displayMonitor++; if (displayMonitor == deviceDisplayMonitorCount[deviceName]) { mirroringDriver = ((displayDevice.StateFlags & DisplayDeviceStateFlags.MirroringDriver) == DisplayDeviceStateFlags.MirroringDriver); break; //Found the display device which matches the monitor } } displayDevice.Size = Marshal.SizeOf(displayDevice); } //Skip the monitor if it's a pseudo monitor if (mirroringDriver) { return(true); } //Store the monitor info in a SystemDisplay object var display = new SystemDisplay(); display.Name = displayDevice.DeviceString; display.AttachedToDesktop = attachedToDesktop; //Should always be true within EnumDisplayMonitors display.IsPrimary = monitorInfoEx.Flags == (uint)1; display.HasMainWindow = (hMonitor == hMainWindowMonitor); display.Bounds = RectFromRectStruct(lprcMonitor); display.WorkArea = RectFromRectStruct(monitorInfoEx.WorkAreaBounds); var devMode = new DEVMODE(); EnumDisplaySettings(monitorInfoEx.DeviceName, ENUM_CURRENT_SETTINGS, ref devMode); display.PixelWidth = devMode.dmPelsWidth; display.PixelHeight = devMode.dmPelsHeight; //Add the SystemDisplay to allDisplays allDisplays.Add(display); } catch (Exception e) { Debug.LogException(e); EWFDebugging.LogError(e.ToString()); } LogWin32Error(); return(true); //Continue the enumeration }, IntPtr.Zero); LogWin32Error(); //Calculate physical bounds foreach (var display in allDisplays) { Rect physicalBounds = display.Bounds; physicalBounds.width = display.PixelWidth; physicalBounds.height = display.PixelHeight; Vector2 displayTopLeft = new Vector2(display.Bounds.xMin, display.Bounds.yMin); var displayTopLeftPhysical = GetPhysicalPoint(mainWindowHandle, displayTopLeft); physicalBounds.x = displayTopLeftPhysical.x; physicalBounds.y = displayTopLeftPhysical.y; display.PhysicalBounds = physicalBounds; } return(allDisplays); }
/// Makes sure a window covers the taskbar when it is fullscreen private static bool MakeWindowCoverTaskBar(EditorWindow editorWindow, string windowClass, string windowTitle, SystemDisplay display) { IntPtr windowHandle = IntPtr.Zero; EWFDebugging.StartTimer("Making window cover taskbar"); EWFDebugging.LogLine("Making window cover taskbar. " + (editorWindow == null ? "WindowTitle: " + (windowTitle == null ? "null" : windowTitle) : "EditorWindow: '" + editorWindow.GetWindowTitle() + "' Window Type: '" + editorWindow.GetType() + "' with class: '" + (windowClass == null ? "null" : windowClass) + "'")); if (editorWindow == null) { string fullscreenWindowClass = windowClass != null ? windowClass : "UnityPopupWndClass"; windowHandle = GetProcessWindow(fullscreenWindowClass, windowTitle, true); if (windowHandle == IntPtr.Zero) { windowHandle = GetProcessWindow(null, windowTitle, true); } } else { if (windowClass == null) { windowHandle = GetProcessWindow(editorWindow); } else { windowHandle = GetProcessWindow(windowClass, editorWindow); } } if (windowHandle == IntPtr.Zero) { EWFDebugging.LogError("Couldn't find window handle."); return(false); } IntPtr existingStyle = GetWindowLongPtr(windowHandle, GWL_STYLE); IntPtr existingExStyle = GetWindowLongPtr(windowHandle, GWL_EXSTYLE); if (editorWindow != null) { var state = EditorFullscreenState.FindWindowState(editorWindow); if (state.OriginalStyle == 0) { state.OriginalStyle = (int)existingStyle; } if (state.OriginalExStyle == 0) { state.OriginalExStyle = (int)existingExStyle; } } if (EWFDebugging.Enabled) { EWFDebugging.LogLine("before Style: " + WindowStyleToString(existingStyle)); EWFDebugging.LogLine("before ExStyle: " + WindowExStyleToString(existingExStyle)); } SetWindowLongPtr(windowHandle, GWL_STYLE, (IntPtr)(WS_POPUP | WS_VISIBLE | ((uint)existingStyle & (WS_CLIPCHILDREN | WS_CLIPSIBLINGS | WS_OVERLAPPED)))); LogWin32Error("Error setting window style"); SetWindowLongPtr(windowHandle, GWL_EXSTYLE, (IntPtr)((uint)existingExStyle & (WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR))); LogWin32Error("Error setting window ex style"); SetWindowPos(windowHandle, IntPtr.Zero, (int)display.Bounds.x, (int)display.Bounds.y, (int)display.Bounds.width, (int)display.Bounds.height, SWP.NOZORDER | SWP.FRAMECHANGED | SWP.NOACTIVATE); LogWin32Error("Error setting window position"); if (EWFDebugging.Enabled) { existingStyle = GetWindowLongPtr(windowHandle, GWL_STYLE); existingExStyle = GetWindowLongPtr(windowHandle, GWL_EXSTYLE); EWFDebugging.LogLine("after Style: " + WindowStyleToString(existingStyle)); EWFDebugging.LogLine("after ExStyle: " + WindowExStyleToString(existingExStyle)); EWFDebugging.LogTime("Making window cover taskbar"); } return(true); }