/// <summary> /// Measure the width and height of string <paramref name="str"/> when drawn on device context HDC /// using the given font <paramref name="font"/>.<br/> /// Restrict the width of the string and get the number of characters able to fit in the restriction and /// the width those characters take. /// </summary> /// <param name="str">the string to measure</param> /// <param name="font">the font to measure string with</param> /// <param name="maxWidth">the max width to render the string in</param> /// <param name="charFit">the number of characters that will fit under <see cref="maxWidth"/> restriction</param> /// <param name="charFitWidth"></param> /// <returns>the size of the string</returns> public static PixelFarm.Drawing.Size MeasureString( char[] buff, int startAt, int len, RequestFont font, float maxWidth, out int charFit, out int charFitWidth) { SetFont(font); if (buff.Length == 0) { charFit = 0; charFitWidth = 0; return(PixelFarm.Drawing.Size.Empty); } var size = new Win32.Size(); //win32 unsafe { fixed(char *startAddr = &buff[0]) { NativeTextWin32.UnsafeGetTextExtentExPoint( s_win32MemDc.DC, startAddr + startAt, len, (int)Math.Round(maxWidth), s_charFit, s_charFitWidth, ref size); } } charFit = s_charFit[0]; charFitWidth = charFit > 0 ? s_charFitWidth[charFit - 1] : 0; return(new PixelFarm.Drawing.Size(size.W, size.H)); //} }
public static PixelFarm.Drawing.Size MeasureString(char[] buff, int startAt, int len, RequestFont font) { SetFont(font); Win32.Size win32_size = new Win32.Size(); if (buff.Length > 0) { unsafe { fixed(char *startAddr = &buff[0]) { NativeTextWin32.UnsafeGetTextExtentPoint32(s_win32MemDc.DC, startAddr + startAt, len, ref win32_size); } } } return(new PixelFarm.Drawing.Size(win32_size.W, win32_size.H)); }