| | |
| | | |
| | | public void SetClickThrough(IntPtr hwnd, bool enabled) |
| | | { |
| | | SetStyleFlag(hwnd, NativeMethods.WsExLayered | NativeMethods.WsExTransparent, enabled, "SetClickThrough"); |
| | | // Only toggle WS_EX_TRANSPARENT. WS_EX_LAYERED is owned by SetLayered / WPF |
| | | // AllowsTransparency; clearing it here would break per-pixel transparency. |
| | | SetStyleFlag(hwnd, NativeMethods.WsExTransparent, enabled, "SetClickThrough"); |
| | | } |
| | | |
| | | public void SetToolWindow(IntPtr hwnd, bool enabled) |
| | |
| | | SetStyleFlag(hwnd, NativeMethods.WsExNoActivate, enabled, "SetNoActivate"); |
| | | } |
| | | |
| | | public void SetLayered(IntPtr hwnd, bool enabled, byte opacity) |
| | | public void SetLayered(IntPtr hwnd, bool enabled) |
| | | { |
| | | // Only manage the WS_EX_LAYERED style bit. Never call SetLayeredWindowAttributes here: |
| | | // it switches the window from WPF AllowsTransparency per-pixel alpha (UpdateLayeredWindow) |
| | | // to constant-alpha mode, in which transparent pixels composite as opaque black. |
| | | // Global opacity is handled by Window.Opacity instead. |
| | | SetStyleFlag(hwnd, NativeMethods.WsExLayered, enabled, "SetLayered"); |
| | | if (enabled && !NativeMethods.SetLayeredWindowAttributes(hwnd, 0, opacity, NativeMethods.LwaAlpha)) |
| | | { |
| | | RecordWin32Failure("SetLayeredWindowAttributes", hwnd); |
| | | } |
| | | } |
| | | |
| | | public void MoveToDesktopLayer(IntPtr hwnd, DesktopLayerMode mode, MonitorInfo monitor) |
| | | => MoveToDesktopLayer(hwnd, mode, monitor.Bounds); |
| | | |
| | | public void MoveToDesktopLayer(IntPtr hwnd, DesktopLayerMode mode, RectD bounds) |
| | | public void MoveToDesktopLayer(IntPtr hwnd, DesktopLayerMode mode, MonitorInfo monitor, RectD bounds) |
| | | { |
| | | var candidates = mode == DesktopLayerMode.WorkerW |
| | | ? _strategies.OrderBy(strategy => strategy is WorkerWDesktopLayerStrategy ? 0 : 1) |
| | |
| | | continue; |
| | | } |
| | | |
| | | if (strategy.TryAttachDesktopWindow(hwnd, bounds, out var error)) |
| | | if (strategy.TryAttachDesktopWindow(hwnd, monitor, bounds, out var error)) |
| | | { |
| | | return; |
| | | } |
| | |
| | | |
| | | public void RestoreTopLevelWindow(IntPtr hwnd) |
| | | { |
| | | RestoreTopLevelStyle(hwnd); |
| | | NativeMethods.SetParent(hwnd, IntPtr.Zero); |
| | | var error = NativeMethods.GetLastError(); |
| | | if (error != 0) |
| | |
| | | DiagnosticCodes.Win32InteropFailed, |
| | | "RestoreTopLevelWindow failed.", |
| | | new Dictionary<string, string> { ["hwnd"] = hwnd.ToString("X"), ["error"] = error.ToString() }); |
| | | } |
| | | |
| | | if (!NativeMethods.SetWindowPos( |
| | | hwnd, |
| | | IntPtr.Zero, |
| | | 0, |
| | | 0, |
| | | 0, |
| | | 0, |
| | | NativeMethods.SwpNoMove | NativeMethods.SwpNoSize | NativeMethods.SwpNoZOrder | NativeMethods.SwpNoActivate | NativeMethods.SwpFrameChanged)) |
| | | { |
| | | RecordWin32Failure("RestoreTopLevelWindow.SetWindowPos", hwnd); |
| | | } |
| | | } |
| | | |
| | |
| | | return true; |
| | | } |
| | | |
| | | var id = string.IsNullOrWhiteSpace(native.DeviceName) |
| | | var scale = 1.0; |
| | | var hr = NativeMethods.GetDpiForMonitor(hMonitor, NativeMethods.MdtEffectiveDpi, out var dpiX, out _); |
| | | if (hr == 0 && dpiX > 0) |
| | | { |
| | | scale = dpiX / 96.0; |
| | | } |
| | | |
| | | var physicalBounds = ToRectD(native.Monitor); |
| | | var physicalWorkArea = ToRectD(native.WorkArea); |
| | | var dipBounds = new RectD( |
| | | physicalBounds.X / scale, |
| | | physicalBounds.Y / scale, |
| | | physicalBounds.Width / scale, |
| | | physicalBounds.Height / scale); |
| | | var dipWorkArea = new RectD( |
| | | physicalWorkArea.X / scale, |
| | | physicalWorkArea.Y / scale, |
| | | physicalWorkArea.Width / scale, |
| | | physicalWorkArea.Height / scale); |
| | | |
| | | var deviceName = string.IsNullOrWhiteSpace(native.DeviceName) |
| | | ? $"monitor-{monitors.Count + 1}" |
| | | : native.DeviceName; |
| | | monitors.Add(new MonitorInfo |
| | | { |
| | | Id = new MonitorId(native.Flags ? "primary" : id), |
| | | Bounds = ToRectD(native.Monitor), |
| | | WorkArea = ToRectD(native.WorkArea), |
| | | Id = new MonitorId(deviceName), |
| | | Bounds = dipBounds, |
| | | WorkArea = dipWorkArea, |
| | | IsPrimary = native.Flags, |
| | | Scale = 1 |
| | | Scale = scale |
| | | }); |
| | | return true; |
| | | }, IntPtr.Zero); |
| | |
| | | Id = new MonitorId("primary"), |
| | | Bounds = new RectD(0, 0, SystemParameters.PrimaryScreenWidth, SystemParameters.PrimaryScreenHeight), |
| | | WorkArea = new RectD(0, 0, SystemParameters.WorkArea.Width, SystemParameters.WorkArea.Height), |
| | | IsPrimary = true |
| | | IsPrimary = true, |
| | | Scale = 1 |
| | | }]; |
| | | } |
| | | |
| | |
| | | var styleFlag = unchecked((uint)flag); |
| | | var currentStyle = unchecked((uint)current); |
| | | var updated = enabled ? currentStyle | styleFlag : currentStyle & ~styleFlag; |
| | | NativeMethods.SetWindowLongPtr(hwnd, NativeMethods.GwlExStyle, new IntPtr(updated)); |
| | | var error = NativeMethods.GetLastError(); |
| | | if (error != 0) |
| | | if (updated == currentStyle) |
| | | { |
| | | _diagnostics.Record( |
| | | DiagnosticSeverity.Warning, |
| | | DiagnosticCodes.Win32InteropFailed, |
| | | $"{operation} failed.", |
| | | new Dictionary<string, string> { ["hwnd"] = hwnd.ToString("X"), ["error"] = error.ToString() }); |
| | | return; |
| | | } |
| | | |
| | | NativeMethods.SetWindowLongPtr(hwnd, NativeMethods.GwlExStyle, new IntPtr(unchecked((int)updated))); |
| | | // Extended-style changes (especially WS_EX_TRANSPARENT) do not reliably update hit-testing |
| | | // until the frame is refreshed. Without this, clearing click-through in edit/safe-input |
| | | // mode can leave the window visually present but permanently unclickable. |
| | | if (!NativeMethods.SetWindowPos( |
| | | hwnd, |
| | | IntPtr.Zero, |
| | | 0, |
| | | 0, |
| | | 0, |
| | | 0, |
| | | NativeMethods.SwpNoMove | NativeMethods.SwpNoSize | NativeMethods.SwpNoZOrder | NativeMethods.SwpNoActivate | NativeMethods.SwpFrameChanged)) |
| | | { |
| | | RecordWin32Failure($"{operation}.SetWindowPos", hwnd); |
| | | } |
| | | } |
| | | |
| | |
| | | { |
| | | return new RectD(rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top); |
| | | } |
| | | |
| | | private static void RestoreTopLevelStyle(IntPtr hwnd) |
| | | { |
| | | var current = unchecked((uint)NativeMethods.GetWindowLongPtr(hwnd, NativeMethods.GwlStyle).ToInt64()); |
| | | var updated = (current | NativeMethods.WsPopup | NativeMethods.WsVisible) & ~NativeMethods.WsChild; |
| | | NativeMethods.SetWindowLongPtr(hwnd, NativeMethods.GwlStyle, new IntPtr(unchecked((int)updated))); |
| | | } |
| | | } |