使用 ElementHost 时加载 wpf 控件之前的黑色背景

发布于 2024-08-18 02:43:31 字数 101 浏览 2 评论 0原文

我在 WinForms 中使用 WPF 和 ElementHost。当表单加载时,ElementHost 即将加载时会出现黑色背景闪烁。这看起来有点糟糕。关于如何摆脱这个问题有什么建议吗?

I'm using WPF in WinForms with ElementHost. When the form loads, there is a flash of black background where the ElementHost is about to load. This looks kind of bad. Any suggestions on how to get rid of this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

征棹 2024-08-25 02:43:31

隐藏元素(Visibility = Hidden)直到 WinForms 控件完全加载...

Hide the element (Visibility = Hidden) until the WinForms control is fully loaded...

老街孤人 2024-08-25 02:43:31

我知道这个问题已经得到了回答,而且这个问题已经很老了,但是所提供的答案都不适合我自己,而且经过很长时间的问题排查后。我终于找到了一个更简单的答案。

如果您在初始构造函数中构建一个从 Element Host 扩展的类。您可以为主机容器设置加载事件。宿主容器是元素宿主子级显示在其顶部的面板。从那里,只需将主机容器背景颜色设置为元素主机父对象背景颜色。

像这样

    using System.Windows;
    using System.Windows.Forms;
    using System.Windows.Media;
    public class MyElementHost : ElementHost
    {
       public MyElementHost()
        {
            this.HostContainer.Loaded += new RoutedEventHandler(HostPanelLoad);
        }

        public void HostPanelLoad(object sender, RoutedEventArgs e)
        {
            System.Drawing.Color parentColor = this.Parent.BackColor;
            this.HostContainer.Background = new SolidColorBrush(Color.FromArgb(parentColor.A, parentColor.R, parentColor.G, parentColor.B));
        }
    }

I know this has already been answered and the question is old but none of the presented answers worked for myself and after a long time of troubleshooting the issue. I finally found an easier answer.

If you build a class extending from Element Host and in the initial constructor. You can set a Load Event for the Host Container. The Host Container is the panel that the Element Hosts Child is being displayed on top of. From there, just set the Host Containers background color to being of the Element Hosts Parents background color.

Like this

    using System.Windows;
    using System.Windows.Forms;
    using System.Windows.Media;
    public class MyElementHost : ElementHost
    {
       public MyElementHost()
        {
            this.HostContainer.Loaded += new RoutedEventHandler(HostPanelLoad);
        }

        public void HostPanelLoad(object sender, RoutedEventArgs e)
        {
            System.Drawing.Color parentColor = this.Parent.BackColor;
            this.HostContainer.Background = new SolidColorBrush(Color.FromArgb(parentColor.A, parentColor.R, parentColor.G, parentColor.B));
        }
    }
握住你手 2024-08-25 02:43:31

您需要首先显示带有空边界的控件,以避免黑色闪烁

if (!_control.Created && _control.BackColor != Color.Transparent)
{
    _control.Bounds = Rectangle.Empty;
    _control.Show();
}

// set control bounds and show it
Rectangle bounds = GetBounds(context, rect);
if (_control.Bounds != bounds)
    _control.Bounds = bounds;
if (!_control.Visible)
    _control.Show();

you need first show control with empty bounds first time to avoid black flickering

if (!_control.Created && _control.BackColor != Color.Transparent)
{
    _control.Bounds = Rectangle.Empty;
    _control.Show();
}

// set control bounds and show it
Rectangle bounds = GetBounds(context, rect);
if (_control.Bounds != bounds)
    _control.Bounds = bounds;
if (!_control.Visible)
    _control.Show();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文