Export (0) Print
Expand All

GroupBoxRenderer Class

Provides methods used to render a group box control with or without visual styles. This class cannot be inherited.

System.Object
  System.Windows.Forms.GroupBoxRenderer

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public sealed class GroupBoxRenderer

The GroupBoxRenderer type exposes the following members.

  NameDescription
Public propertyStatic memberRenderMatchingApplicationStateGets or sets a value indicating whether the renderer uses the application state to determine rendering style.
Top

  NameDescription
Public methodStatic memberDrawGroupBox(Graphics, Rectangle, GroupBoxState)Draws a group box control in the specified state and bounds.
Public methodStatic memberDrawGroupBox(Graphics, Rectangle, String, Font, GroupBoxState)Draws a group box control in the specified state and bounds, with the specified text and font.
Public methodStatic memberDrawGroupBox(Graphics, Rectangle, String, Font, Color, GroupBoxState)Draws a group box control in the specified state and bounds, with the specified text, font, and color.
Public methodStatic memberDrawGroupBox(Graphics, Rectangle, String, Font, TextFormatFlags, GroupBoxState)Draws a group box control in the specified state and bounds, with the specified text, font, and text formatting.
Public methodStatic memberDrawGroupBox(Graphics, Rectangle, String, Font, Color, TextFormatFlags, GroupBoxState)Draws a group box control in the specified state and bounds, with the specified text, font, color, and text formatting.
Public methodStatic memberDrawParentBackgroundDraws the background of a control's parent in the specified area.
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodStatic memberIsBackgroundPartiallyTransparentIndicates whether the background of the group box has any semitransparent or alpha-blended pieces.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

The GroupBoxRenderer class provides a set of static methods that can be used to render a group box control. Rendering a control refers to drawing the user interface of a control. To draw a group box, use one of the DrawGroupBox methods. These methods provide a variety of text drawing options.

If visual styles are enabled in the operating system and visual styles are applied to the current application, DrawGroupBox will draw the group box with the current visual style. Otherwise, DrawGroupBox will draw the group box with the classic Windows style. This is useful if you are drawing a custom control that should automatically match the current visual style setting of the operating system.

This class wraps the functionality of a System.Windows.Forms.VisualStyles.VisualStyleRenderer that is set to one of the elements exposed by the System.Windows.Forms.VisualStyles.VisualStyleElement.Button.GroupBox class. For more information, see Rendering Controls with Visual Styles.

Windows XP Home Edition, Windows XP Professional x64 Edition, Windows Server 2003 Platform Note: Visual styles are supported only on these platforms.

The following code example demonstrates how to create a custom control that uses the DrawGroupBox method to draw a group box with a double border if visual styles are enabled.

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;

namespace GroupBoxRendererSample
{
    class Form1 : Form
    {
        private Button button1;

        public Form1()
            : base()
        {
            CustomGroupBox GroupBox1 = new CustomGroupBox();
            button1 = new Button();

            GroupBox1.Text = "Radio Button Display";
            this.button1.Location = new System.Drawing.Point(205, 231);
            this.button1.Size = new System.Drawing.Size(105, 23);
            this.button1.Text = "Toggle Visual Styles";
            this.button1.Click += new System.EventHandler(this.button1_Click);
            Controls.Add(GroupBox1);
            this.Controls.Add(this.button1);

            // Add some radio buttons to test the CustomGroupBox. 
            int count = 8;
            RadioButton[] ButtonArray = new RadioButton[count];
            for (int i = 0; i < count; i++)
            {
                ButtonArray[i] = new RadioButton();
                ButtonArray[i].Text = "Button " + (i + 1).ToString();
                GroupBox1.Controls.Add(ButtonArray[i]);
            }

            if (Application.RenderWithVisualStyles)
                this.Text = "Visual Styles Enabled";
            else 
                this.Text = "Visual Styles Disabled";
        }

        [STAThread]
        static void Main()
        {
            // If you do not call EnableVisualStyles below, then  
            // GroupBoxRenderer automatically detects this and draws 
            // the group box without visual styles.
            Application.EnableVisualStyles();
            Application.Run(new Form1());
        }


	// Match application style and toggle visual styles off 
	// and on for the application. 
        private void button1_Click(object sender, EventArgs e)
        {
            GroupBoxRenderer.RenderMatchingApplicationState = true;
            Application.VisualStyleState = 
                Application.VisualStyleState ^ 
                VisualStyleState.ClientAndNonClientAreasEnabled;


            if (Application.RenderWithVisualStyles)
                this.Text = "Visual Styles Enabled";
            else 
                this.Text = "Visual Styles Disabled";
        }
    }

    public class CustomGroupBox : Control
    {
        private Rectangle innerRectangle = new Rectangle();
        private GroupBoxState state = GroupBoxState.Normal;
        private FlowLayoutPanel panel = new FlowLayoutPanel();

        public CustomGroupBox()
            : base()
        {
            this.Size = new Size(200, 200);
            this.Location = new Point(10, 10);
            this.Controls.Add(panel);
            this.Text = "CustomGroupBox";
            this.Font = SystemFonts.IconTitleFont;

            innerRectangle.X = ClientRectangle.X + 5;
            innerRectangle.Y = ClientRectangle.Y + 15;
            innerRectangle.Width = ClientRectangle.Width - 10;
            innerRectangle.Height = ClientRectangle.Height - 20;

            panel.FlowDirection = FlowDirection.TopDown;
            panel.Location = new Point(innerRectangle.X + 5,
                innerRectangle.Y + 5);
            panel.Size = new Size(innerRectangle.Width - 10,
                innerRectangle.Height - 10);
        }

        // Draw the group box in the current state. 
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            GroupBoxRenderer.DrawGroupBox(e.Graphics, ClientRectangle,
                this.Text, this.Font, state);

            // Draw an additional inner border if visual styles are enabled. 
            if (Application.RenderWithVisualStyles)
            {
                GroupBoxRenderer.DrawGroupBox(e.Graphics, innerRectangle, state);
            }
        }

        // Pass added controls to the internal FlowLayoutPanel. 
        protected override void OnControlAdded(ControlEventArgs e)
        {
            base.OnControlAdded(e);

            // Ensure that you do not add the panel itself. 
            if (e.Control != this.panel)
            {
                this.Controls.Remove(e.Control);
                panel.Controls.Add(e.Control);
            }
        }
    }
}

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft