Ejemplo n.º 1
0
        private static void DrawTextInternal(
            IDeviceContext dc,
            string?text,
            Font?font,
            Rectangle bounds,
            Color foreColor,
            Color backColor = default,
            User32.DT flags = User32.DT.CENTER | User32.DT.VCENTER)
        {
            if (dc is null)
            {
                throw new ArgumentNullException(nameof(dc));
            }

            // Avoid creating the HDC, etc if we're not going to do any drawing
            if (string.IsNullOrEmpty(text) || foreColor == Color.Transparent)
            {
                return;
            }

            // This MUST come before retreiving the HDC, which locks the Graphics object
            Gdi32.QUALITY quality = FontQualityFromTextRenderingHint(dc);

            using var hdc            = new DeviceContextHdcScope(dc, applyGraphicsState: false);
            using WindowsGraphics wg = WindowsGraphics.FromHdc(hdc);
            using WindowsFont? wf    = WindowsGraphicsCacheManager.GetWindowsFont(font, quality);
            wg.DrawText(text, wf, bounds, foreColor, backColor, flags);
        }
Ejemplo n.º 2
0
        /// <include file='doc\TextRenderer.uex' path='docs/doc[@for="TextRenderer.DrawText1"]/*' />
        public static void DrawText(IDeviceContext dc, string text, Font font, Point pt, Color foreColor, Color backColor)
        {
            if (dc == null)
            {
                throw new ArgumentNullException("dc");
            }

            WindowsFontQuality fontQuality = WindowsFont.WindowsFontQualityFromTextRenderingHint(dc as Graphics);

            IntPtr hdc = dc.GetHdc();

            try
            {
                using (WindowsGraphics wg = WindowsGraphics.FromHdc(hdc))
                {
                    using (WindowsFont wf = WindowsGraphicsCacheManager.GetWindowsFont(font, fontQuality)) {
                        wg.DrawText(text, wf, pt, foreColor, backColor);
                    }
                }
            }
            finally
            {
                dc.ReleaseHdc();
            }
        }
Ejemplo n.º 3
0
        public static void DrawText(IDeviceContext dc, string text, Font font, Rectangle bounds, Color foreColor)
        {
            if (dc == null)
            {
                throw new ArgumentNullException(nameof(dc));
            }

            Gdi32.QUALITY fontQuality = WindowsFont.WindowsFontQualityFromTextRenderingHint(dc as Graphics);

            IntPtr hdc = dc.GetHdc();

            try
            {
                using (WindowsGraphics wg = WindowsGraphics.FromHdc(hdc))
                {
                    using (WindowsFont wf = WindowsGraphicsCacheManager.GetWindowsFont(font, fontQuality))
                    {
                        wg.DrawText(text, wf, bounds, foreColor);
                    }
                }
            }
            finally
            {
                dc.ReleaseHdc();
            }
        }
        private void DrawGroupBox(PaintEventArgs e)
        {
            Graphics  graphics        = e.Graphics;
            Rectangle clientRectangle = base.ClientRectangle;
            int       num             = 8;
            Color     disabledColor   = base.DisabledColor;
            Pen       pen             = new Pen(ControlPaint.Light(disabledColor, 1f));
            Pen       pen2            = new Pen(ControlPaint.Dark(disabledColor, 0f));

            clientRectangle.X     += num;
            clientRectangle.Width -= 2 * num;
            try
            {
                Size size;
                int  num2;
                if (this.UseCompatibleTextRendering)
                {
                    using (Brush brush = new SolidBrush(this.ForeColor))
                    {
                        using (StringFormat format = new StringFormat())
                        {
                            format.HotkeyPrefix = this.ShowKeyboardCues ? HotkeyPrefix.Show : HotkeyPrefix.Hide;
                            if (this.RightToLeft == RightToLeft.Yes)
                            {
                                format.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
                            }
                            size = Size.Ceiling(graphics.MeasureString(this.Text, this.Font, clientRectangle.Width, format));
                            if (base.Enabled)
                            {
                                graphics.DrawString(this.Text, this.Font, brush, clientRectangle, format);
                            }
                            else
                            {
                                ControlPaint.DrawStringDisabled(graphics, this.Text, this.Font, disabledColor, clientRectangle, format);
                            }
                        }
                        goto Label_01E7;
                    }
                }
                using (WindowsGraphics graphics2 = WindowsGraphics.FromGraphics(graphics))
                {
                    IntTextFormatFlags flags = IntTextFormatFlags.TextBoxControl | IntTextFormatFlags.WordBreak;
                    if (!this.ShowKeyboardCues)
                    {
                        flags |= IntTextFormatFlags.HidePrefix;
                    }
                    if (this.RightToLeft == RightToLeft.Yes)
                    {
                        flags |= IntTextFormatFlags.RightToLeft;
                        flags |= IntTextFormatFlags.Right;
                    }
                    using (WindowsFont font = WindowsGraphicsCacheManager.GetWindowsFont(this.Font))
                    {
                        size = graphics2.MeasureText(this.Text, font, new Size(clientRectangle.Width, 0x7fffffff), flags);
                        if (base.Enabled)
                        {
                            graphics2.DrawText(this.Text, font, clientRectangle, this.ForeColor, flags);
                        }
                        else
                        {
                            ControlPaint.DrawStringDisabled(graphics2, this.Text, this.Font, disabledColor, clientRectangle, (TextFormatFlags)flags);
                        }
                    }
                }
Label_01E7:
                num2 = num;
                if (this.RightToLeft == RightToLeft.Yes)
                {
                    num2 += clientRectangle.Width - size.Width;
                }
                int num3 = Math.Min((int)(num2 + size.Width), (int)(base.Width - 6));
                int num4 = base.FontHeight / 2;
                graphics.DrawLine(pen, 1, num4, 1, base.Height - 1);
                graphics.DrawLine(pen2, 0, num4, 0, base.Height - 2);
                graphics.DrawLine(pen, 0, base.Height - 1, base.Width, base.Height - 1);
                graphics.DrawLine(pen2, 0, base.Height - 2, base.Width - 1, base.Height - 2);
                graphics.DrawLine(pen2, 0, num4 - 1, num2, num4 - 1);
                graphics.DrawLine(pen, 1, num4, num2, num4);
                graphics.DrawLine(pen2, num3, num4 - 1, base.Width - 2, num4 - 1);
                graphics.DrawLine(pen, num3, num4, base.Width - 1, num4);
                graphics.DrawLine(pen, (int)(base.Width - 1), (int)(num4 - 1), (int)(base.Width - 1), (int)(base.Height - 1));
                graphics.DrawLine(pen2, base.Width - 2, num4, base.Width - 2, base.Height - 2);
            }
            finally
            {
                pen.Dispose();
                pen2.Dispose();
            }
        }
Ejemplo n.º 5
0
        private void DrawGroupBox(PaintEventArgs e)
        {
            Graphics  graphics      = e.Graphics;
            Rectangle textRectangle = ClientRectangle; // Max text bounding box passed to drawing methods to support RTL.

            int textOffset = 8;                        // Offset from the left bound.

            Color backColor = DisabledColor;

            Pen  light = new Pen(ControlPaint.Light(backColor, 1.0f));
            Pen  dark  = new Pen(ControlPaint.Dark(backColor, 0f));
            Size textSize;

            textRectangle.X     += textOffset;
            textRectangle.Width -= 2 * textOffset;

            try {
                if (UseCompatibleTextRendering)
                {
                    using (Brush textBrush = new SolidBrush(ForeColor)){
                        using (StringFormat format = new StringFormat()){
                            format.HotkeyPrefix = ShowKeyboardCues ? System.Drawing.Text.HotkeyPrefix.Show : System.Drawing.Text.HotkeyPrefix.Hide;

                            // Adjust string format for Rtl controls

                            if (RightToLeft == RightToLeft.Yes)
                            {
                                format.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
                            }

                            textSize = Size.Ceiling(graphics.MeasureString(Text, Font, textRectangle.Width, format));

                            if (Enabled)
                            {
                                graphics.DrawString(Text, Font, textBrush, textRectangle, format);
                            }
                            else
                            {
                                ControlPaint.DrawStringDisabled(graphics, Text, Font, backColor, textRectangle, format);
                            }
                        }
                    }
                }
                else
                {
                    using (WindowsGraphics wg = WindowsGraphics.FromGraphics(graphics)){
                        IntTextFormatFlags flags = IntTextFormatFlags.WordBreak | IntTextFormatFlags.TextBoxControl;

                        if (!ShowKeyboardCues)
                        {
                            flags |= IntTextFormatFlags.HidePrefix;
                        }

                        if (RightToLeft == RightToLeft.Yes)
                        {
                            flags |= IntTextFormatFlags.RightToLeft;
                            flags |= IntTextFormatFlags.Right;
                        }


                        using (WindowsFont wfont = WindowsGraphicsCacheManager.GetWindowsFont(this.Font)) {
                            textSize = wg.MeasureText(Text, wfont, new Size(textRectangle.Width, int.MaxValue), flags);

                            if (Enabled)
                            {
                                wg.DrawText(Text, wfont, textRectangle, ForeColor, flags);
                            }
                            else
                            {
                                ControlPaint.DrawStringDisabled(wg, Text, Font, backColor, textRectangle, ((TextFormatFlags)flags));
                            }
                        }
                    }
                }

                int textLeft = textOffset;    // Left side of binding box (independent on RTL).

                if (RightToLeft == RightToLeft.Yes)
                {
                    textLeft += textRectangle.Width - textSize.Width;
                }

                // Math.Min to assure we paint at least a small line.
                int textRight = Math.Min(textLeft + textSize.Width, Width - 6);

                int boxTop = FontHeight / 2;

                if (SystemInformation.HighContrast && AccessibilityImprovements.Level1)
                {
                    Color boxColor;
                    if (Enabled)
                    {
                        boxColor = ForeColor;
                    }
                    else
                    {
                        boxColor = SystemColors.GrayText;
                    }
                    bool needToDispose = !boxColor.IsSystemColor;
                    Pen  boxPen        = null;
                    try {
                        if (needToDispose)
                        {
                            boxPen = new Pen(boxColor);
                        }
                        else
                        {
                            boxPen = SystemPens.FromSystemColor(boxColor);
                        }

                        // left
                        graphics.DrawLine(boxPen, 0, boxTop, 0, Height);
                        //bottom
                        graphics.DrawLine(boxPen, 0, Height - 1, Width, Height - 1);
                        //top-left
                        graphics.DrawLine(boxPen, 0, boxTop, textLeft, boxTop);
                        //top-right
                        graphics.DrawLine(boxPen, textRight, boxTop, Width - 1, boxTop);
                        //right
                        graphics.DrawLine(boxPen, Width - 1, boxTop, Width - 1, Height - 1);
                    }
                    finally {
                        if (needToDispose && boxPen != null)
                        {
                            boxPen.Dispose();
                        }
                    }
                }
                else
                {
                    // left
                    graphics.DrawLine(light, 1, boxTop, 1, Height - 1);
                    graphics.DrawLine(dark, 0, boxTop, 0, Height - 2);

                    // bottom
                    graphics.DrawLine(light, 0, Height - 1, Width, Height - 1);
                    graphics.DrawLine(dark, 0, Height - 2, Width - 1, Height - 2);

                    // top-left

                    graphics.DrawLine(dark, 0, boxTop - 1, textLeft, boxTop - 1);
                    graphics.DrawLine(light, 1, boxTop, textLeft, boxTop);

                    // top-right
                    graphics.DrawLine(dark, textRight, boxTop - 1, Width - 2, boxTop - 1);
                    graphics.DrawLine(light, textRight, boxTop, Width - 1, boxTop);

                    // right
                    graphics.DrawLine(light, Width - 1, boxTop - 1, Width - 1, Height - 1);
                    graphics.DrawLine(dark, Width - 2, boxTop, Width - 2, Height - 2);
                }
            }
            finally {
                light.Dispose();
                dark.Dispose();
            }
        }