Repository: AvaloniaUI/Avalonia
File: src/Windows/Avalonia.Win32.Interoperability/WinForms/WinFormsAvaloniaControlHost.cs (C#)

Code before:
    }

    /// <inheritdoc />
    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);
        }
    }

    public bool PreFilterMessage(ref Message m)
    {
        if (_root?.PlatformImpl is WindowImpl impl && !impl.IsOurWindow(m.HWnd))
        {
            var message = (UnmanagedMethods.WindowsMessage)m.Msg;

            switch (message)
            {
                case UnmanagedMethods.WindowsMessage.WM_LBUTTONDOWN:
                case UnmanagedMethods.WindowsMessage.WM_MBUTTONDOWN:
                case UnmanagedMethods.WindowsMessage.WM_RBUTTONDOWN:
                case UnmanagedMethods.WindowsMessage.WM_NCLBUTTONDOWN:
                case UnmanagedMethods.WindowsMessage.WM_NCMBUTTONDOWN:
                case UnmanagedMethods.WindowsMessage.WM_NCRBUTTONDOWN:
                    impl.Deactivated?.Invoke();
                    break;
            }
        }

        return false;
    }
}


Review comment (performance):
For performance, check the message first. If we're not interested in it, you can return immediately, avoiding all the further checks. `IsOurWindow` takes a lock and iterates through all windows (granted, we aren't expecting many of those).

Code after:
    }

    /// <inheritdoc />
    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);
        }
    }

    public bool PreFilterMessage(ref Message m)
    {
        var message = (UnmanagedMethods.WindowsMessage)m.Msg;

        switch (message)
        {
            case UnmanagedMethods.WindowsMessage.WM_LBUTTONDOWN:
            case UnmanagedMethods.WindowsMessage.WM_MBUTTONDOWN:
            case UnmanagedMethods.WindowsMessage.WM_RBUTTONDOWN:
            case UnmanagedMethods.WindowsMessage.WM_NCLBUTTONDOWN:
            case UnmanagedMethods.WindowsMessage.WM_NCMBUTTONDOWN:
            case UnmanagedMethods.WindowsMessage.WM_NCRBUTTONDOWN:
                if (_root?.PlatformImpl is WindowImpl impl && !impl.IsOurWindow(m.HWnd))
                {
                    impl.Deactivated?.Invoke();
                }
                break;
        }

        return false;
    }
}