示例#1
0
        public static void DrawGroupBox(Graphics g, Rectangle bounds, string groupBoxText, Font font, Color textColor, TextFormatFlags flags, GroupBoxState state)
        {
            Size font_size = TextRenderer.MeasureText(groupBoxText, font);

            if (Application.RenderWithVisualStyles || always_use_visual_styles == true)
            {
                VisualStyleRenderer vsr;
                Rectangle           new_bounds;

                switch (state)
                {
                case GroupBoxState.Normal:
                default:
                    vsr        = new VisualStyleRenderer(VisualStyleElement.Button.GroupBox.Normal);
                    new_bounds = new Rectangle(bounds.Left, bounds.Top + (int)(font_size.Height / 2) - 1, bounds.Width, bounds.Height - (int)(font_size.Height / 2) + 1);
                    break;

                case GroupBoxState.Disabled:
                    vsr        = new VisualStyleRenderer(VisualStyleElement.Button.GroupBox.Disabled);
                    new_bounds = new Rectangle(bounds.Left, bounds.Top + (int)(font_size.Height / 2) - 2, bounds.Width, bounds.Height - (int)(font_size.Height / 2) + 2);
                    break;
                }

                if (groupBoxText == String.Empty)
                {
                    vsr.DrawBackground(g, bounds);
                }
                else
                {
                    vsr.DrawBackgroundExcludingArea(g, new_bounds, new Rectangle(bounds.Left + 9, bounds.Top, font_size.Width - 3, font_size.Height));
                }

                if (textColor == Color.Empty)
                {
                    textColor = vsr.GetColor(ColorProperty.TextColor);
                }

                if (groupBoxText != String.Empty)
                {
                    TextRenderer.DrawText(g, groupBoxText, font, new Point(bounds.Left + 8, bounds.Top), textColor, flags);
                }
            }
            else
            {
                // MS has a pretty big bug when rendering the non-visual styles group box.  Instead of using the height
                // part of the bounds as height, they use it as the bottom, so the boxes are drawn in completely different
                // places.  Rather than emulate this bug, we do it correctly.  After googling for a while, I don't think
                // anyone has ever actually used this class for anything, so it should be fine.  :)
                Rectangle new_bounds = new Rectangle(bounds.Left, bounds.Top + (int)(font_size.Height / 2), bounds.Width, bounds.Height - (int)(font_size.Height / 2));

                // Don't paint over the background where we are going to put the text
                var savedClip = g.Clip.Clone();

                g.SetClip(new Rectangle(bounds.Left + 9, bounds.Top, font_size.Width - 3, font_size.Height), System.Drawing.Drawing2D.CombineMode.Exclude);
                ControlPaint.DrawBorder3D(g, new_bounds, Border3DStyle.Etched);

                g.Clip = savedClip;

                if (groupBoxText != String.Empty)
                {
                    if (textColor == Color.Empty)
                    {
                        textColor = state == GroupBoxState.Normal ? SystemColors.ControlText :
                                    SystemColors.GrayText;
                    }
                    TextRenderer.DrawText(g, groupBoxText, font, new Point(bounds.Left + 11 /*8*/, bounds.Top), textColor, flags);
                }
            }
        }