ElementHost flickers black borders on resize

1.5k views Asked by At

Disclaimer: This is not a duplicated post. I googled about the issue. Also read this, this and this SO questions. I tried some of those things but nothing seemed to help.

Consider the following simple example code. It's just an empty ElementHost inside a WinForm (no WPF control inside):

using System.Windows.Forms;
using System.Windows.Forms.Integration;

namespace WindowsFormsApplication15
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            ElementHost host = new ElementHost();
            host.Dock = DockStyle.Fill;
            this.Controls.Add(host);
        }
    }
}

When you resize the form, you can see two black edges at the form border: enter image description here

Please, ¿someone could give a working solution over my example to fix this issue?

2

There are 2 answers

0
Daniel Peñalba On BEST ANSWER

The issue is not related to ElementHost and Winforms. It's just a WPF issue, and I found the answer in the following SO question:

How to fix the WPF form resize - controls lagging behind and black background?

8
Loathing On

Try this (same idea as the first link you provided, but better performance):

    public class ElementHost2 : ElementHost {

        public ElementHost2() {
            this.AutoSize = true;
        }

        public override Size GetPreferredSize(Size proposedSize) {
            Form f = this.FindForm();
            Size s = f.ClientSize;
            return s;
        }

        private const uint WM_SETREDRAW = 0xB;

        [DllImport("user32.dll")]
        private static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);

        [DllImport("user32.dll")]
        static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

        private const uint NOSIZE = 0x0001;
        private const uint NOMOVE = 0x0002;
        private const uint NOZORDER = 0x0004;
        private const uint NOREDRAW = 0x0008;
        private const uint NOACTIVATE = 0x0010;
        private const uint DRAWFRAME = 0x0020;
        private const uint FRAMECHANGED = 0x0020;
        private const uint SHOWWINDOW = 0x0040;
        private const uint HIDEWINDOW = 0x0080;
        private const uint NOCOPYBITS = 0x0100;
        private const uint NOOWNERZORDER = 0x0200;
        private const uint NOREPOSITION = 0x0200;
        private const uint NOSENDCHANGING = 0x0400;
        private const uint DEFERERASE = 0x2000;
        private const uint ASYNCWINDOWPOS = 0x4000;

        protected override void OnResize(EventArgs e) {
            base.OnResize(e);
            SendMessage(this.Handle, WM_SETREDRAW, 0, 0);
            SendMessage(this.Handle, WM_SETREDRAW, 1, 0);
            // forces window to redraw:
            SetWindowPos(this.Handle, IntPtr.Zero, 0, 0, 0, 0, NOSIZE | NOMOVE| NOZORDER | NOACTIVATE | SHOWWINDOW);
        }

        // better performance?
        protected override void OnPaintBackground(PaintEventArgs pevent) {
            //base.OnPaintBackground(pevent);
        }

        protected override void OnPaint(PaintEventArgs e) {
            //base.OnPaint(e);
        }
    }

    class Form2 : Form {
        ElementHost host = new ElementHost2();
        public Form2() {
            Controls.Add(host);
            this.BackColor = Color.Red;
            var p = new System.Windows.Controls.DockPanel();
            p.Background = System.Windows.Media.Brushes.Red;
            host.Child = p;
            p.Children.Add(new System.Windows.Controls.TextBox { Width = 100, Height = 20 });
        }
    }