From 40c559c41d937ef2df495a721f9d4ab09f2fc6fc Mon Sep 17 00:00:00 2001 From: Nikita Tsukanov Date: Wed, 2 Dec 2015 01:26:10 +0300 Subject: [PATCH] WndHost is unreliable --- .../Perspex.Designer/AppHost/WindowHost.cs | 48 ++++++++++++------- .../InProcDesigner/InProcDesignerView.xaml.cs | 2 +- .../Perspex.Designer/PerspexDesigner.xaml | 2 +- .../Perspex.Designer/PerspexDesigner.xaml.cs | 32 ++----------- 4 files changed, 39 insertions(+), 45 deletions(-) diff --git a/src/Windows/Perspex.Designer/AppHost/WindowHost.cs b/src/Windows/Perspex.Designer/AppHost/WindowHost.cs index 2ca7ad8c3a..a98850bcf3 100644 --- a/src/Windows/Perspex.Designer/AppHost/WindowHost.cs +++ b/src/Windows/Perspex.Designer/AppHost/WindowHost.cs @@ -11,13 +11,18 @@ namespace Perspex.Designer.AppHost { class WindowHost : UserControl { - public WindowHost() + private readonly bool _supportScroll; + + public WindowHost(bool supportScroll) { - AutoScroll = true; - VerticalScroll.Enabled = true; - HorizontalScroll.Enabled = true; + _supportScroll = supportScroll; + if (_supportScroll) + { + AutoScroll = true; + VerticalScroll.Enabled = true; + HorizontalScroll.Enabled = true; + } SetStyle(ControlStyles.AllPaintingInWmPaint, true); - Text = "ScrollableArea"; Controls.Add(_windowHost); _windowHost.Anchor = AnchorStyles.None; _timer.Tick += delegate @@ -102,25 +107,36 @@ namespace Perspex.Designer.AppHost { if (_hWnd != IntPtr.Zero) { - WinApi.RECT rc; - WinApi.GetWindowRect(_hWnd, out rc); - _desiredWidth = rc.Right - rc.Left; - _desiredHeight = rc.Bottom - rc.Top; - var pt = _windowHost.PointToClient(new Point(rc.Left, rc.Top)); + if (_supportScroll) + { + WinApi.RECT rc; + WinApi.GetWindowRect(_hWnd, out rc); + _desiredWidth = rc.Right - rc.Left; + _desiredHeight = rc.Bottom - rc.Top; + var pt = _windowHost.PointToClient(new Point(rc.Left, rc.Top)); - if (!(pt.Y == 0 && pt.X == 0 && _desiredWidth == _windowHost.Width && _desiredHeight == _windowHost.Height)) + if ( + !(pt.Y == 0 && pt.X == 0 && _desiredWidth == _windowHost.Width && + _desiredHeight == _windowHost.Height)) + { + _windowHost.Width = _desiredWidth; + _windowHost.Height = _desiredHeight; + WinApi.MoveWindow(_hWnd, 0, 0, _desiredWidth, _desiredHeight, true); + } + FixPosition(); + } + else { - _windowHost.Width = _desiredWidth; - _windowHost.Height = _desiredHeight; - WinApi.MoveWindow(_hWnd, 0, 0, _desiredWidth, _desiredHeight, true); + _windowHost.Width = Width; + _windowHost.Height = Height; + WinApi.MoveWindow(_hWnd, 0, 0, Width, Height, true); } - FixPosition(); } } protected override void OnResize(EventArgs e) { - FixPosition(); + FixWindow(); base.OnResize(e); } } diff --git a/src/Windows/Perspex.Designer/InProcDesigner/InProcDesignerView.xaml.cs b/src/Windows/Perspex.Designer/InProcDesigner/InProcDesignerView.xaml.cs index 9e617f1560..c936b2faa8 100644 --- a/src/Windows/Perspex.Designer/InProcDesigner/InProcDesignerView.xaml.cs +++ b/src/Windows/Perspex.Designer/InProcDesigner/InProcDesignerView.xaml.cs @@ -33,7 +33,7 @@ namespace Perspex.Designer.InProcDesigner InitializeComponent(); DataContext = _appModel; _appModel.PropertyChanged += ModelPropertyChanged; - WindowHostControl.Child = _host = new WindowHost(); + WindowHostControl.Child = _host = new WindowHost(true); HandleVisibility(); HandleWindow(); diff --git a/src/Windows/Perspex.Designer/PerspexDesigner.xaml b/src/Windows/Perspex.Designer/PerspexDesigner.xaml index a5a14d170e..96679be499 100644 --- a/src/Windows/Perspex.Designer/PerspexDesigner.xaml +++ b/src/Windows/Perspex.Designer/PerspexDesigner.xaml @@ -6,7 +6,7 @@ mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"> - + diff --git a/src/Windows/Perspex.Designer/PerspexDesigner.xaml.cs b/src/Windows/Perspex.Designer/PerspexDesigner.xaml.cs index 2f944d7251..ad4488d5c6 100644 --- a/src/Windows/Perspex.Designer/PerspexDesigner.xaml.cs +++ b/src/Windows/Perspex.Designer/PerspexDesigner.xaml.cs @@ -73,41 +73,19 @@ namespace Perspex.Designer { if (e.PropertyName == nameof(ProcessHost.WindowHandle)) { - if (NativeContainer.Content != null) + if (NativeContainer.Child == null) { - var wndHost = ((HwndHost) NativeContainer.Content); - NativeContainer.Content = null; - wndHost?.Dispose(); + NativeContainer.Child = new WindowHost(false); } - if (_host.WindowHandle != IntPtr.Zero) { - var host = new NativeWindowHost(_host.WindowHandle); - NativeContainer.Content = host; + var wndHost = ((WindowHost) NativeContainer.Child); + wndHost.SetWindow(_host.WindowHandle); } - } - } - - class NativeWindowHost :HwndHost - { - private readonly IntPtr _hWnd; - - public NativeWindowHost(IntPtr hWnd) - { - _hWnd = hWnd; - } - - protected override HandleRef BuildWindowCore(HandleRef hwndParent) - { - WinApi.SetParent(_hWnd, hwndParent.Handle); - return new HandleRef(this, _hWnd); - } - protected override void DestroyWindowCore(HandleRef hwnd) - { - WinApi.SendMessage(hwnd.Handle, WinApi.WM_CLOSE, IntPtr.Zero, IntPtr.Zero); } } + public void KillProcess() { _host.Kill();