ZForge.Controls.HeaderPanel.DrawCaption C# (CSharp) Method

DrawCaption() private method

private DrawCaption ( Graphics canvas ) : void
canvas Graphics
return void
        private void DrawCaption(Graphics canvas)
        {
            int brdWidth = 0;
            int shadowWidth = 0;
            Bitmap offScreenBmp = null;
            Graphics offScreenDC = null;
            Rectangle drawRect;
            StringFormat format = null;
            LinearGradientBrush brsh = null;

            try
            {
                if (Bounds.Width == 0 || Bounds.Height == 0)
                {
                    return;
                }
                if (menm_BorderStyle == HeaderPanelBorderStyles.None)
                    brdWidth = 0;
                else if (menm_BorderStyle == HeaderPanelBorderStyles.Groove ||
                                menm_BorderStyle == HeaderPanelBorderStyles.Ridge)
                    brdWidth = 2;
                else
                    brdWidth = DEFAULT_BORDER_WIDTH;

                if (menm_BorderStyle == HeaderPanelBorderStyles.Shadow)
                    shadowWidth = DEFAULT_SHADOW_WIDTH;

                format = new StringFormat();
                format.FormatFlags = StringFormatFlags.NoWrap;
                format.LineAlignment = StringAlignment.Center;
                format.Alignment = StringAlignment.Near;
                format.Trimming = StringTrimming.EllipsisCharacter;

                //Console.WriteLine(" W: " + (Bounds.Width- ((2 * brdWidth) + shadowWidth)));
                //Console.WriteLine(" W: " + Bounds.Width);
                if (menm_CaptionPosition == HeaderPanelCaptionPositions.Bottom ||
                        menm_CaptionPosition == HeaderPanelCaptionPositions.Top)
                    offScreenBmp = new Bitmap(Bounds.Width - ((2 * brdWidth) + shadowWidth),
                                                                            mint_CaptionHeight);
                else
                    offScreenBmp = new Bitmap(Bounds.Height - ((2 * brdWidth) + shadowWidth),
                                                                            mint_CaptionHeight);

                offScreenDC = Graphics.FromImage(offScreenBmp);
                if (mbln_Antialias)
                    offScreenDC.TextRenderingHint = TextRenderingHint.AntiAlias;

                brsh = new LinearGradientBrush(new Rectangle(0, 0,
                                                                                        offScreenBmp.Width,
                                                                                        offScreenBmp.Height),
                                                                                mclr_CaptionBeginColor,
                                                                                mclr_CaptionEndColor,
                                                                                menm_CaptionGradientMode);
                offScreenDC.FillRectangle(brsh, 0.0f, 0.0f, offScreenBmp.Width,
                                                                        offScreenBmp.Height);
                if (mico_Icon != null && mbln_IconVisible)
                {
                    offScreenDC.DrawIcon(mico_Icon,
                                                            new Rectangle(brdWidth + 2,
                                                                            (mint_CaptionHeight - 16) / 2,
                                                                            16, 16));
                    offScreenDC.DrawString(this.CaptionText, this.CaptionFont,
                                                                    new SolidBrush(this.ForeColor),
                                                                    new Rectangle(20, 0,
                                                                                    offScreenBmp.Width - 20,
                                                                                    mint_CaptionHeight), format);
                }
                else
                {
                    offScreenDC.DrawString(this.CaptionText, this.CaptionFont,
                                                                    new SolidBrush(this.ForeColor),
                                                                    new Rectangle(2, 0,
                                                                                    offScreenBmp.Width,
                                                                                    mint_CaptionHeight), format);
                    //Console.WriteLine("Draw Caption " + mstr_CaptionText + " " + this.CaptionText + " " + offScreenBmp.Width + " " + mint_CaptionHeight);
                }

                drawRect = new Rectangle();
                switch (menm_CaptionPosition)
                {
                    case HeaderPanelCaptionPositions.Top:
                        {
                            drawRect.X = brdWidth;
                            drawRect.Width = DisplayRectangle.Width;
                            drawRect.Y = brdWidth;
                            drawRect.Height = mint_CaptionHeight;
                            break;
                        }
                    case HeaderPanelCaptionPositions.Bottom:
                        {
                            drawRect.X = brdWidth;
                            drawRect.Width = DisplayRectangle.Width;
                            drawRect.Y = Height - (shadowWidth + brdWidth +
                                                                            mint_CaptionHeight);
                            drawRect.Height = mint_CaptionHeight;
                            break;
                        }
                    case HeaderPanelCaptionPositions.Left:
                        {
                            drawRect.X = brdWidth;
                            drawRect.Width = mint_CaptionHeight;
                            drawRect.Y = brdWidth;
                            drawRect.Height = Height - (shadowWidth + (2 * brdWidth));
                            offScreenBmp.RotateFlip(RotateFlipType.Rotate270FlipNone);
                            break;
                        }
                    case HeaderPanelCaptionPositions.Right:
                        {
                            drawRect.X = Width - (shadowWidth + brdWidth + mint_CaptionHeight);
                            drawRect.Width = mint_CaptionHeight;
                            drawRect.Y = brdWidth;
                            drawRect.Height = Height - (shadowWidth + (2 * brdWidth));
                            offScreenBmp.RotateFlip(RotateFlipType.Rotate90FlipNone);
                            break;
                        }
                }
                canvas.DrawImage(offScreenBmp, drawRect);
            }
            finally
            {
                if (brsh != null)
                {
                    brsh.Dispose();
                    brsh = null;
                }

                if (offScreenDC != null)
                {
                    offScreenDC.Dispose();
                    offScreenDC = null;
                }

                if (offScreenBmp != null)
                {
                    offScreenBmp.Dispose();
                    offScreenBmp = null;
                }

                if (format != null)
                {
                    format.Dispose();
                    format = null;
                }
            }
        }