public WpfWindowResizeTestMainWindow() { InitializeComponent(); CurrentDpi = Dpi.DpiFromHdcForTheEntireScreen; StateMonitorWindow = new StateMonitoringWindow(); StateMonitorWindow.Initialize(this); StateMonitorWindow.Show(); this.MinWidth = 192; this.MinHeight = 120; this.KeyDown += (sender, e) => { if (e.Key == Key.Escape) { this.Close(); } }; Resizing = new AspectRatioKeepingWindowResize(this); Resizing.MarginBetweenWindowAndContent = new Thickness( windowBorder.BorderThickness.Left + windowBorder.Margin.Left, windowBorder.BorderThickness.Top + windowBorder.Margin.Top, windowBorder.BorderThickness.Right + windowBorder.Margin.Right, windowBorder.BorderThickness.Bottom + windowBorder.Margin.Bottom); Resizing.ContentAspectRatio = 384.0 / 240.0; Resizing.GripEdgeThickness = new Thickness(50); windowsFormsCursorPositionMonitoringTimer = new System.Windows.Threading.DispatcherTimer() { Interval = TimeSpan.FromMilliseconds(1000.0 / 60.0) }; windowsFormsCursorPositionMonitoringTimer.Tick += delegate { if (isBeingResizedByMouse && Mouse.LeftButton == MouseButtonState.Released) { Resizing.DraggingRegion = AspectRatioKeepingWindowDraggingRegions.None; return; } #if false // NOTE: App.configに、下記の記述を行わない限り、DPIが100%以外だとまともな値にならない!!!!! // <appSettings><add key="EnableWindowsFormsHighDpiAutoResizing" value="true" /></appSettings> var pos = System.Windows.Forms.Cursor.Position; var x = pos.X; var y = pos.Y; #elif false // NOTE: これもダメ! var pos = Mouse.GetPosition(this); var x = this.Left + pos.X; var y = this.Top + pos.Y; #else var pos = CurrentDpi.GetScaledPosition(System.Windows.Forms.Cursor.Position); var x = pos.X; var y = pos.Y; #endif // NOTE: 渡された値は内部でDebug.Writelineで表示される。 Resizing.ResizeWithKeepingContentAspectRatio(x, y); }; Resizing.IsResizingChanged += (sender, e) => { windowsFormsCursorPositionMonitoringTimer.IsEnabled = Resizing.IsResizing; if (Resizing.IsResizing == false) { isBeingResizedByMouse = false; } }; Resizing.DraggingRegionChanged += (sender, e) => { if (Resizing.DraggingRegion == AspectRatioKeepingWindowDraggingRegions.Center) { this.DragMove(); } }; this.MouseLeftButtonDown += (sender, e) => { CurrentDpi = Dpi.DpiFromHdcForTheEntireScreen; Debug.WriteLine("CurrentDpi: {0:G5}, {1:G5}", CurrentDpi.X, CurrentDpi.Y); var scaledCursorPosition = CurrentDpi.GetScaledPosition(System.Windows.Forms.Cursor.Position); Resizing.OnMouseDownOrTouchOnWindow(scaledCursorPosition.X, scaledCursorPosition.Y); isBeingResizedByMouse = true; }; this.MouseLeftButtonUp += (sender, e) => { Resizing.DraggingRegion = AspectRatioKeepingWindowDraggingRegions.None; }; }