Exemplo n.º 1
0
        /// <summary>
        /// Measures the specified string when drawn with the specified <see cref="OpenNETCF.Drawing.FontEx"/> object.
        /// </summary>
        /// <param name="text"><see cref="String"/> to measure.</param>
        /// <param name="font"><see cref="OpenNETCF.Drawing.FontEx"/> object that defines the text format of the string.</param>
        /// <returns></returns>
        public SizeF MeasureString(string text, FontEx font)
        {
            GDIPlus.SIZE sz       = new GDIPlus.SIZE();
            int          fit      = 0;
            IntPtr       hOldFont = GDIPlus.SelectObject(hDC, font.hFont);
            int          width    = 100;

            GDIPlus.GetTextExtentExPoint(hDC, text, text.Length, width, out fit, null, ref sz);
            GDIPlus.SelectObject(hDC, hOldFont);
            return(new SizeF(sz.width, sz.height));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Measures the specified string when drawn with the specified <see cref="FontEx"/> object.
        /// </summary>
        /// <param name="text">String to measure.</param>
        /// <param name="font"><see cref="FontEx"/> object that defines the text format of the string.</param>
        /// <param name="width">Width to fit the string.</param>
        /// <returns></returns>
        public SizeF MeasureString(string text, FontEx font, int width)
        {
            GDIPlus.SIZE sz      = new GDIPlus.SIZE();
            IntPtr       hdcTemp = GDIPlus.CreateCompatibleDC(hDC);
            IntPtr       oldFont = GDIPlus.SelectObject(hdcTemp, font.hFont);

            GDIPlus.RECT rc = new GDIPlus.RECT();
            rc.right  = width;
            rc.bottom = 320;

            int height = GDIPlus.DrawText(hdcTemp, text, text.Length, ref rc,
                                          GDIPlus.DT_LEFT | GDIPlus.DT_TOP | GDIPlus.DT_WORDBREAK | GDIPlus.DT_CALCRECT);

            GDIPlus.SelectObject(hdcTemp, oldFont);
            GDIPlus.DeleteDC(hdcTemp);
            return(new SizeF(width, height));
        }