14

I am having a WinForms control, inside that I have a TableLayoutPanel which holds multiple ElementHosts and each ElementHost contains a WPF control.

Everything works fine except when the size of controls is bigger then window and ScrollBar is there; when I scroll down, the controls get rendered distorted, like this -

enter image description here

On maximizing the window or re-sizing it, controls render properly (reducing the size such that controls go out of visible area and then increase the size again to bring them back in visible area)

This doesn't happen with WinForms control in the same window just the WPF ones; any idea why this is happening and any solution for this?

akjoshi
  • 15,374
  • 13
  • 103
  • 121
  • Anyone, anything....let me know if any other detail can be of help in solving this issue! – akjoshi Aug 10 '12 at 12:20
  • Possible duplicate http://stackoverflow.com/questions/2589948/how-to-avoid-visual-artifacts-when-hosting-wpf-user-controls-within-a-winforms-m – jpierson May 23 '13 at 08:46
  • 1
    Note that on Windows 7 this problem does not occur when using a "classic" Windows theme. It seems to happen only when using the "Aero" theme. – Dimitri C. Oct 08 '14 at 10:05
  • @DimitriC. Thanks for info, didn't noticed that in this case but yes I have come across some theme related issues in WPF/WinForms interoperability and they always reminds me of browser incompatibility issues and hacks used their :) – akjoshi Oct 08 '14 at 10:54

3 Answers3

15
this.Loaded += delegate
{
    var source = PresentationSource.FromVisual(this);
    var hwndTarget = source.CompositionTarget as HwndTarget;

    if (hwndTarget != null)
    {
        hwndTarget.RenderMode = RenderMode.SoftwareOnly;
    }
};

Try using that in the wpf control you are hosting. This is a known rendering issue of the the wpf controls that are hosted in win forms. Changing the rendering mode to software only will solve the problem.

akjoshi
  • 15,374
  • 13
  • 103
  • 121
  • Thanks Valentin, Sounds interesting, will try this solution and hope it would work. – akjoshi Sep 28 '12 at 11:31
  • Some info regarding performance impact is mentioned [here](http://blogs.msdn.com/b/jgoldb/archive/2007/10/10/performance-improvements-in-wpf-in-net-3-5-3-0-sp1.aspx) – akjoshi Dec 12 '12 at 12:16
  • Works fine for me. I have splitters and several ElementHosts, the fragmenting and black parts do no longer show after applying this method. – Mike Fuchs Feb 21 '13 at 17:57
  • This worked on my machine but caused some others to crash (http://stackoverflow.com/questions/17473857/what-causes-windows-to-hang-in-this-wpf-ribbon-application and http://stackoverflow.com/questions/7719627/wpf-elementhost-in-winforms-crashes-windows-when-maximized) I still couldn't find a better solution – Eduardo Wada Jun 29 '15 at 20:52
  • Does this mean that we will lose performance while using software rendering and more rely on CPU? – joe Jan 25 '19 at 05:14
  • I had a wpf control inside a winform elementhost that wouldn't draw on ONE of my dual screen setup (But worked fine on the other screen!?). This solved the problem. – Maxter Nov 22 '19 at 14:50
1

I had a similar problem and solved forcing a refresh of the ElmenetHost in the scroll event of the TableLayoutPanel

akjoshi
  • 15,374
  • 13
  • 103
  • 121
MaRuf
  • 1,834
  • 2
  • 19
  • 22
  • Thanks Mackho, will try this and see if it works. BTW did this had any impact on the performance? – akjoshi Aug 16 '12 at 12:10
  • No I didn't notice any significant decrease of performance, but I have to say my form is pretty light so i can't guarantee you anything ;) – MaRuf Aug 16 '12 at 12:31
1

Ok, this is gonna sound like total B.S. but it worked for me: in the Load event of your form, resize the form.

public class MyForm : Form
{
   public MyForm()
   {
      Load += (o, e) => { Width -=1; Width +=1; };
   }
}

After the form has been resized, I could not force a display issue.

Scott Baker
  • 10,013
  • 17
  • 56
  • 102
  • That sounds really interesting, I will try to test this and see if it works in my application. – akjoshi Jul 31 '15 at 18:43
  • I'd like to know why this got a downvote. It actually solved the problem for me – Scott Baker Oct 02 '18 at 15:46
  • Not sure when/who downvoted it, but its too old for anyone to remember. I also don't remember if I ever tried that or not, but upvoting it as its a potential solution for sure. – akjoshi Oct 08 '18 at 09:13
  • Actually, we still use this approach in some of our code. Worth to consider as an option. – Kahuna Jun 23 '23 at 06:16