private static void DrawPolyLineImpl(Graphics g, Color color, Point[] points) { if (points.Length < 1) { return; } uint nativeColor = (uint)(color.R + (color.G << 8) + (color.B << 16)); IntPtr hdc = IntPtr.Zero; IntPtr pen = IntPtr.Zero; IntPtr oldObject = IntPtr.Zero; try { hdc = g.GetHdc(); pen = SafeNativeMethods.CreatePen(NativeConstants.PS_SOLID, 1, nativeColor); if (pen == IntPtr.Zero) { NativeMethods.ThrowOnWin32Error("CreatePen returned NULL"); } oldObject = SafeNativeMethods.SelectObject(hdc, pen); NativeStructs.POINT pt; bool bResult = SafeNativeMethods.MoveToEx(hdc, points[0].X, points[0].Y, out pt); if (!bResult) { NativeMethods.ThrowOnWin32Error("MoveToEx returned false"); } for (int i = 1; i < points.Length; ++i) { bResult = SafeNativeMethods.LineTo(hdc, points[i].X, points[i].Y); if (!bResult) { NativeMethods.ThrowOnWin32Error("LineTo returned false"); } } } finally { if (oldObject != IntPtr.Zero) { SafeNativeMethods.SelectObject(hdc, oldObject); oldObject = IntPtr.Zero; } if (pen != IntPtr.Zero) { SafeNativeMethods.DeleteObject(pen); pen = IntPtr.Zero; } if (hdc != IntPtr.Zero) { g.ReleaseHdc(hdc); hdc = IntPtr.Zero; } } GC.KeepAlive(g); }
private static void FillRectanglesImpl(Graphics g, Color color, Rectangle[] rects) { uint nativeColor = (uint)(color.R + (color.G << 8) + (color.B << 16)); IntPtr hdc = IntPtr.Zero; IntPtr brush = IntPtr.Zero; IntPtr oldObject = IntPtr.Zero; try { hdc = g.GetHdc(); brush = SafeNativeMethods.CreateSolidBrush(nativeColor); if (brush == IntPtr.Zero) { NativeMethods.ThrowOnWin32Error("CreateSolidBrush returned NULL"); } oldObject = SafeNativeMethods.SelectObject(hdc, brush); foreach (Rectangle rect in rects) { NativeStructs.RECT nativeRect; nativeRect.left = rect.Left; nativeRect.top = rect.Top; nativeRect.right = rect.Right; nativeRect.bottom = rect.Bottom; int result = SafeNativeMethods.FillRect(hdc, ref nativeRect, brush); if (result == 0) { NativeMethods.ThrowOnWin32Error("FillRect returned zero"); } } } finally { if (oldObject != IntPtr.Zero) { SafeNativeMethods.SelectObject(hdc, oldObject); oldObject = IntPtr.Zero; } if (brush != IntPtr.Zero) { SafeNativeMethods.DeleteObject(brush); brush = IntPtr.Zero; } if (hdc != IntPtr.Zero) { g.ReleaseHdc(hdc); hdc = IntPtr.Zero; } } GC.KeepAlive(g); }
/// <summary> /// Measures text with the given graphics context, font, string, location, and anti-aliasing flag. /// </summary> /// <param name="g">The Graphics context to measure for.</param> /// <param name="font">The Font to measure with.</param> /// <param name="text">The string of text to measure.</param> /// <param name="antiAliasing">Whether the font should be rendered with anti-aliasing.</param> public static Size MeasureString(Graphics g, Font font, string text, bool antiAliasing, FontSmoothing fontSmoothing) { if (fontSmoothing == FontSmoothing.Smooth && antiAliasing) { PixelOffsetMode oldPOM = g.PixelOffsetMode; g.PixelOffsetMode = PixelOffsetMode.Half; TextRenderingHint oldTRH = g.TextRenderingHint; g.TextRenderingHint = TextRenderingHint.AntiAlias; StringFormat format = (StringFormat)StringFormat.GenericTypographic.Clone(); format.FormatFlags |= StringFormatFlags.MeasureTrailingSpaces; SizeF sf = g.MeasureString(text, font, new PointF(0, 0), format); sf.Height = font.GetHeight(); g.PixelOffsetMode = oldPOM; g.TextRenderingHint = oldTRH; return(Size.Ceiling(sf)); } else if (fontSmoothing == FontSmoothing.Sharp || !antiAliasing) { IntPtr hdc = IntPtr.Zero; IntPtr hFont = IntPtr.Zero; IntPtr hOldObject = IntPtr.Zero; try { hdc = g.GetHdc(); hFont = CreateFontObject(font, antiAliasing); hOldObject = SafeNativeMethods.SelectObject(hdc, hFont); NativeStructs.RECT rect = new NativeStructs.RECT(); rect.left = 0; rect.top = 0; rect.right = rect.left; rect.bottom = rect.top; int result = SafeNativeMethods.DrawTextW( hdc, text, text.Length, ref rect, NativeConstants.DT_CALCRECT | NativeConstants.DT_LEFT | NativeConstants.DT_NOCLIP | NativeConstants.DT_NOPREFIX | NativeConstants.DT_SINGLELINE | NativeConstants.DT_TOP); if (result == 0) { NativeMethods.ThrowOnWin32Error("DrawTextW returned 0"); } return(new Size(rect.right - rect.left, rect.bottom - rect.top)); } finally { if (hOldObject != IntPtr.Zero) { SafeNativeMethods.SelectObject(hdc, hOldObject); hOldObject = IntPtr.Zero; } if (hFont != IntPtr.Zero) { SafeNativeMethods.DeleteObject(hFont); hFont = IntPtr.Zero; } if (hdc != IntPtr.Zero) { g.ReleaseHdc(hdc); hdc = IntPtr.Zero; } } } else { throw new InvalidEnumArgumentException("fontSmoothing = " + (int)fontSmoothing); } }