ElementHost的闪烁在调整大小的黑色边框(ElementHost flickers blac

2019-10-22 09:54发布

免责声明 :这不是一个重复的职位。 我用Google搜索有关的问题。 又看了这个 , 这个和这个做题。 我尝试了一些这些东西,但似乎没有什么帮助。

考虑下面这个简单的示例代码。 这只是一个空的ElementHost的一个WinForm(内部没有WPF控件)内:

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

调整表格 ,你可以看到在表单边境两个黑边:

请¿有人能够给过我的例子一个工作解决方案解决这个问题?

Answer 1:

试试这个(同样的想法,你所提供的第一个链接,但更好的性能):

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


Answer 2:

这个问题是不相关的ElementHost的和WinForms。 这只是一个WPF的问题,我发现下面的SO问题的答案:

如何修复WPF窗体调整大小-控制落后和黑色背景吗?



文章来源: ElementHost flickers black borders on resize