Black background before loading a wpf controll when using ElementHost
Asked Answered
N

3

1

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?

Newspaper answered 18/1, 2010 at 16:12 Comment(0)
S
7

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

Soapberry answered 18/1, 2010 at 16:15 Comment(2)
Yeah that would do it. I had to hide the ElementHost, though. If I just sat the wpf element to Visibility=Hidden, I still got the flash. But thanks for pointing me in the right direction.Newspaper
At which point exactly is the WinForms control considered "fully loaded"?Speed
F
1

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));
        }
    }
Flanigan answered 15/3, 2019 at 15:34 Comment(0)
C
0

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();
Coper answered 23/2, 2010 at 19:39 Comment(1)
Could you elaborate on this? I'm having a hard time figuring out the context on this...Corrosive

© 2022 - 2024 — McMap. All rights reserved.