简体   繁体   English

在调整大小时,ElementHost会闪烁黑色边框

[英]ElementHost flickers black borders on resize

Disclaimer : This is not a duplicated post. 免责声明 :这不是重复的帖子。 I googled about the issue. 我用Google搜索了这个问题。 Also read this , this and this SO questions. 还读这个这个这个 SO问题。 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): 它只是WinForm中的一个空ElementHost(内部没有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);
        }
    }
}

When you resize the form , you can see two black edges at the form border: 调整窗体大小时 ,您可以在窗体边框处看到两个黑色边缘: 在此输入图像描述

Please, ¿someone could give a working solution over my example to fix this issue? 请,¿有人可以为我的例子提供一个有效的解决方案来解决这个问题吗?

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

The issue is not related to ElementHost and Winforms. 该问题与ElementHost和Winforms无关。 It's just a WPF issue, and I found the answer in the following SO question: 这只是一个WPF问题,我在以下SO问题中找到了答案:

How to fix the WPF form resize - controls lagging behind and black background? 如何修复WPF表单调整大小 - 控制滞后和黑色背景?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM