Software rendering mode - WPF

前端 未结 3 438
野性不改
野性不改 2020-11-28 08:20

I have a WPF user control for which I need to force rendering in RenderMode.SoftwareOnly. Since I am using .NET 3.5, I had to do something like this:

<         


        
3条回答
  •  青春惊慌失措
    2020-11-28 08:51

    Here's what we did:

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        if (ForceSoftwareRendering)
        {
            HwndSource hwndSource = PresentationSource.FromVisual(this) as HwndSource;
            HwndTarget hwndTarget = hwndSource.CompositionTarget;
            hwndTarget.RenderMode = RenderMode.SoftwareOnly;
        }
    }
    

    It worked OK for us, EXCEPT... This needs to be done for every Window. In .NET 3.5 there was no way to make the setting take effect application-wide. And there are some windows that you won't have as much control over - for example, right-click "context" windows. We found that there was no good solution for .NET 3.5 except the registry setting.

    Edited

    Here's the logic we used to determine when to force software rendering. It was suggested by a Microsoft support engineer.

    public bool ForceSoftwareRendering 
    {
        get 
        { 
            int renderingTier = (System.Windows.Media.RenderCapability.Tier >> 16);
            return renderingTier == 0;
        }
    }
    

    In .NET 4 Microsoft added an application-wide setting that works perfectly for us. Its a much better option because you don't need to set it on every window. You just set it once and it applies to all windows.

    System.Windows.Media.RenderOptions.ProcessRenderMode

    Edited

    The new .NET 4.0 property can be set at application startup like this:

    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            if (ForceSoftwareRendering)
                RenderOptions.ProcessRenderMode = RenderMode.SoftwareOnly;
        }
    }
    

提交回复
热议问题