Ejemplo n.º 1
0
        // GetTextExtent wrapper
        public static Size GetTextExtent(Graphics graphics, string text, Font font)
        {
            IntPtr hDC      = graphics.GetHdc();
            IntPtr hFont    = font.ToHfont();
            IntPtr oldHFont = SelectObject(hDC, hFont);
            SIZE   size     = new SIZE();

            GetTextExtentPoint32A(hDC, text, text.Length, ref size);
            int width = size.Width;

            // Add the overhang for italic and bold fonts
            if ((font.Bold || font.Italic) && (text.Length > 0))
            {
                Win32Calls.ABCFLOAT[] WidthsABC = new Win32Calls.ABCFLOAT[1];
                uint code = text[text.Length - 1];
                Win32Calls.GetCharABCWidthsFloat(hDC, code, code, WidthsABC);
                double dOverhangTrailing = WidthsABC[0].abcfC;
                if (dOverhangTrailing < 0)
                {
                    width -= (int)dOverhangTrailing;
                }
            }

            SelectObject(hDC, oldHFont);
            DeleteObject(hFont);
            graphics.ReleaseHdc(hDC);

            return(new Size(width, size.Height));
        }
Ejemplo n.º 2
0
        // GetTextExtent wrapper
        public static Size GetTextExtent(Graphics graphics, string text, Font font)
        {
            IntPtr hDC = graphics.GetHdc();
            IntPtr hFont = font.ToHfont();
            IntPtr oldHFont = SelectObject(hDC, hFont);
            SIZE size = new SIZE();
            GetTextExtentPoint32A(hDC, text, text.Length, ref size);
            int width = size.Width;

            // Add the overhang for italic and bold fonts
            if ((font.Bold || font.Italic) && (text.Length > 0))
            {
                Win32Calls.ABCFLOAT[] WidthsABC = new Win32Calls.ABCFLOAT[1];
                uint code = text[text.Length - 1];
                Win32Calls.GetCharABCWidthsFloat(hDC, code, code, WidthsABC);
                double dOverhangTrailing = WidthsABC[0].abcfC;
                if (dOverhangTrailing < 0)
                    width -= (int)dOverhangTrailing;
            }

            SelectObject(hDC, oldHFont);
            DeleteObject(hFont);
            graphics.ReleaseHdc(hDC);

            return new Size(width, size.Height);
        }