diff --git a/src/Windows/Perspex.Designer/AppHost/PerspexAppHost.cs b/src/Windows/Perspex.Designer/AppHost/PerspexAppHost.cs index 1bbabeb8cd..5e099e6eb0 100644 --- a/src/Windows/Perspex.Designer/AppHost/PerspexAppHost.cs +++ b/src/Windows/Perspex.Designer/AppHost/PerspexAppHost.cs @@ -46,11 +46,19 @@ namespace Perspex.Designer.AppHost { if(!_initSuccess) return; - dynamic window = Activator.CreateInstance(LookupType("Perspex.Controls.Window")); - _xamlReader(new MemoryStream(Encoding.UTF8.GetBytes(xaml)), window); - var hWnd = (IntPtr) window.PlatformImpl.Handle; - _host.SetWindow(hWnd); - window.Show(); + try + { + dynamic window = Activator.CreateInstance(LookupType("Perspex.Controls.Window")); + _xamlReader(new MemoryStream(Encoding.UTF8.GetBytes(xaml)), window); + var hWnd = (IntPtr) window.PlatformImpl.Handle; + _host.SetWindow(hWnd); + window.Show(); + } + catch(Exception e) + { + _host.SetWindow(IntPtr.Zero); + _host.PlaceholderText = e.ToString(); + } } void UpdateState(string state) diff --git a/src/Windows/Perspex.Designer/AppHost/WindowHost.cs b/src/Windows/Perspex.Designer/AppHost/WindowHost.cs index 90daca18e4..260cb96ff7 100644 --- a/src/Windows/Perspex.Designer/AppHost/WindowHost.cs +++ b/src/Windows/Perspex.Designer/AppHost/WindowHost.cs @@ -13,26 +13,43 @@ namespace Perspex.Designer.AppHost { public WindowHost() { - SetStyle(ControlStyles.AllPaintingInWmPaint, true); + BackColor = Color.AliceBlue; + _textBox = new TextBox {Dock = DockStyle.Fill, ReadOnly = true, Multiline = true, Visible = false}; + Controls.Add(_textBox); } + + private IntPtr _hWnd; + private TextBox _textBox; - protected override void OnPaint(PaintEventArgs e) + public string PlaceholderText { - e.Graphics.FillRectangle(Brushes.AliceBlue, ClientRectangle); + get { return _textBox.Text; } + set + { + _textBox.Text = value; + FixWindow(); + } } - private IntPtr _hWnd; + public void SetWindow(IntPtr hWnd) { + if (_hWnd != IntPtr.Zero) + WinApi.SendMessage(_hWnd, WinApi.WM_CLOSE, IntPtr.Zero, IntPtr.Zero); _hWnd = hWnd; - WinApi.SetParent(hWnd, Handle); - FixWindow(); + if (_hWnd != IntPtr.Zero) + { + WinApi.SetParent(hWnd, Handle); + FixWindow(); + } } void FixWindow() { - WinApi.MoveWindow(_hWnd, 0, 0, Width, Height, true); + if (_hWnd != IntPtr.Zero) + WinApi.MoveWindow(_hWnd, 0, 0, Width, Height, true); + _textBox.Visible = _hWnd == IntPtr.Zero && !string.IsNullOrWhiteSpace(_textBox.Text); } protected override void OnResize(EventArgs e) diff --git a/src/Windows/Perspex.Designer/Comm/ProcessHost.cs b/src/Windows/Perspex.Designer/Comm/ProcessHost.cs index 4b61d62b18..8870ee3d97 100644 --- a/src/Windows/Perspex.Designer/Comm/ProcessHost.cs +++ b/src/Windows/Perspex.Designer/Comm/ProcessHost.cs @@ -65,6 +65,7 @@ namespace Perspex.Designer.Comm void OnExited(object sender, EventArgs eventArgs) { _comm.Dispose(); + _proc = null; _dispatcher.Post(_ => { IsAlive = false; @@ -78,7 +79,11 @@ namespace Perspex.Designer.Comm if (_proc != null) { _proc.Exited -= OnExited; - _proc.Kill(); + try + { + _proc.Kill(); + } + catch { } OnExited(null, null); State = "Restarting..."; } diff --git a/src/Windows/Perspex.Designer/WinApi.cs b/src/Windows/Perspex.Designer/WinApi.cs index f9cd76e85b..04e9dbed29 100644 --- a/src/Windows/Perspex.Designer/WinApi.cs +++ b/src/Windows/Perspex.Designer/WinApi.cs @@ -13,5 +13,10 @@ namespace Perspex.Designer public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint); [DllImport("user32.dll")] public static extern bool SetParent(IntPtr hWnd, IntPtr hWndNewParent); + [DllImport("user32.dll", CharSet = CharSet.Auto)] + public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam); + + public const uint WM_CLOSE = 0x0010; + } }