1

免责声明:这不是重复的帖子。我用谷歌搜索了这个问题。另请阅读SO 问题。我尝试了其中一些东西,但似乎没有任何帮助。

考虑以下简单的示例代码。它只是 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);
        }
    }
}

调整表单大小时,可以在表单边框处看到两条黑边: 在此处输入图像描述

请,¿有人可以通过我的示例给出一个可行的解决方案来解决这个问题吗?

4

2 回答 2

2

试试这个(与您提供的第一个链接相同,但性能更好):

    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 });
        }
    }
于 2015-03-26T13:43:23.113 回答
1

该问题与 ElementHost 和 Winforms 无关。这只是一个 WPF 问题,我在以下 SO 问题中找到了答案:

如何修复 WPF 表单调整大小 - 控件滞后和黑色背景?

于 2016-06-08T14:28:00.910 回答