measureText() public static method

Measures the Size of a Text.
public static measureText ( Graphics g, string text, Font font ) : SizeF
g System.Drawing.Graphics Graphics object used to draw the text
text string The text
font System.Drawing.Font Font used to draw the text
return System.Drawing.SizeF
        protected override void OnPaint(PaintEventArgs pevent)
        {
            Brush bg;

            if (Checked)
            {
                bg = new SolidBrush(ColorManager.ui_hoveredLight);
            }
            else
            {
                bg = new SolidBrush(hover ? ColorManager.ui_hoveredDark : ColorManager.ui_bgDark);
            }

            pevent.Graphics.FillRectangle(bg, ClientRectangle);

            int bx = Width - 1;
            int by = Height - 1;

            pevent.Graphics.DrawLine(new Pen(Color.FromArgb(64, Color.White)), 0, 0, bx, 0);
            pevent.Graphics.DrawLine(new Pen(Color.FromArgb(64, Color.White)), 0, 1, 0, by);
            pevent.Graphics.DrawLine(new Pen(Color.FromArgb(64, Color.Black)), 0, by, bx, by);
            pevent.Graphics.DrawLine(new Pen(Color.FromArgb(64, Color.Black)), bx, 1, bx, by);

            string text     = DrawingUtils.clampText(pevent.Graphics, Text, Font, Width);
            SizeF  textSize = DrawingUtils.measureText(pevent.Graphics, text, Font);
            int    width    = (int)textSize.Width;
            int    x        = Math.Max(0, (Width / 2) - (width / 2));
            int    yText    = (Height / 2) - (int)(textSize.Height / 2);

            pevent.Graphics.DrawString(text, Font, new SolidBrush(Enabled ? ForeColor : Color.Silver), new Point(x, yText));
        }
Example #2
0
        protected override void OnPaint(PaintEventArgs e)
        {
            string text     = autoSize ? Text : DrawingUtils.clampText(e.Graphics, Text, Font, Width);
            SizeF  textSize = DrawingUtils.measureText(e.Graphics, text, Font);

            if (autoSize)
            {
                Size = new Size((int)textSize.Width, (int)textSize.Height);
            }
            int   x            = centered ? (Width / 2) - ((int)textSize.Width / 2) : 0;
            Point textLocation = new Point(x, (Height / 2) - ((int)textSize.Height / 2));

            e.Graphics.DrawString(text, Font, new SolidBrush(Enabled ? ForeColor : Color.Silver), textLocation);

            base.OnPaint(e);
        }
Example #3
0
        protected override void OnPaint(PaintEventArgs pevent)
        {
            Brush bg = new SolidBrush(hover ? ColorManager.ui_hoveredDark : ColorManager.ui_bgDark);

            pevent.Graphics.FillRectangle(bg, ClientRectangle);

            int bx = Width - 1;
            int by = Height - 1;

            pevent.Graphics.DrawLine(new Pen(Color.FromArgb(64, Color.White)), 0, 0, bx, 0);
            pevent.Graphics.DrawLine(new Pen(Color.FromArgb(64, Color.White)), 0, 1, 0, by);
            pevent.Graphics.DrawLine(new Pen(Color.FromArgb(64, Color.Black)), 0, by, bx, by);
            pevent.Graphics.DrawLine(new Pen(Color.FromArgb(64, Color.Black)), bx, 1, bx, by);

            string text = null;

            if (Text.Length > 0)
            {
                text = DrawingUtils.clampText(pevent.Graphics, Text, Font, Width - (img != null ? img.Width : 0) - 2);
            }
            SizeF textSize = DrawingUtils.measureText(pevent.Graphics, text, Font);
            int   width    = (int)textSize.Width;
            int   yImage   = 0;

            if (img != null)
            {
                width += img.Width;
                yImage = (Height / 2) - (img.Height / 2);
            }

            int   x         = centered ? (Width / 2) - (width / 2) : 0;
            int   yText     = (Height / 2) - (int)(textSize.Height / 2);
            Brush textBrush = new SolidBrush(Enabled ? ForeColor : Color.Silver);

            if (img != null)
            {
                pevent.Graphics.DrawImage(img, new Rectangle(x, yImage, img.Width, img.Height));
                pevent.Graphics.DrawString(text, Font, textBrush, new Point(x + img.Width, yText));
            }
            else
            {
                pevent.Graphics.DrawString(text, Font, textBrush, new Point(x, yText));
            }

            base.OnPaint(pevent);
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            int w = Resources.ui_icon_tick.Width;
            int h = Resources.ui_icon_tick.Height;

            //Draw box around
            Color lineColor = ForeColor;

            if (!Enabled)
            {
                lineColor = SystemColors.InactiveCaptionText;
            }
            e.Graphics.DrawLine(new Pen(lineColor), new Point(0, Height - 1), new Point(w - 1, Height - 1));
            e.Graphics.DrawLine(new Pen(lineColor), new Point(0, Height - 1), new Point(0, Height - 2));
            e.Graphics.DrawLine(new Pen(lineColor), new Point(w - 1, Height - 1), new Point(w - 1, Height - 2));

            //Ticked icon (if checked)
            Rectangle checkRect = new Rectangle(0, (Height / 2) - (h / 2), w, h);

            if (_checked)
            {
                e.Graphics.DrawImage(Resources.ui_icon_tick, checkRect);
            }

            //Draw text at the right of the box
            string text     = autoSize ? Text : DrawingUtils.clampText(e.Graphics, Text, Font, Width);
            SizeF  textSize = DrawingUtils.measureText(e.Graphics, text, Font);

            if (autoSize)
            {
                Size = new Size((int)textSize.Width + checkRect.Width, (int)textSize.Height);
            }
            Point textLocation = new Point(checkRect.Width, (Height / 2) - ((int)textSize.Height / 2));

            e.Graphics.DrawString(text, Font, new SolidBrush(Enabled ? ForeColor : Color.Silver), textLocation);

            base.OnPaint(e);
        }