|
|
@ -1,5 +1,7 @@ |
|
|
using System; |
|
|
using System; |
|
|
using System.ComponentModel; |
|
|
using System.ComponentModel; |
|
|
|
|
|
using System.Drawing; |
|
|
|
|
|
using System.Drawing.Drawing2D; |
|
|
using System.Windows.Forms; |
|
|
using System.Windows.Forms; |
|
|
using Avalonia.Controls.Embedding; |
|
|
using Avalonia.Controls.Embedding; |
|
|
using Avalonia.Win32.Interop; |
|
|
using Avalonia.Win32.Interop; |
|
|
@ -49,15 +51,20 @@ public class WinFormsAvaloniaControlHost : WinFormsControl |
|
|
/// <inheritdoc />
|
|
|
/// <inheritdoc />
|
|
|
protected override void OnHandleCreated(EventArgs e) |
|
|
protected override void OnHandleCreated(EventArgs e) |
|
|
{ |
|
|
{ |
|
|
_root = new(); |
|
|
// EmbeddableControlRoot requires Avalonia to be initialized which is not done by the Windows Forms designer.
|
|
|
_root.Content = _content; |
|
|
if (!DesignMode) |
|
|
_root.Prepare(); |
|
|
{ |
|
|
_root.StartRendering(); |
|
|
_root = new(); |
|
|
_root.GotFocus += RootGotFocus; |
|
|
_root.Content = _content; |
|
|
|
|
|
_root.Prepare(); |
|
|
|
|
|
_root.StartRendering(); |
|
|
|
|
|
_root.GotFocus += RootGotFocus; |
|
|
|
|
|
|
|
|
|
|
|
FixPosition(); |
|
|
|
|
|
|
|
|
|
|
|
UnmanagedMethods.SetParent(WindowHandle, Handle); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
FixPosition(); |
|
|
|
|
|
|
|
|
|
|
|
UnmanagedMethods.SetParent(WindowHandle, Handle); |
|
|
|
|
|
base.OnHandleCreated(e); |
|
|
base.OnHandleCreated(e); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -93,14 +100,14 @@ public class WinFormsAvaloniaControlHost : WinFormsControl |
|
|
if (handle != default) |
|
|
if (handle != default) |
|
|
UnmanagedMethods.SetFocus(handle); |
|
|
UnmanagedMethods.SetFocus(handle); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private void FixPosition() |
|
|
private void FixPosition() |
|
|
{ |
|
|
{ |
|
|
var handle = WindowHandle; |
|
|
var handle = WindowHandle; |
|
|
if (handle != default && Width > 0 && Height > 0) |
|
|
if (handle != default && Width > 0 && Height > 0) |
|
|
UnmanagedMethods.MoveWindow(handle, 0, 0, Width, Height, true); |
|
|
UnmanagedMethods.MoveWindow(handle, 0, 0, Width, Height, true); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
/// <inheritdoc />
|
|
|
protected override void OnResize(EventArgs e) |
|
|
protected override void OnResize(EventArgs e) |
|
|
{ |
|
|
{ |
|
|
@ -111,6 +118,21 @@ public class WinFormsAvaloniaControlHost : WinFormsControl |
|
|
/// <inheritdoc />
|
|
|
/// <inheritdoc />
|
|
|
protected override void OnPaint(PaintEventArgs e) |
|
|
protected override void OnPaint(PaintEventArgs e) |
|
|
{ |
|
|
{ |
|
|
|
|
|
if (DesignMode) |
|
|
|
|
|
{ |
|
|
|
|
|
const string message = "Avalonia control is disabled in design mode."; |
|
|
|
|
|
|
|
|
|
|
|
using var pen = new Pen(SystemBrushes.ControlDark); |
|
|
|
|
|
var outline = ClientSize - new SizeF(pen.Width, pen.Width); |
|
|
|
|
|
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; |
|
|
|
|
|
e.Graphics.DrawRectangle(pen, 0, 0, outline.Width, outline.Height); |
|
|
|
|
|
e.Graphics.DrawLine(pen, 0, 0, outline.Width, outline.Height); |
|
|
|
|
|
e.Graphics.DrawLine(pen, 0, outline.Height, outline.Width, 0); |
|
|
|
|
|
|
|
|
|
|
|
var messageSize = e.Graphics.MeasureString(message, Font, ClientSize); |
|
|
|
|
|
var messageLocation = new PointF(ClientSize.Width / 2 - messageSize.Width / 2, ClientSize.Height / 2 - messageSize.Height / 2); |
|
|
|
|
|
var messageArea = new RectangleF(messageLocation, messageSize); |
|
|
|
|
|
e.Graphics.DrawString(message, Font, SystemBrushes.ControlText, messageArea); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|