none
elementHost repaint problem in MDI application

    Question

  •  

    Hi

    I am having a refresh\repaint problem with the ElementHost control. I have created a Windows Forms MDI application and inserted ElementHost on a child form with a very simple XAML.  When I move a child form out of MDI parent working area, and then use MDI parent scroll bars to bring it back, ElementHost  located in a child form does not repaint itself and brings back visual garbage!

    Here are simple steps to reproduce my problem:

    1). Create Windows Forms project and add a MDIParent form.

    2).  Add User Control (WPF) named UserControl1.xaml , leave it as is just with <Grid></Grid> content.

     

    3). Add Windows Form frm_Child,  add ElementHost with Dock.Fill and specify UserControl1.xaml as a child;

     

    4). Use the following code in MDIParent

     

                      frm_Child childForm = new frm_Child();

                      childForm.MdiParent = this;

                      childForm.Text = "Window " + childFormNumber++;

                      childForm.Show();

     

    Now,  run application and, using pre-generated MDIParent toolbar, create several child forms, move one out of the MDI working area, and use MDIParent  scrollbars to bring it back. All ElementHosts on all child forms will become crazy, filled with parts and pieces of a visual garbage.

    What  am I missing?  How can I made ElementHosts repaint correctly?

    Friday, December 19, 2008 3:28 AM

Answers

  •  

    Hi Marco,

    Thank s for your reply, it definitely clears the matter. The code you’ve send works well in a test project. The application I am developing is a business application and switching off hardware acceleration seems to be an option.

    However, I am currently resolved the problem just by disabling MDI parent scrollbars with the technique described by Jacob Slusser in http://www.codeproject.com/KB/dialog/mdiclientcontroller.aspx

    • Marked as answer by Uriah65 Tuesday, January 06, 2009 3:33 PM
    • Unmarked as answer by Uriah65 Tuesday, January 06, 2009 3:33 PM
    • Marked as answer by Uriah65 Tuesday, January 06, 2009 3:33 PM
    • Unmarked as answer by Uriah65 Tuesday, January 06, 2009 3:33 PM
    • Marked as answer by Uriah65 Tuesday, January 06, 2009 3:33 PM
    Tuesday, January 06, 2009 3:32 PM

All replies

  • -> What am I missing? How can I made ElementHosts repaint correctly?

    Could you please send your test project to me at v-mazho at microsoft dot com for repro?

     

    Thanks



    Another Paradigm Shift
    http://shevaspace.blogspot.com
    Monday, December 22, 2008 8:34 AM
  • Marco,

    thank you for your reply. I have send the project as suggested.

    Monday, December 22, 2008 1:27 PM
  • Sorry for the late reply, unfortunately, this is a known issue if the WPF content is hosted inside a  hwnd with scrolling enabled, the DWM cannot properly handle this type of scenario, in order to workaround this, you could try disabling hardware accelaration, so that the WPF content will be presented to the DWM redirected surface using GDI code path, you could disable hardware acceleration using the following method:

     

    public partial class UserControl1 : UserControl

    {

        public UserControl1()

        {

            InitializeComponent();

            this.Loaded += delegate

            {

                var source = PresentationSource.FromVisual(this);

                var hwndTarget = source.CompositionTarget as HwndTarget;

                if (hwndTarget != null)

                {

                    hwndTarget.RenderMode = RenderMode.SoftwareOnly;

                }

            };

        }

    }

     

    Software rendering will be slower, but in simple rendering case, you might be sufficient for your scenario.

    Let me know if disabling hardware acceleration is an option for you.


    Marco


    Another Paradigm Shift
    http://shevaspace.blogspot.com
    • Marked as answer by Uriah65 Tuesday, January 06, 2009 3:33 PM
    • Unmarked as answer by Uriah65 Tuesday, January 06, 2009 3:33 PM
    • Proposed as answer by miaodadao Monday, January 28, 2013 9:33 AM
    Monday, January 05, 2009 8:18 AM
  •  

    Hi Marco,

    Thank s for your reply, it definitely clears the matter. The code you’ve send works well in a test project. The application I am developing is a business application and switching off hardware acceleration seems to be an option.

    However, I am currently resolved the problem just by disabling MDI parent scrollbars with the technique described by Jacob Slusser in http://www.codeproject.com/KB/dialog/mdiclientcontroller.aspx

    • Marked as answer by Uriah65 Tuesday, January 06, 2009 3:33 PM
    • Unmarked as answer by Uriah65 Tuesday, January 06, 2009 3:33 PM
    • Marked as answer by Uriah65 Tuesday, January 06, 2009 3:33 PM
    • Unmarked as answer by Uriah65 Tuesday, January 06, 2009 3:33 PM
    • Marked as answer by Uriah65 Tuesday, January 06, 2009 3:33 PM
    Tuesday, January 06, 2009 3:32 PM
  • I've noticed the same problem and posted the issue on StackOverflow. In my situation I found another workaround which is to call the Refresh method on the parent form whenever a child form moves. This seems to correct both issue of artifacts introduced by allowing the child form to be clipped by the MDI parent with scrolling turned on and also fixed the issue where portions of a background child forms content is painted over the child form that I've dragging.

    How to avoid visual artifacts when hosting WPF user controls within a WinForms MDI app?

     

    Wednesday, April 07, 2010 4:12 PM
  • Is this fixed in .Net 4.0?
    Thursday, April 22, 2010 11:00 AM
  • I tried on a Win7 x64 system with a DirectX11 compatible video card using .NET 4.0 and still see the paint problem.
    Tuesday, May 11, 2010 10:13 PM