public static Size Measure(Graphics gr, string text, Font font, Rectangle rect) { Tenor.Mobile.NativeMethods.RECT bounds = new Tenor.Mobile.NativeMethods.RECT(); bounds.left = rect.Left; bounds.right = rect.Right; bounds.bottom = rect.Bottom; bounds.right = rect.Right; IntPtr hFont = font.ToHfont(); IntPtr hdc = gr.GetHdc(); IntPtr originalObject = NativeMethods.SelectObject(hdc, hFont); int flags = NativeMethods.DT_CALCRECT | NativeMethods.DT_WORDBREAK; NativeMethods.DrawText(hdc, text, text.Length, ref bounds, flags); NativeMethods.SelectObject(hdc, originalObject); gr.ReleaseHdc(hdc); return(new Size(bounds.right - bounds.left, bounds.bottom - bounds.top)); }
/// <summary> /// Draws an image with alpha blending. /// </summary> private void DrawImageGdi(Graphics g, Rectangle destination) { if (g == null) { throw new ArgumentNullException("g"); } if (destination.IsEmpty) { throw new ArgumentException("The destination rectangle cannot be empty.", "destination"); } IntPtr hdc = g.GetHdc(); try { Tenor.Mobile.NativeMethods.RECT rect = new Tenor.Mobile.NativeMethods.RECT() { left = destination.X, top = destination.Y, right = destination.Right, bottom = destination.Bottom }; iimage.Draw(hdc, ref rect, IntPtr.Zero); } catch (COMException ex) { throw new Exception("Unable to draw transparent image.", ex); } catch { throw; } finally { g.ReleaseHdc(hdc); } }
internal static extern int DrawText(IntPtr hdc, string lpStr, int nCount, ref Tenor.Mobile.NativeMethods.RECT lpRect, int wFormat);