Пример #1
0
        /// <summary>
        /// Calculates the rectangle needed for rendering the text string.
        /// </summary>
        /// <param name="control">Control to which the text will be rendered.
        /// It's used for getting the device context and setting the initial text bounds
        /// (the latter is ignored in the single-line case).</param>
        /// <param name="text">Text to be rendered.</param>
        /// <param name="font">Font in which the text will be rendered.</param>
        /// <param name="bounds">Initial size that should be expanded to fit the text.</param>
        /// <param name="dtf">Additional parameters that control rendering of the text.</param>
        /// <returns>Size of the text's bounding rectangle.</returns>
        public static Size GetTextSize(Control control, string text, Font font, Size bounds, DrawTextFormatFlags dtf)
        {
            using (Graphics g = control.CreateGraphics())
            {
                IntPtr hdc = g.GetHdc();
                try
                {
                    IntPtr hFont   = _fontCache.GetHFont(font);
                    IntPtr oldFont = Win32Declarations.SelectObject(hdc, hFont);
                    RECT   rc      = new RECT(0, 0, bounds.Width, bounds.Height);

                    Win32Declarations.DrawText(hdc, text, text.Length, ref rc,
                                               dtf | DrawTextFormatFlags.DT_CALCRECT);

                    int height = rc.bottom - rc.top;
                    //height += 1;
                    Size sz = new Size(rc.right - rc.left, height);

                    Win32Declarations.SelectObject(hdc, oldFont);

                    return(sz);
                }
                finally
                {
                    g.ReleaseHdc(hdc);
                }
            }
        }
Пример #2
0
        public int DrawText(Graphics g, string text, Font font, Color color, Rectangle rc, StringFormat format)
        {
            int        height;
            RectangleF rcClip = g.ClipBounds;
            RECT       rect   = new RECT(rc.Left, rc.Top, rc.Right, rc.Bottom);
            IntPtr     hdc    = g.GetHdc();

            try
            {
                IntPtr clipRgn = Win32Declarations.CreateRectRgn(0, 0, 0, 0);
                if (Win32Declarations.GetClipRgn(hdc, clipRgn) != 1)
                {
                    Win32Declarations.DeleteObject(clipRgn);
                    clipRgn = IntPtr.Zero;
                }
                Win32Declarations.IntersectClipRect(hdc, (int)rcClip.Left, (int)rcClip.Top, (int)rcClip.Right, (int)rcClip.Bottom);

                IntPtr         hFont     = _fontCache.GetHFont(font);
                IntPtr         oldFont   = Win32Declarations.SelectObject(hdc, hFont);
                int            textColor = Win32Declarations.ColorToRGB(color);
                int            oldColor  = Win32Declarations.SetTextColor(hdc, textColor);
                BackgroundMode oldMode   = Win32Declarations.SetBkMode(hdc, BackgroundMode.TRANSPARENT);

                DrawTextFormatFlags flags = 0;
                if ((format.FormatFlags & StringFormatFlags.NoWrap) != 0)
                {
                    flags |= DrawTextFormatFlags.DT_SINGLELINE;
                }
                else
                {
                    flags |= DrawTextFormatFlags.DT_WORDBREAK;
                }
                if (format.Alignment == StringAlignment.Center)
                {
                    flags |= DrawTextFormatFlags.DT_CENTER;
                }
                else if (format.Alignment == StringAlignment.Far)
                {
                    flags |= DrawTextFormatFlags.DT_RIGHT;
                }

                if (format.LineAlignment == StringAlignment.Center)
                {
                    flags |= DrawTextFormatFlags.DT_VCENTER;
                }
                if (format.Trimming == StringTrimming.EllipsisCharacter)
                {
                    flags |= DrawTextFormatFlags.DT_END_ELLIPSIS;
                }
                if (format.HotkeyPrefix == HotkeyPrefix.None)
                {
                    flags |= DrawTextFormatFlags.DT_NOPREFIX;
                }

                height = Win32Declarations.DrawText(hdc, text, text.Length, ref rect, flags);

                Win32Declarations.SelectClipRgn(hdc, clipRgn);
                Win32Declarations.DeleteObject(clipRgn);

                Win32Declarations.SetBkMode(hdc, oldMode);
                Win32Declarations.SetTextColor(hdc, oldColor);
                Win32Declarations.SelectObject(hdc, oldFont);
            }
            finally
            {
                g.ReleaseHdc(hdc);
            }
            return(height);
        }