RadioButtonRenderer.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / WinForms / Managed / System / WinForms / RadioButtonRenderer.cs / 1305376 / RadioButtonRenderer.cs

                            //------------------------------------------------------------------------------ 
// 
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// 
//----------------------------------------------------------------------------- 
namespace System.Windows.Forms {
 
using System; 
using System.Drawing;
using System.Diagnostics.CodeAnalysis; 
using System.Windows.Forms.Internal;
using System.Windows.Forms.VisualStyles;
using Microsoft.Win32;
 
    /// 
    ///  
    ///     
    ///       This is a rendering class for the RadioButton control. It works downlevel too (obviously
    ///       without visual styles applied.) 
    ///    
    /// 
    public sealed class RadioButtonRenderer {
 
       //Make this per-thread, so that different threads can safely use these methods.
       [ThreadStatic] 
       private static VisualStyleRenderer visualStyleRenderer = null; 
       private static readonly VisualStyleElement RadioElement = VisualStyleElement.Button.RadioButton.UncheckedNormal;
        private static bool renderMatchingApplicationState = true; 

       //cannot instantiate
       private RadioButtonRenderer() {
       } 

       ///  
       ///  
       ///    
       ///      If this property is true, then the renderer will use the setting from Application.RenderWithVisualStyles to 
       /// determine how to render.
       ///      If this property is false, the renderer will always render with visualstyles.
       ///    
       ///  
       public static bool RenderMatchingApplicationState {
           get { 
               return renderMatchingApplicationState; 
           }
           set { 
               renderMatchingApplicationState = value;
           }
       }
 
       private static bool RenderWithVisualStyles {
           get { 
               return (!renderMatchingApplicationState || Application.RenderWithVisualStyles); 
           }
       } 

       /// 
       /// 
       ///     
       ///       Returns true if the background corresponding to the given state is partially transparent, else false.
       ///     
       ///  
       public static bool IsBackgroundPartiallyTransparent(RadioButtonState state) {
           if (RenderWithVisualStyles) { 
               InitializeRenderer((int)state);

               return visualStyleRenderer.IsBackgroundPartiallyTransparent();
           } 
           else {
               return false; //for downlevel, this is false 
           } 
       }
 
       /// 
       /// 
       ///    
       ///       This is just a convenience wrapper for VisualStyleRenderer.DrawThemeParentBackground. For downlevel, 
       ///       this isn't required and does nothing.
       ///     
       ///  
       [
           SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters") // Using Graphics instead of IDeviceContext intentionally 
       ]
       public static void DrawParentBackground(Graphics g, Rectangle bounds, Control childControl) {
           if (RenderWithVisualStyles) {
               InitializeRenderer(0); 

               visualStyleRenderer.DrawParentBackground(g, bounds, childControl); 
           } 
       }
 
       /// 
       /// 
       ///    
       ///       Renders a RadioButton control. 
       ///    
       ///  
       public static void DrawRadioButton(Graphics g, Point glyphLocation, RadioButtonState state) { 
           Rectangle glyphBounds = new Rectangle(glyphLocation, GetGlyphSize(g, state));
 
           if (RenderWithVisualStyles) {
               InitializeRenderer((int)state);

               visualStyleRenderer.DrawBackground(g, glyphBounds); 
           }
           else { 
               ControlPaint.DrawRadioButton(g, glyphBounds, ConvertToButtonState(state)); 
           }
       } 

       /// 
       /// 
       ///     
       ///       Renders a RadioButton control.
       ///     
       ///  
       public static void DrawRadioButton(Graphics g, Point glyphLocation, Rectangle textBounds, string radioButtonText, Font font, bool focused, RadioButtonState state) {
           DrawRadioButton(g, glyphLocation, textBounds, radioButtonText, font, 
                      TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.SingleLine,
                      focused, state);
       }
 

 
       ///  
       /// 
       ///     
       ///       Renders a RadioButton control.
       ///    
       /// 
       public static void DrawRadioButton(Graphics g, Point glyphLocation, Rectangle textBounds, string radioButtonText, Font font, TextFormatFlags flags, bool focused, RadioButtonState state) { 
           Rectangle glyphBounds = new Rectangle(glyphLocation, GetGlyphSize(g, state));
           Color textColor; 
 
           if (RenderWithVisualStyles) {
               InitializeRenderer((int)state); 

               visualStyleRenderer.DrawBackground(g, glyphBounds);
               textColor = visualStyleRenderer.GetColor(ColorProperty.TextColor);
           } 
           else {
               ControlPaint.DrawRadioButton(g, glyphBounds, ConvertToButtonState(state)); 
               textColor = SystemColors.ControlText; 
           }
 
           TextRenderer.DrawText(g, radioButtonText, font, textBounds, textColor, flags);

           if (focused) {
               ControlPaint.DrawFocusRectangle(g, textBounds); 
           }
       } 
 
       /// 
       ///  
       ///    
       ///       Renders a RadioButton control.
       ///    
       ///  
       public static void DrawRadioButton(Graphics g, Point glyphLocation, Rectangle textBounds, string radioButtonText, Font font, Image image, Rectangle imageBounds, bool focused, RadioButtonState state) {
           DrawRadioButton(g, glyphLocation, textBounds, radioButtonText, font, 
                      TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.SingleLine, 
                      image, imageBounds, focused, state);
       } 



       ///  
       /// 
       ///     
       ///       Renders a RadioButton control. 
       ///    
       ///  
       public static void DrawRadioButton(Graphics g, Point glyphLocation, Rectangle textBounds, string radioButtonText, Font font, TextFormatFlags flags, Image image, Rectangle imageBounds, bool focused, RadioButtonState state) {
           Rectangle glyphBounds = new Rectangle(glyphLocation, GetGlyphSize(g, state));
           Color textColor;
 
           if (RenderWithVisualStyles) {
               InitializeRenderer((int)state); 
 
               //Keep this drawing order! It matches default drawing order.
               visualStyleRenderer.DrawImage(g, imageBounds, image); 
               visualStyleRenderer.DrawBackground(g, glyphBounds);
               textColor = visualStyleRenderer.GetColor(ColorProperty.TextColor);
           }
           else { 
               g.DrawImage(image, imageBounds);
               ControlPaint.DrawRadioButton(g, glyphBounds, ConvertToButtonState(state)); 
               textColor = SystemColors.ControlText; 
           }
 
           TextRenderer.DrawText(g, radioButtonText, font, textBounds, textColor, flags);

           if (focused) {
               ControlPaint.DrawFocusRectangle(g, textBounds); 
           }
       } 
 
       /// 
       ///  
       ///    
       ///       Returns the size of the RadioButton glyph.
       ///    
       ///  
       [
           SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters") // Using Graphics instead of IDeviceContext intentionally 
       ] 
       public static Size GetGlyphSize(Graphics g, RadioButtonState state) {
           if (RenderWithVisualStyles) { 
               InitializeRenderer((int)state);

               return visualStyleRenderer.GetPartSize(g, ThemeSizeType.Draw);
           } 

           return new Size(13, 13); 
       } 

       internal static ButtonState ConvertToButtonState(RadioButtonState state) { 
           switch (state) {
           case RadioButtonState.CheckedNormal:
           case RadioButtonState.CheckedHot:
               return ButtonState.Checked; 
           case RadioButtonState.CheckedPressed:
               return (ButtonState.Checked | ButtonState.Pushed); 
           case RadioButtonState.CheckedDisabled: 
               return (ButtonState.Checked | ButtonState.Inactive);
 
           case RadioButtonState.UncheckedPressed:
               return ButtonState.Pushed;
           case RadioButtonState.UncheckedDisabled:
               return ButtonState.Inactive; 

           default: 
               return ButtonState.Normal; 
           }
       } 

       internal static RadioButtonState ConvertFromButtonState(ButtonState state, bool isHot) {
           if ((state & ButtonState.Checked) == ButtonState.Checked) {
               if ((state & ButtonState.Pushed) == ButtonState.Pushed) { 
                   return RadioButtonState.CheckedPressed;
               } 
               else if ((state & ButtonState.Inactive) == ButtonState.Inactive) { 
                   return RadioButtonState.CheckedDisabled;
               } 
               else if (isHot) {
                   return RadioButtonState.CheckedHot;
               }
 
               return RadioButtonState.CheckedNormal;
           } 
           else { //unchecked 
               if ((state & ButtonState.Pushed) == ButtonState.Pushed) {
                   return RadioButtonState.UncheckedPressed; 
               }
               else if ((state & ButtonState.Inactive) == ButtonState.Inactive) {
                   return RadioButtonState.UncheckedDisabled;
               } 
               else if (isHot) {
                   return RadioButtonState.UncheckedHot; 
               } 

               return RadioButtonState.UncheckedNormal; 
           }
       }

       private static void InitializeRenderer(int state) { 
           if (visualStyleRenderer == null) {
               visualStyleRenderer = new VisualStyleRenderer(RadioElement.ClassName, RadioElement.Part, state); 
           } 
           else {
               visualStyleRenderer.SetParameters(RadioElement.ClassName, RadioElement.Part, state); 
           }
       }
    }
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------------------------ 
// 
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// 
//----------------------------------------------------------------------------- 
namespace System.Windows.Forms {
 
using System; 
using System.Drawing;
using System.Diagnostics.CodeAnalysis; 
using System.Windows.Forms.Internal;
using System.Windows.Forms.VisualStyles;
using Microsoft.Win32;
 
    /// 
    ///  
    ///     
    ///       This is a rendering class for the RadioButton control. It works downlevel too (obviously
    ///       without visual styles applied.) 
    ///    
    /// 
    public sealed class RadioButtonRenderer {
 
       //Make this per-thread, so that different threads can safely use these methods.
       [ThreadStatic] 
       private static VisualStyleRenderer visualStyleRenderer = null; 
       private static readonly VisualStyleElement RadioElement = VisualStyleElement.Button.RadioButton.UncheckedNormal;
        private static bool renderMatchingApplicationState = true; 

       //cannot instantiate
       private RadioButtonRenderer() {
       } 

       ///  
       ///  
       ///    
       ///      If this property is true, then the renderer will use the setting from Application.RenderWithVisualStyles to 
       /// determine how to render.
       ///      If this property is false, the renderer will always render with visualstyles.
       ///    
       ///  
       public static bool RenderMatchingApplicationState {
           get { 
               return renderMatchingApplicationState; 
           }
           set { 
               renderMatchingApplicationState = value;
           }
       }
 
       private static bool RenderWithVisualStyles {
           get { 
               return (!renderMatchingApplicationState || Application.RenderWithVisualStyles); 
           }
       } 

       /// 
       /// 
       ///     
       ///       Returns true if the background corresponding to the given state is partially transparent, else false.
       ///     
       ///  
       public static bool IsBackgroundPartiallyTransparent(RadioButtonState state) {
           if (RenderWithVisualStyles) { 
               InitializeRenderer((int)state);

               return visualStyleRenderer.IsBackgroundPartiallyTransparent();
           } 
           else {
               return false; //for downlevel, this is false 
           } 
       }
 
       /// 
       /// 
       ///    
       ///       This is just a convenience wrapper for VisualStyleRenderer.DrawThemeParentBackground. For downlevel, 
       ///       this isn't required and does nothing.
       ///     
       ///  
       [
           SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters") // Using Graphics instead of IDeviceContext intentionally 
       ]
       public static void DrawParentBackground(Graphics g, Rectangle bounds, Control childControl) {
           if (RenderWithVisualStyles) {
               InitializeRenderer(0); 

               visualStyleRenderer.DrawParentBackground(g, bounds, childControl); 
           } 
       }
 
       /// 
       /// 
       ///    
       ///       Renders a RadioButton control. 
       ///    
       ///  
       public static void DrawRadioButton(Graphics g, Point glyphLocation, RadioButtonState state) { 
           Rectangle glyphBounds = new Rectangle(glyphLocation, GetGlyphSize(g, state));
 
           if (RenderWithVisualStyles) {
               InitializeRenderer((int)state);

               visualStyleRenderer.DrawBackground(g, glyphBounds); 
           }
           else { 
               ControlPaint.DrawRadioButton(g, glyphBounds, ConvertToButtonState(state)); 
           }
       } 

       /// 
       /// 
       ///     
       ///       Renders a RadioButton control.
       ///     
       ///  
       public static void DrawRadioButton(Graphics g, Point glyphLocation, Rectangle textBounds, string radioButtonText, Font font, bool focused, RadioButtonState state) {
           DrawRadioButton(g, glyphLocation, textBounds, radioButtonText, font, 
                      TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.SingleLine,
                      focused, state);
       }
 

 
       ///  
       /// 
       ///     
       ///       Renders a RadioButton control.
       ///    
       /// 
       public static void DrawRadioButton(Graphics g, Point glyphLocation, Rectangle textBounds, string radioButtonText, Font font, TextFormatFlags flags, bool focused, RadioButtonState state) { 
           Rectangle glyphBounds = new Rectangle(glyphLocation, GetGlyphSize(g, state));
           Color textColor; 
 
           if (RenderWithVisualStyles) {
               InitializeRenderer((int)state); 

               visualStyleRenderer.DrawBackground(g, glyphBounds);
               textColor = visualStyleRenderer.GetColor(ColorProperty.TextColor);
           } 
           else {
               ControlPaint.DrawRadioButton(g, glyphBounds, ConvertToButtonState(state)); 
               textColor = SystemColors.ControlText; 
           }
 
           TextRenderer.DrawText(g, radioButtonText, font, textBounds, textColor, flags);

           if (focused) {
               ControlPaint.DrawFocusRectangle(g, textBounds); 
           }
       } 
 
       /// 
       ///  
       ///    
       ///       Renders a RadioButton control.
       ///    
       ///  
       public static void DrawRadioButton(Graphics g, Point glyphLocation, Rectangle textBounds, string radioButtonText, Font font, Image image, Rectangle imageBounds, bool focused, RadioButtonState state) {
           DrawRadioButton(g, glyphLocation, textBounds, radioButtonText, font, 
                      TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.SingleLine, 
                      image, imageBounds, focused, state);
       } 



       ///  
       /// 
       ///     
       ///       Renders a RadioButton control. 
       ///    
       ///  
       public static void DrawRadioButton(Graphics g, Point glyphLocation, Rectangle textBounds, string radioButtonText, Font font, TextFormatFlags flags, Image image, Rectangle imageBounds, bool focused, RadioButtonState state) {
           Rectangle glyphBounds = new Rectangle(glyphLocation, GetGlyphSize(g, state));
           Color textColor;
 
           if (RenderWithVisualStyles) {
               InitializeRenderer((int)state); 
 
               //Keep this drawing order! It matches default drawing order.
               visualStyleRenderer.DrawImage(g, imageBounds, image); 
               visualStyleRenderer.DrawBackground(g, glyphBounds);
               textColor = visualStyleRenderer.GetColor(ColorProperty.TextColor);
           }
           else { 
               g.DrawImage(image, imageBounds);
               ControlPaint.DrawRadioButton(g, glyphBounds, ConvertToButtonState(state)); 
               textColor = SystemColors.ControlText; 
           }
 
           TextRenderer.DrawText(g, radioButtonText, font, textBounds, textColor, flags);

           if (focused) {
               ControlPaint.DrawFocusRectangle(g, textBounds); 
           }
       } 
 
       /// 
       ///  
       ///    
       ///       Returns the size of the RadioButton glyph.
       ///    
       ///  
       [
           SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters") // Using Graphics instead of IDeviceContext intentionally 
       ] 
       public static Size GetGlyphSize(Graphics g, RadioButtonState state) {
           if (RenderWithVisualStyles) { 
               InitializeRenderer((int)state);

               return visualStyleRenderer.GetPartSize(g, ThemeSizeType.Draw);
           } 

           return new Size(13, 13); 
       } 

       internal static ButtonState ConvertToButtonState(RadioButtonState state) { 
           switch (state) {
           case RadioButtonState.CheckedNormal:
           case RadioButtonState.CheckedHot:
               return ButtonState.Checked; 
           case RadioButtonState.CheckedPressed:
               return (ButtonState.Checked | ButtonState.Pushed); 
           case RadioButtonState.CheckedDisabled: 
               return (ButtonState.Checked | ButtonState.Inactive);
 
           case RadioButtonState.UncheckedPressed:
               return ButtonState.Pushed;
           case RadioButtonState.UncheckedDisabled:
               return ButtonState.Inactive; 

           default: 
               return ButtonState.Normal; 
           }
       } 

       internal static RadioButtonState ConvertFromButtonState(ButtonState state, bool isHot) {
           if ((state & ButtonState.Checked) == ButtonState.Checked) {
               if ((state & ButtonState.Pushed) == ButtonState.Pushed) { 
                   return RadioButtonState.CheckedPressed;
               } 
               else if ((state & ButtonState.Inactive) == ButtonState.Inactive) { 
                   return RadioButtonState.CheckedDisabled;
               } 
               else if (isHot) {
                   return RadioButtonState.CheckedHot;
               }
 
               return RadioButtonState.CheckedNormal;
           } 
           else { //unchecked 
               if ((state & ButtonState.Pushed) == ButtonState.Pushed) {
                   return RadioButtonState.UncheckedPressed; 
               }
               else if ((state & ButtonState.Inactive) == ButtonState.Inactive) {
                   return RadioButtonState.UncheckedDisabled;
               } 
               else if (isHot) {
                   return RadioButtonState.UncheckedHot; 
               } 

               return RadioButtonState.UncheckedNormal; 
           }
       }

       private static void InitializeRenderer(int state) { 
           if (visualStyleRenderer == null) {
               visualStyleRenderer = new VisualStyleRenderer(RadioElement.ClassName, RadioElement.Part, state); 
           } 
           else {
               visualStyleRenderer.SetParameters(RadioElement.ClassName, RadioElement.Part, state); 
           }
       }
    }
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.

                        

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