ElementHostPropertyMap.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / Orcas / NetFXw7 / wpf / src / WinFormsIntegration / System / Windows / Integration / ElementHostPropertyMap.cs / 1 / ElementHostPropertyMap.cs

                            using System.Windows.Forms.Integration; 
using System.Runtime.Serialization;
using SWC = System.Windows.Controls;
using SD = System.Drawing;
using SW = System.Windows; 
using SWM = System.Windows.Media;
using SWF = System.Windows.Forms; 
using SWI = System.Windows.Input; 

namespace System.Windows.Forms.Integration 
{
    internal sealed class ElementHostPropertyMap : PropertyMap
    {
        //Since the host controls our lifetime, we shouldn't be disposing it. 
#pragma warning disable 1634, 1691
#pragma warning disable 56524 
        private ElementHost _host; 
#pragma warning restore 1634, 1691, 56524
        public ElementHostPropertyMap(ElementHost host) 
            : base(host)
        {
            _host = host;
            InitializeDefaultTranslators(); 
            ResetAll();
        } 
 
        /// 
        ///     Initialize the list of things we translate by default, like 
        ///     BackColor.
        /// 
        private void InitializeDefaultTranslators()
        { 
            DefaultTranslators.Add("BackColor", BackgroundPropertyTranslator);
            DefaultTranslators.Add("BackgroundImage", BackgroundPropertyTranslator); 
            DefaultTranslators.Add("BackgroundImageLayout", BackgroundPropertyTranslator); 
            DefaultTranslators.Add("Cursor", CursorPropertyTranslator);
            DefaultTranslators.Add("Enabled", EnabledPropertyTranslator); 
            DefaultTranslators.Add("Font", FontPropertyTranslator);
            DefaultTranslators.Add("RightToLeft", RightToLeftPropertyTranslator);
            DefaultTranslators.Add("Visible", VisiblePropertyTranslator);
            DefaultTranslators.Add("ImeMode", ImeModePropertyTranslator); 
        }
 
        ///  
        ///     Translator for BackColor, BackgroundImage, and BackgroundImageLayout
        ///  
        private static void BackgroundPropertyTranslator(object host, string propertyName, object value)
        {
            ElementHost elementHost = host as ElementHost;
            if (elementHost != null) 
            {
                UpdateBackgroundImage(elementHost); 
            } 
        }
 
        private static void UpdateBackgroundImage(ElementHost host)
        {
            if (host != null && host.HostContainerInternal != null)
            { 
                if (host.BackColorTransparent)
                { 
                    Control parent = host.Parent; 
                    if (parent != null && parent.Visible)
                    { 
                        using (SD.Bitmap parentBitmap = HostUtils.GetCoveredPortionOfBitmap(parent, host))
                        {
                            host.HostContainerInternal.Background = new SWM.ImageBrush(Convert.ToSystemWindowsMediaImagingBitmapImage(parentBitmap));
                        } 
                    }
                } 
                else 
                {
                    using (SD.Bitmap elementHostBitmap = HostUtils.GetBitmapOfControl(host, host)) 
                    {
                        host.HostContainerInternal.Background = new SWM.ImageBrush(Convert.ToSystemWindowsMediaImagingBitmapImage(elementHostBitmap));
                    }
                } 
            }
        } 
 
        /// 
        ///     Translator for Cursor 
        /// 
        private static void CursorPropertyTranslator(object host, string propertyName, object value)
        {
            ElementHost elementHost = host as ElementHost; 
            if (elementHost != null)
            { 
                AvalonAdapter adapter = elementHost.HostContainerInternal; 
                if (adapter != null)
                { 
                    //Note: Allow nulls to propagate
                    SWF.Cursor fromCursor = value as SWF.Cursor;
                    SWI.Cursor toCursor = Convert.ToSystemWindowsInputCursor(fromCursor);
                    adapter.Cursor = toCursor; 
                }
            } 
        } 

        ///  
        ///     Translator for Enabled
        /// 
        private static void EnabledPropertyTranslator(object host, string propertyName, object value)
        { 
            ElementHost elementHost = host as ElementHost;
            if (elementHost != null) 
            { 
                AvalonAdapter adapter = elementHost.HostContainerInternal;
                if (adapter != null && value is bool) 
                {
                    adapter.IsEnabled = (bool)value;
                }
            } 
        }
 
        ///  
        ///     Translator for Font
        ///  
        private static void FontPropertyTranslator(object host, string propertyName, object value)
        {
            ElementHost elementHost = host as ElementHost;
            SD.Font wfFont = value as SD.Font; 

            if (elementHost != null && wfFont != null) 
            { 
                AvalonAdapter adapter = elementHost.HostContainerInternal;
                if (adapter != null) 
                {
                    adapter.SetValue(SWC.Control.FontSizeProperty, Convert.SystemDrawingFontToSystemWindowsFontSize(wfFont));
                    adapter.SetValue(SWC.Control.FontFamilyProperty, Convert.ToSystemWindowsFontFamily(wfFont.FontFamily));
                    adapter.SetValue(SWC.Control.FontWeightProperty, Convert.ToSystemWindowsFontWeight(wfFont)); 
                    adapter.SetValue(SWC.Control.FontStyleProperty, Convert.ToSystemWindowsFontStyle(wfFont));
 
                    SWC.TextBlock childTextBlock = elementHost.Child as SWC.TextBlock; 
                    if (childTextBlock != null)
                    { 
                        TextDecorationCollection decorations = new TextDecorationCollection();
                        if (wfFont.Underline) { decorations.Add(TextDecorations.Underline); };
                        if (wfFont.Strikeout) { decorations.Add(TextDecorations.Strikethrough); }
                        childTextBlock.TextDecorations = decorations; 
                    }
                } 
            } 
        }
 
        /// 
        ///     Translator for ImeMode
        /// 
        private static void ImeModePropertyTranslator(object host, string propertyName, object value) 
        {
            ElementHost elementHost = host as ElementHost; 
            if (elementHost != null && elementHost.HwndSource != null) 
            {
                elementHost.SyncHwndSrcImeStatus(); 
            }
        }

        ///  
        ///     Translator for RightToLeft
        ///  
        private static void RightToLeftPropertyTranslator(object host, string propertyName, object value) 
        {
            ElementHost elementHost = host as ElementHost; 
            if (elementHost != null)
            {
                AvalonAdapter adapter = elementHost.HostContainerInternal;
                if (adapter != null && value is SWF.RightToLeft) 
                {
                    SWF.RightToLeft fromRTL = (SWF.RightToLeft)value; 
                    SW.FlowDirection toFlowDirection = ((fromRTL == SWF.RightToLeft.Yes) ? SW.FlowDirection.RightToLeft : SW.FlowDirection.LeftToRight); 
                    adapter.FlowDirection = toFlowDirection;
                } 
            }
        }

        ///  
        ///     Translator for Visible
        ///  
        private static void VisiblePropertyTranslator(object host, string propertyName, object value) 
        {
            ElementHost elementHost = host as ElementHost; 
            if (elementHost != null)
            {
                AvalonAdapter adapter = elementHost.HostContainerInternal;
                if (value is bool && adapter != null) 
                {
                    bool fromVisible = (bool)value; 
                    SW.Visibility toVisibility = ((fromVisible) ? SW.Visibility.Visible : SW.Visibility.Hidden); 
                    adapter.Visibility = toVisibility;
                } 
            }
        }
    }
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
using System.Windows.Forms.Integration; 
using System.Runtime.Serialization;
using SWC = System.Windows.Controls;
using SD = System.Drawing;
using SW = System.Windows; 
using SWM = System.Windows.Media;
using SWF = System.Windows.Forms; 
using SWI = System.Windows.Input; 

namespace System.Windows.Forms.Integration 
{
    internal sealed class ElementHostPropertyMap : PropertyMap
    {
        //Since the host controls our lifetime, we shouldn't be disposing it. 
#pragma warning disable 1634, 1691
#pragma warning disable 56524 
        private ElementHost _host; 
#pragma warning restore 1634, 1691, 56524
        public ElementHostPropertyMap(ElementHost host) 
            : base(host)
        {
            _host = host;
            InitializeDefaultTranslators(); 
            ResetAll();
        } 
 
        /// 
        ///     Initialize the list of things we translate by default, like 
        ///     BackColor.
        /// 
        private void InitializeDefaultTranslators()
        { 
            DefaultTranslators.Add("BackColor", BackgroundPropertyTranslator);
            DefaultTranslators.Add("BackgroundImage", BackgroundPropertyTranslator); 
            DefaultTranslators.Add("BackgroundImageLayout", BackgroundPropertyTranslator); 
            DefaultTranslators.Add("Cursor", CursorPropertyTranslator);
            DefaultTranslators.Add("Enabled", EnabledPropertyTranslator); 
            DefaultTranslators.Add("Font", FontPropertyTranslator);
            DefaultTranslators.Add("RightToLeft", RightToLeftPropertyTranslator);
            DefaultTranslators.Add("Visible", VisiblePropertyTranslator);
            DefaultTranslators.Add("ImeMode", ImeModePropertyTranslator); 
        }
 
        ///  
        ///     Translator for BackColor, BackgroundImage, and BackgroundImageLayout
        ///  
        private static void BackgroundPropertyTranslator(object host, string propertyName, object value)
        {
            ElementHost elementHost = host as ElementHost;
            if (elementHost != null) 
            {
                UpdateBackgroundImage(elementHost); 
            } 
        }
 
        private static void UpdateBackgroundImage(ElementHost host)
        {
            if (host != null && host.HostContainerInternal != null)
            { 
                if (host.BackColorTransparent)
                { 
                    Control parent = host.Parent; 
                    if (parent != null && parent.Visible)
                    { 
                        using (SD.Bitmap parentBitmap = HostUtils.GetCoveredPortionOfBitmap(parent, host))
                        {
                            host.HostContainerInternal.Background = new SWM.ImageBrush(Convert.ToSystemWindowsMediaImagingBitmapImage(parentBitmap));
                        } 
                    }
                } 
                else 
                {
                    using (SD.Bitmap elementHostBitmap = HostUtils.GetBitmapOfControl(host, host)) 
                    {
                        host.HostContainerInternal.Background = new SWM.ImageBrush(Convert.ToSystemWindowsMediaImagingBitmapImage(elementHostBitmap));
                    }
                } 
            }
        } 
 
        /// 
        ///     Translator for Cursor 
        /// 
        private static void CursorPropertyTranslator(object host, string propertyName, object value)
        {
            ElementHost elementHost = host as ElementHost; 
            if (elementHost != null)
            { 
                AvalonAdapter adapter = elementHost.HostContainerInternal; 
                if (adapter != null)
                { 
                    //Note: Allow nulls to propagate
                    SWF.Cursor fromCursor = value as SWF.Cursor;
                    SWI.Cursor toCursor = Convert.ToSystemWindowsInputCursor(fromCursor);
                    adapter.Cursor = toCursor; 
                }
            } 
        } 

        ///  
        ///     Translator for Enabled
        /// 
        private static void EnabledPropertyTranslator(object host, string propertyName, object value)
        { 
            ElementHost elementHost = host as ElementHost;
            if (elementHost != null) 
            { 
                AvalonAdapter adapter = elementHost.HostContainerInternal;
                if (adapter != null && value is bool) 
                {
                    adapter.IsEnabled = (bool)value;
                }
            } 
        }
 
        ///  
        ///     Translator for Font
        ///  
        private static void FontPropertyTranslator(object host, string propertyName, object value)
        {
            ElementHost elementHost = host as ElementHost;
            SD.Font wfFont = value as SD.Font; 

            if (elementHost != null && wfFont != null) 
            { 
                AvalonAdapter adapter = elementHost.HostContainerInternal;
                if (adapter != null) 
                {
                    adapter.SetValue(SWC.Control.FontSizeProperty, Convert.SystemDrawingFontToSystemWindowsFontSize(wfFont));
                    adapter.SetValue(SWC.Control.FontFamilyProperty, Convert.ToSystemWindowsFontFamily(wfFont.FontFamily));
                    adapter.SetValue(SWC.Control.FontWeightProperty, Convert.ToSystemWindowsFontWeight(wfFont)); 
                    adapter.SetValue(SWC.Control.FontStyleProperty, Convert.ToSystemWindowsFontStyle(wfFont));
 
                    SWC.TextBlock childTextBlock = elementHost.Child as SWC.TextBlock; 
                    if (childTextBlock != null)
                    { 
                        TextDecorationCollection decorations = new TextDecorationCollection();
                        if (wfFont.Underline) { decorations.Add(TextDecorations.Underline); };
                        if (wfFont.Strikeout) { decorations.Add(TextDecorations.Strikethrough); }
                        childTextBlock.TextDecorations = decorations; 
                    }
                } 
            } 
        }
 
        /// 
        ///     Translator for ImeMode
        /// 
        private static void ImeModePropertyTranslator(object host, string propertyName, object value) 
        {
            ElementHost elementHost = host as ElementHost; 
            if (elementHost != null && elementHost.HwndSource != null) 
            {
                elementHost.SyncHwndSrcImeStatus(); 
            }
        }

        ///  
        ///     Translator for RightToLeft
        ///  
        private static void RightToLeftPropertyTranslator(object host, string propertyName, object value) 
        {
            ElementHost elementHost = host as ElementHost; 
            if (elementHost != null)
            {
                AvalonAdapter adapter = elementHost.HostContainerInternal;
                if (adapter != null && value is SWF.RightToLeft) 
                {
                    SWF.RightToLeft fromRTL = (SWF.RightToLeft)value; 
                    SW.FlowDirection toFlowDirection = ((fromRTL == SWF.RightToLeft.Yes) ? SW.FlowDirection.RightToLeft : SW.FlowDirection.LeftToRight); 
                    adapter.FlowDirection = toFlowDirection;
                } 
            }
        }

        ///  
        ///     Translator for Visible
        ///  
        private static void VisiblePropertyTranslator(object host, string propertyName, object value) 
        {
            ElementHost elementHost = host as ElementHost; 
            if (elementHost != null)
            {
                AvalonAdapter adapter = elementHost.HostContainerInternal;
                if (value is bool && adapter != null) 
                {
                    bool fromVisible = (bool)value; 
                    SW.Visibility toVisibility = ((fromVisible) ? SW.Visibility.Visible : SW.Visibility.Hidden); 
                    adapter.Visibility = toVisibility;
                } 
            }
        }
    }
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.

                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK