/ src / modules / Workspaces / WorkspacesEditor / OverlayWindow.xaml.cs
OverlayWindow.xaml.cs
 1  // Copyright (c) Microsoft Corporation
 2  // The Microsoft Corporation licenses this file to you under the MIT license.
 3  // See the LICENSE file in the project root for more information.
 4  
 5  using System;
 6  using System.Windows;
 7  
 8  using WorkspacesEditor.Utils;
 9  
10  namespace WorkspacesEditor
11  {
12      /// <summary>
13      /// Interaction logic for OverlayWindow.xaml
14      /// </summary>
15      public partial class OverlayWindow : Window
16      {
17          private int _targetX;
18          private int _targetY;
19          private int _targetWidth;
20          private int _targetHeight;
21  
22          public OverlayWindow()
23          {
24              InitializeComponent();
25              SourceInitialized += OnWindowSourceInitialized;
26          }
27  
28          /// <summary>
29          /// Sets the target bounds for the overlay window.
30          /// The window will be positioned using DPI-unaware context after initialization.
31          /// </summary>
32          public void SetTargetBounds(int x, int y, int width, int height)
33          {
34              _targetX = x;
35              _targetY = y;
36              _targetWidth = width;
37              _targetHeight = height;
38  
39              // Set initial WPF properties (will be corrected after HWND creation)
40              Left = x;
41              Top = y;
42              Width = width;
43              Height = height;
44          }
45  
46          private void OnWindowSourceInitialized(object sender, EventArgs e)
47          {
48              // Reposition window using DPI-unaware context to match the virtual coordinates.
49              // This fixes overlay positioning on mixed-DPI multi-monitor setups.
50              NativeMethods.SetWindowPositionDpiUnaware(this, _targetX, _targetY, _targetWidth, _targetHeight);
51          }
52      }
53  }