|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation 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.Runtime.InteropServices; |
| 7 | +using System.Windows.Threading; |
| 8 | +using System.Collections; |
| 9 | +using System.Text; |
| 10 | + |
| 11 | + |
| 12 | +using System.ComponentModel; |
| 13 | +using System.Windows; |
| 14 | +using System.Windows.Controls; |
| 15 | +using System.Windows.Interop; |
| 16 | +using System.Windows.Media; |
| 17 | + |
| 18 | + |
| 19 | +namespace DRT |
| 20 | +{ |
| 21 | + public class WindowStartupLocationApp : Application |
| 22 | + { |
| 23 | + [STAThread] |
| 24 | + public static int Main() |
| 25 | + { |
| 26 | + TestData.DisableProcessWindowsGhosting(); |
| 27 | + WindowStartupLocationApp theApp = new WindowStartupLocationApp(); |
| 28 | + int retVal = theApp.Run(); |
| 29 | + |
| 30 | + theApp._logger.Log(retVal == 0? "Passed" : "Failed"); |
| 31 | + return retVal; |
| 32 | + } |
| 33 | + |
| 34 | + // Tests WindowStartupLocation |
| 35 | + // Test 1 |
| 36 | + // Width = Height = Top = Left = none, WindowStartupLocation = CenterScreen |
| 37 | + // |
| 38 | + // Test 2 |
| 39 | + // Width = 400; Height = Top = Left = none; WindowStartupLocation = CenterScreen |
| 40 | + // |
| 41 | + // Test 3 |
| 42 | + // Width = none; Height = 400; Top = Left = none; WindowStartupLocation = CenterScreen |
| 43 | + // |
| 44 | + // Test 4 |
| 45 | + // Width = 400; Height = 400; Top = Left = none; WindowStartupLocation = CenterScreen |
| 46 | + // |
| 47 | + // Test 5 |
| 48 | + // Width = Height = Top = Left = none, WindowStartupLocation = CenterOwner |
| 49 | + // |
| 50 | + // Test 6 |
| 51 | + // Width = none; Height = 400; Top = Left = none; WindowStartupLocation = CenterOwner |
| 52 | + // |
| 53 | + // Test 7 |
| 54 | + // Width = 400; Height = none; Top = Left = none; WindowStartupLocation = CenterOwner |
| 55 | + // |
| 56 | + // Test 8 |
| 57 | + // Width = 400; Height = 400; Top = Left = none; WindowStartupLocation = CenterOwner |
| 58 | + // |
| 59 | + |
| 60 | + private ArrayList _array = new ArrayList(); |
| 61 | + private bool _result = true; |
| 62 | + public Logger _logger = new Logger("DrtWindowAutoLocation", "Microsoft", "Testing WindowStartupLocation API"); |
| 63 | + |
| 64 | + protected override void OnStartup(StartupEventArgs e) |
| 65 | + { |
| 66 | + Dispatcher.CurrentDispatcher.BeginInvoke( |
| 67 | + DispatcherPriority.Background, |
| 68 | + new DispatcherOperationCallback(InitTests), |
| 69 | + this); |
| 70 | + |
| 71 | + Dispatcher.CurrentDispatcher.BeginInvoke( |
| 72 | + DispatcherPriority.Background, |
| 73 | + new DispatcherOperationCallback(VerifyTests), |
| 74 | + this); |
| 75 | + |
| 76 | + Dispatcher.CurrentDispatcher.BeginInvoke( |
| 77 | + DispatcherPriority.Background, |
| 78 | + new DispatcherOperationCallback(ShutdownApp), |
| 79 | + this); |
| 80 | + |
| 81 | + base.OnStartup(e); |
| 82 | + } |
| 83 | + |
| 84 | + private object ShutdownApp(object obj) |
| 85 | + { |
| 86 | + this.Shutdown(_result == true? 0 : 1); |
| 87 | + return null; |
| 88 | + } |
| 89 | + |
| 90 | + private object InitTests(object obj) |
| 91 | + { |
| 92 | + TestData td; |
| 93 | + double defaultVal = Double.NaN; |
| 94 | + |
| 95 | + // Test 1 |
| 96 | + // Width = Height = Top = Left = none, WindowStartupLocation = CenterScreen |
| 97 | + // |
| 98 | + td = new TestData(defaultVal, defaultVal, defaultVal, defaultVal, WindowStartupLocation.CenterScreen); |
| 99 | + _array.Add(td); |
| 100 | + |
| 101 | + // Test 2 |
| 102 | + // Width = 400; Height = Top = Left = none; WindowStartupLocation = CenterScreen |
| 103 | + // |
| 104 | + td = new TestData(400, defaultVal, defaultVal, defaultVal, WindowStartupLocation.CenterScreen); |
| 105 | + _array.Add(td); |
| 106 | + |
| 107 | + // Test 3 |
| 108 | + // Width = none; Height = 400; Top = Left = none; WindowStartupLocation = CenterScreen |
| 109 | + // |
| 110 | + td = new TestData(defaultVal, 400, defaultVal, defaultVal, WindowStartupLocation.CenterScreen); |
| 111 | + _array.Add(td); |
| 112 | + |
| 113 | + // Test 4 |
| 114 | + // Width = 400; Height = 400; Top = Left = none; WindowStartupLocation = CenterScreen |
| 115 | + // |
| 116 | + td = new TestData(400, 400, defaultVal, defaultVal, WindowStartupLocation.CenterScreen); |
| 117 | + _array.Add(td); |
| 118 | + |
| 119 | + // Test 5 |
| 120 | + // Width = Height = Top = Left = none, WindowStartupLocation = CenterOwner |
| 121 | + // |
| 122 | + td = new TestData(defaultVal, defaultVal, defaultVal, defaultVal, WindowStartupLocation.CenterOwner); |
| 123 | + _array.Add(td); |
| 124 | + |
| 125 | + // Test 6 |
| 126 | + // Width = none; Height = 400; Top = Left = none; WindowStartupLocation = CenterOwner |
| 127 | + // |
| 128 | + td = new TestData(defaultVal, 400, defaultVal, defaultVal, WindowStartupLocation.CenterOwner); |
| 129 | + _array.Add(td); |
| 130 | + |
| 131 | + // Test 7 |
| 132 | + // Width = 400; Height = none; Top = Left = none; WindowStartupLocation = CenterOwner |
| 133 | + // |
| 134 | + td = new TestData(400, defaultVal, defaultVal, defaultVal, WindowStartupLocation.CenterOwner); |
| 135 | + _array.Add(td); |
| 136 | + |
| 137 | + // Test 8 |
| 138 | + // Width = 400; Height = 400; Top = Left = none; WindowStartupLocation = CenterOwner |
| 139 | + // |
| 140 | + td = new TestData(400, 400, defaultVal, defaultVal, WindowStartupLocation.CenterOwner); |
| 141 | + _array.Add(td); |
| 142 | + |
| 143 | + |
| 144 | + return null; |
| 145 | + } |
| 146 | + |
| 147 | + private object VerifyTests(object obj) |
| 148 | + { |
| 149 | + foreach (TestData td in _array) |
| 150 | + { |
| 151 | + _result = (td.Verify() == false? false: _result); |
| 152 | + } |
| 153 | + |
| 154 | + return null; |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + class TestData |
| 159 | + { |
| 160 | + double _width = Double.NaN; |
| 161 | + double _height = Double.NaN; |
| 162 | + double _top = Double.NaN; |
| 163 | + double _left = Double.NaN; |
| 164 | + WindowStartupLocation _wsl = WindowStartupLocation.Manual; |
| 165 | + Window _win; |
| 166 | + Window _ownerWin; |
| 167 | + IntPtr _winHandle = IntPtr.Zero; |
| 168 | + |
| 169 | + public TestData(double width, double height, double left, double top, WindowStartupLocation wsl) |
| 170 | + { |
| 171 | + _width = width; |
| 172 | + _height = height; |
| 173 | + _left = left; |
| 174 | + _top = top; |
| 175 | + _wsl = wsl; |
| 176 | + |
| 177 | + _win = new Window(); |
| 178 | + _win.Width = _width; |
| 179 | + _win.Height = _height; |
| 180 | + _win.Top = _top; |
| 181 | + _win.Left = _left; |
| 182 | + _win.WindowStartupLocation = _wsl; |
| 183 | + _win.Title = String.Format("Width={0}, Height={1}, Left={2}, Top={3}, WindowStartupLocation={4}", _width, _height, _left, _top, _wsl); |
| 184 | + |
| 185 | + if (_wsl == WindowStartupLocation.CenterOwner) |
| 186 | + { |
| 187 | + RECT workArea = new RECT(); |
| 188 | + SystemParametersInfo(SPI_GETWORKAREA, 0, ref workArea, 0); |
| 189 | + int workAreaWidth = workArea.right - workArea.left; |
| 190 | + int workAreaHeight = workArea.bottom - workArea.top; |
| 191 | + |
| 192 | + _ownerWin = new Window(); |
| 193 | + _ownerWin.Width = workAreaWidth/2; |
| 194 | + _ownerWin.Height = workAreaHeight/2; |
| 195 | + _ownerWin.Left = workArea.left + workAreaWidth/4; |
| 196 | + _ownerWin.Top = workArea.top + workAreaHeight/4; |
| 197 | + _ownerWin.Show(); |
| 198 | + |
| 199 | + _win.Owner = _ownerWin; |
| 200 | + } |
| 201 | + |
| 202 | + _win.Show(); |
| 203 | + |
| 204 | + WindowInteropHelper wih = new WindowInteropHelper(_win); |
| 205 | + _winHandle = wih.Handle; |
| 206 | + } |
| 207 | + |
| 208 | + public bool Verify() |
| 209 | + { |
| 210 | + int left, top, width, height; |
| 211 | + double ownerLeft = 0 , ownerTop = 0, ownerWidth = 0, ownerHeight = 0; |
| 212 | + |
| 213 | + RECT rc = new RECT(); |
| 214 | + GetWindowRect(_winHandle, ref rc); |
| 215 | + left = rc.left; |
| 216 | + top = rc.top; |
| 217 | + |
| 218 | + if ((left != _win.Left) || (top != _win.Top)) |
| 219 | + { |
| 220 | + App._logger.Log("********************************"); |
| 221 | + App._logger.Log("Failed --- GetValue Top&Left"); |
| 222 | + return false; |
| 223 | + } |
| 224 | + |
| 225 | + width = rc.right - rc.left; |
| 226 | + height = rc.bottom - rc.top; |
| 227 | + |
| 228 | + if ((width != _win.ActualWidth) || (height != _win.ActualHeight)) |
| 229 | + { |
| 230 | + App._logger.Log("********************************"); |
| 231 | + App._logger.Log("Failed --- GetValue ActualWidth/ActualHeight"); |
| 232 | + return false; |
| 233 | + } |
| 234 | + |
| 235 | + if (_ownerWin != null) |
| 236 | + { |
| 237 | + WindowInteropHelper wih = new WindowInteropHelper(_ownerWin); |
| 238 | + GetWindowRect(wih.Handle, ref rc); |
| 239 | + ownerLeft = rc.left; |
| 240 | + ownerTop = rc.top; |
| 241 | + ownerWidth = rc.right - rc.left; |
| 242 | + ownerHeight = rc.bottom - rc.top; |
| 243 | + } |
| 244 | + |
| 245 | + switch (_wsl) |
| 246 | + { |
| 247 | + case WindowStartupLocation.Manual: |
| 248 | + { |
| 249 | + if ( ( Double.IsNaN(_width) == false && (int)_width != (int)width ) |
| 250 | + || ( Double.IsNaN(_height) == false && (int)_height != (int)height ) |
| 251 | + || ( Double.IsNaN(_left) == false && (int)_left != (int)left ) |
| 252 | + || ( Double.IsNaN(_top) == false && (int)_top != (int)top ) |
| 253 | + ) |
| 254 | + { |
| 255 | + App._logger.Log("********************************"); |
| 256 | + App._logger.Log("Failed --- WindowStartupLocation.Manual"); |
| 257 | + App._logger.Log(String.Format("SetValues : Width={0}, Height={1}, Left={2}, Top={3}", _width, _height, _left, _top)); |
| 258 | + App._logger.Log(String.Format("GetValues : Width={0}, Height={1}, Left={2}, Top={3}", width, height, left, top)); |
| 259 | + return false; |
| 260 | + } |
| 261 | + break; |
| 262 | + } |
| 263 | + case WindowStartupLocation.CenterOwner: |
| 264 | + { |
| 265 | + if ((AreClose((int)ownerLeft, (int)(left - (ownerWidth - width)/2)) == false) || |
| 266 | + (AreClose((int)ownerTop, (int)(top - (ownerHeight - height)/2)) == false)) |
| 267 | + { |
| 268 | + App._logger.Log("********************************"); |
| 269 | + App._logger.Log("Failed --- WindowStartupLocation.CenterOwner"); |
| 270 | + App._logger.Log(String.Format("Owner co-ods : Width={0}, Height={1}, Left={2}, Top={3}", ownerWidth, ownerHeight, ownerLeft, ownerTop)); |
| 271 | + App._logger.Log(String.Format("SetValues : Width={0}, Height={1}, Left={2}, Top={3}", _width, _height, _left, _top)); |
| 272 | + App._logger.Log(String.Format("GetValues : Width={0}, Height={1}, Left={2}, Top={3}", width, height, left, top)); |
| 273 | + return false; |
| 274 | + } |
| 275 | + break; |
| 276 | + } |
| 277 | + case WindowStartupLocation.CenterScreen: |
| 278 | + { |
| 279 | + IntPtr hMonitor = MonitorFromWindow(_winHandle, MONITOR_DEFAULTTONEAREST); |
| 280 | + |
| 281 | + if (hMonitor != IntPtr.Zero) |
| 282 | + { |
| 283 | + MONITORINFOEX monitorInfo = new MONITORINFOEX(); |
| 284 | + monitorInfo.cbSize = Marshal.SizeOf(typeof(MONITORINFOEX)); |
| 285 | + |
| 286 | + if (GetMonitorInfo(hMonitor, monitorInfo)) |
| 287 | + { |
| 288 | + RECT workAreaRect = monitorInfo.rcWork; |
| 289 | + double workAreaWidth = workAreaRect.right - workAreaRect.left; |
| 290 | + double workAreaHeight = workAreaRect.bottom - workAreaRect.top; |
| 291 | + |
| 292 | + if ((AreClose(left, (int)(workAreaRect.left + (workAreaWidth - width)/2)) == false) || |
| 293 | + (AreClose(top, (int)(workAreaRect.top + (workAreaHeight - height)/2)) == false)) |
| 294 | + { |
| 295 | + App._logger.Log("********************************"); |
| 296 | + App._logger.Log("Failed --- WindowStartupLocation.CenterScreen"); |
| 297 | + App._logger.Log(String.Format("Monitor (work area) co-ods : Width={0}, Height={1}, Left={2}, Top={3}", workAreaWidth, workAreaHeight, workAreaRect.left, workAreaRect.top)); |
| 298 | + App._logger.Log(String.Format("SetValues : Width={0}, Height={1}, Left={2}, Top={3}", _width, _height, _left, _top)); |
| 299 | + App._logger.Log(String.Format("GetValues : Width={0}, Height={1}, Left={2}, Top={3}", width, height, left, top)); |
| 300 | + |
| 301 | + return false; |
| 302 | + } |
| 303 | + } |
| 304 | + } |
| 305 | + break; |
| 306 | + } |
| 307 | + default: |
| 308 | + break; |
| 309 | + } |
| 310 | + return true; |
| 311 | + } |
| 312 | + |
| 313 | + private bool AreClose(int val1, int val2) |
| 314 | + { |
| 315 | + if (Math.Abs(val1 - val2) <= 1) |
| 316 | + { |
| 317 | + return true; |
| 318 | + } |
| 319 | + return false; |
| 320 | + } |
| 321 | + |
| 322 | + private WindowStartupLocationApp App |
| 323 | + { |
| 324 | + get |
| 325 | + { |
| 326 | + return (WindowStartupLocationApp)Application.Current; |
| 327 | + } |
| 328 | + } |
| 329 | + |
| 330 | + private const int SPI_GETWORKAREA = 0x0030; |
| 331 | + private const int MONITOR_DEFAULTTONEAREST = 0x00000002; |
| 332 | + |
| 333 | + [DllImport("user32.dll", ExactSpelling=true, CharSet=CharSet.Auto)] |
| 334 | + private static extern bool GetWindowRect(IntPtr hWnd, [In, Out] ref RECT rect); |
| 335 | + |
| 336 | + [DllImport("user32.dll", CharSet=CharSet.Auto)] |
| 337 | + private static extern bool SystemParametersInfo(int nAction, int nParam, ref RECT rc, int nUpdate); |
| 338 | + |
| 339 | + [DllImport("user32.dll", ExactSpelling=true)] |
| 340 | + private static extern IntPtr MonitorFromWindow(IntPtr handle, int flags); |
| 341 | + |
| 342 | + [DllImport("user32.dll", CharSet=CharSet.Auto)] |
| 343 | + private static extern bool GetMonitorInfo(IntPtr hmonitor, [In, Out]MONITORINFOEX info); |
| 344 | + |
| 345 | + [DllImport("user32.dll", ExactSpelling=true, CharSet=CharSet.Auto)] |
| 346 | + internal extern static void DisableProcessWindowsGhosting(); |
| 347 | + |
| 348 | + [StructLayout(LayoutKind.Sequential,CharSet=CharSet.Auto)] |
| 349 | + private class MONITORINFOEX { |
| 350 | + internal int cbSize = Marshal.SizeOf(typeof(MONITORINFOEX)); |
| 351 | + internal RECT rcMonitor = new RECT(); |
| 352 | + internal RECT rcWork = new RECT(); |
| 353 | + internal int dwFlags = 0; |
| 354 | + [MarshalAs(UnmanagedType.ByValArray, SizeConst=32)] |
| 355 | + internal char[] szDevice = new char[32]; |
| 356 | + } |
| 357 | + |
| 358 | + [StructLayout(LayoutKind.Sequential)] |
| 359 | + private struct RECT { |
| 360 | + public int left; |
| 361 | + public int top; |
| 362 | + public int right; |
| 363 | + public int bottom; |
| 364 | + |
| 365 | + public RECT(int left, int top, int right, int bottom) { |
| 366 | + this.left = left; |
| 367 | + this.top = top; |
| 368 | + this.right = right; |
| 369 | + this.bottom = bottom; |
| 370 | + } |
| 371 | + |
| 372 | + public static RECT FromXYWH(int x, int y, int width, int height) { |
| 373 | + return new RECT(x, y, x + width, y + height); |
| 374 | + } |
| 375 | + } |
| 376 | + |
| 377 | + } |
| 378 | +} |
| 379 | + |
0 commit comments