|
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="VisualExtensions.cs" company="Catel development team">
// Copyright (c) 2008 - 2011 Catel development team. All rights reserved.
// </copyright>
// <summary>
// Extensions for the <see cref="Visual" />
// </summary>
// --------------------------------------------------------------------------------------------------------------------
using System;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop;
using System.Windows.Media;
using Catel.Windows.Properties;
using log4net;
namespace Catel.Windows
{
/// <summary>
/// Extensions for the <see cref="Visual"/>
/// </summary>
public static class VisualExtensions
{
#region Variables
/// <summary>
/// The <see cref="ILog">log</see> object.
/// </summary>
private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
private static readonly Delegate _onVisualLoadedDelegate = Delegate.CreateDelegate(typeof(RoutedEventHandler), typeof(VisualExtensions), "OnVisualLoaded");
#endregion
/// <summary>
/// Disables the hardware acceleration for the specified visual.
/// </summary>
/// <param name="visual">The visual.</param>
/// <remarks>
/// When the visual is not yet loaded, this method tries to subscribe to the <see cref="Control.Loaded"/> event so disabled the
/// hardware acceleration as soon as the control is loaded.
/// </remarks>
/// <exception cref="ArgumentNullException">when <paramref name="visual"/> is <c>null</c>.</exception>
public static void DisableHardwareAcceleration(this Visual visual)
{
if (visual == null)
{
throw new ArgumentNullException("visual");
}
Type visualType = visual.GetType();
var hwndSource = PresentationSource.FromVisual(visual) as HwndSource;
if (hwndSource == null)
{
// Visual is now null, try to subscribe to the loaded event
EventInfo loadedEvent = visualType.GetEvent("Loaded");
if (loadedEvent != null)
{
Log.Debug(TraceMessages.HardwareAccelerationCannotBeDisabledYet, visualType);
loadedEvent.AddEventHandler(visual, _onVisualLoadedDelegate);
}
else
{
Log.Warn(TraceMessages.FailedToDisableHardwareAcceleration, visualType);
}
}
else
{
// Set to software only
hwndSource.CompositionTarget.RenderMode = RenderMode.SoftwareOnly;
Log.Debug(TraceMessages.DisabledHardwareAcceleration, visualType);
}
}
/// <summary>
/// Called when [visual loaded].
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
private static void OnVisualLoaded(object sender, RoutedEventArgs e)
{
Visual visual = (Visual)sender;
EventInfo loadedEvent = visual.GetType().GetEvent("Loaded");
loadedEvent.RemoveEventHandler(visual, _onVisualLoadedDelegate);
DisableHardwareAcceleration((Visual) sender);
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.