Beispiel #1
0
 internal extern static int FillRect(
     IntPtr hDC,
     ref NativeStructs.RECT lprc,
     IntPtr hbr);
Beispiel #2
0
        /// <summary>
        /// Measures text with the given graphics context, font, string, location, and anti-aliasing flag.
        /// </summary>
        /// <param name="g">The Graphics context to measure for.</param>
        /// <param name="font">The Font to measure with.</param>
        /// <param name="text">The string of text to measure.</param>
        /// <param name="antiAliasing">Whether the font should be rendered with anti-aliasing.</param>
        public static Size MeasureString(Graphics g, Font font, string text, bool antiAliasing, FontSmoothing fontSmoothing)
        {
            if (fontSmoothing == FontSmoothing.Smooth && antiAliasing)
            {
                PixelOffsetMode oldPOM = g.PixelOffsetMode;
                g.PixelOffsetMode = PixelOffsetMode.Half;

                TextRenderingHint oldTRH = g.TextRenderingHint;
                g.TextRenderingHint = TextRenderingHint.AntiAlias;

                StringFormat format = (StringFormat)StringFormat.GenericTypographic.Clone();
                format.FormatFlags |= StringFormatFlags.MeasureTrailingSpaces;

                SizeF sf = g.MeasureString(text, font, new PointF(0, 0), format);
                sf.Height = font.GetHeight();

                g.PixelOffsetMode   = oldPOM;
                g.TextRenderingHint = oldTRH;
                return(Size.Ceiling(sf));
            }
            else if (fontSmoothing == FontSmoothing.Sharp || !antiAliasing)
            {
                IntPtr hdc        = IntPtr.Zero;
                IntPtr hFont      = IntPtr.Zero;
                IntPtr hOldObject = IntPtr.Zero;

                try
                {
                    hdc        = g.GetHdc();
                    hFont      = CreateFontObject(font, antiAliasing);
                    hOldObject = SafeNativeMethods.SelectObject(hdc, hFont);

                    NativeStructs.RECT rect = new NativeStructs.RECT();
                    rect.left   = 0;
                    rect.top    = 0;
                    rect.right  = rect.left;
                    rect.bottom = rect.top;

                    int result = SafeNativeMethods.DrawTextW(
                        hdc,
                        text,
                        text.Length,
                        ref rect,
                        NativeConstants.DT_CALCRECT |
                        NativeConstants.DT_LEFT |
                        NativeConstants.DT_NOCLIP |
                        NativeConstants.DT_NOPREFIX |
                        NativeConstants.DT_SINGLELINE |
                        NativeConstants.DT_TOP);

                    if (result == 0)
                    {
                        NativeMethods.ThrowOnWin32Error("DrawTextW returned 0");
                    }

                    return(new Size(rect.right - rect.left, rect.bottom - rect.top));
                }

                finally
                {
                    if (hOldObject != IntPtr.Zero)
                    {
                        SafeNativeMethods.SelectObject(hdc, hOldObject);
                        hOldObject = IntPtr.Zero;
                    }

                    if (hFont != IntPtr.Zero)
                    {
                        SafeNativeMethods.DeleteObject(hFont);
                        hFont = IntPtr.Zero;
                    }

                    if (hdc != IntPtr.Zero)
                    {
                        g.ReleaseHdc(hdc);
                        hdc = IntPtr.Zero;
                    }
                }
            }
            else
            {
                throw new InvalidEnumArgumentException("fontSmoothing = " + (int)fontSmoothing);
            }
        }
Beispiel #3
0
 internal static extern int DrawTextW(
     IntPtr hdc,
     string lpString,
     int nCount,
     ref NativeStructs.RECT lpRect,
     uint uFormat);