internal int DrawText(IntPtr hDC, IntPtr hFont, string sText, int nLen, ref Rectangle rect, uint uFormat, Color foreColor, Color backColor) { IntPtr hOldFont = GDIPlus.SelectObject(hDC, hFont); GDIPlus.SetTextColor(hDC, ColorTranslator.ToWin32(foreColor) /*(int)GDIPlus.RGB(foreColor)*/); GDIPlus.SetBkColor(hDC, ColorTranslator.ToWin32(backColor) /*(int)GDIPlus.RGB(backColor)*/); if (backColor == Color.Empty) { GDIPlus.SetBkMode(hDC, GDIPlus.TRANSPARENT); } else { GDIPlus.SetBkMode(hDC, GDIPlus.OPAQUE); } GDIPlus.RECT rc = new GDIPlus.RECT(); rc.left = rect.Left; rc.top = rect.Top; rc.right = rect.Right; rc.bottom = rect.Bottom; //int nRet = GDIPlus.DrawText(hDC, sText, nLen, ref rc, uFormat); int nRet = GDIPlus.ExtTextOut(hDC, rc.left, rc.top, uFormat, ref rc, sText, sText.Length, null); GDIPlus.SelectObject(hDC, hOldFont); return(nRet); }
/// <summary> /// Copies graphics from <see cref="Control"/> with transparent color. /// </summary> /// <param name="ctl">Control to copy graphics from</param> /// <param name="rc">Rectangle to copy.</param> /// <param name="transpColor">Transaprent color.</param> public void CopyGraphics(Control ctl, Rectangle rc, Color transpColor) { IntPtr hwnd = GetControlHandle(ctl); IntPtr hdcSrc = GDIPlus.GetDC(hwnd); GDIPlus.RECT srcRect = new GDIPlus.RECT(); GDIPlus.GetWindowRect(hwnd, ref srcRect); GDIPlus.TransparentImage(hDC, rc.Left, rc.Top, rc.Width, rc.Height, hdcSrc, 0, 0, srcRect.right - srcRect.left, srcRect.bottom - srcRect.top, ColorTranslator.ToWin32(transpColor) /*(int)GDIPlus.RGB(transpColor)*/); GDIPlus.ReleaseDC(hwnd, hdcSrc); }
/// <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)); }