public unsafe void DrawLine(WindowsPen pen, int x1, int y1, int x2, int y2) { HandleRef hdc = new HandleRef(DeviceContext, DeviceContext.Hdc); Gdi32.R2 rasterOp = DeviceContext.BinaryRasterOperation; Gdi32.BKMODE bckMode = DeviceContext.BackgroundMode; if (rasterOp != Gdi32.R2.COPYPEN) { rasterOp = DeviceContext.SetRasterOperation(Gdi32.R2.COPYPEN); } if (bckMode != Gdi32.BKMODE.TRANSPARENT) { bckMode = DeviceContext.SetBackgroundMode(Gdi32.BKMODE.TRANSPARENT); } if (pen != null) { DeviceContext.SelectObject(pen.HPen, GdiObjectType.Pen); } Point oldPoint = new Point(); IntUnsafeNativeMethods.MoveToEx(hdc, x1, y1, &oldPoint); IntUnsafeNativeMethods.LineTo(hdc, x2, y2); if (bckMode != Gdi32.BKMODE.TRANSPARENT) { DeviceContext.SetBackgroundMode(bckMode); } if (rasterOp != Gdi32.R2.COPYPEN) { DeviceContext.SetRasterOperation(rasterOp); } IntUnsafeNativeMethods.MoveToEx(hdc, oldPoint.X, oldPoint.Y, &oldPoint); }
public unsafe void DrawLine(WindowsPen pen, int x1, int y1, int x2, int y2) { HandleRef hdc = new HandleRef(DeviceContext, DeviceContext.Hdc); DeviceContextBinaryRasterOperationFlags rasterOp = DeviceContext.BinaryRasterOperation; DeviceContextBackgroundMode bckMode = DeviceContext.BackgroundMode; if (rasterOp != DeviceContextBinaryRasterOperationFlags.CopyPen) { rasterOp = DeviceContext.SetRasterOperation(DeviceContextBinaryRasterOperationFlags.CopyPen); } if (bckMode != DeviceContextBackgroundMode.Transparent) { bckMode = DeviceContext.SetBackgroundMode(DeviceContextBackgroundMode.Transparent); } if (pen != null) { DeviceContext.SelectObject(pen.HPen, GdiObjectType.Pen); } Point oldPoint = new Point(); IntUnsafeNativeMethods.MoveToEx(hdc, x1, y1, &oldPoint); IntUnsafeNativeMethods.LineTo(hdc, x2, y2); if (bckMode != DeviceContextBackgroundMode.Transparent) { DeviceContext.SetBackgroundMode(bckMode); } if (rasterOp != DeviceContextBinaryRasterOperationFlags.CopyPen) { DeviceContext.SetRasterOperation(rasterOp); } IntUnsafeNativeMethods.MoveToEx(hdc, oldPoint.X, oldPoint.Y, &oldPoint); }
/// <summary> /// Draws the text in the given bounds, using the given Font, foreColor and backColor, and according to the specified /// TextFormatFlags flags. /// /// If font is null, the font currently selected in the hdc is used. /// /// If foreColor and/or backColor are Color.Empty, the hdc current text and/or background color are used. /// </summary> public void DrawText(string text, WindowsFont font, Rectangle bounds, Color foreColor, Color backColor, User32.DT flags) { if (string.IsNullOrEmpty(text) || foreColor == Color.Transparent) { return; } Debug.Assert(((uint)flags & GdiUnsupportedFlagMask) == 0, "Some custom flags were left over and are not GDI compliant!"); Debug.Assert((flags & User32.DT.CALCRECT) == 0, "CALCRECT flag is set, text won't be drawn"); HandleRef hdc = new HandleRef(DeviceContext, DeviceContext.Hdc); // DrawText requires default text alignment. if (DeviceContext.TextAlignment != default) { DeviceContext.TextAlignment = default; } // color empty means use the one currently selected in the dc. if (!foreColor.IsEmpty && foreColor != DeviceContext.TextColor) { DeviceContext.TextColor = foreColor; } if (font != null) { DeviceContext.SelectFont(font); } Gdi32.BKMODE newBackGndMode = (backColor.IsEmpty || backColor == Color.Transparent) ? Gdi32.BKMODE.TRANSPARENT : Gdi32.BKMODE.OPAQUE; if (DeviceContext.BackgroundMode != newBackGndMode) { DeviceContext.SetBackgroundMode(newBackGndMode); } if (newBackGndMode != Gdi32.BKMODE.TRANSPARENT && backColor != DeviceContext.BackgroundColor) { DeviceContext.BackgroundColor = backColor; } User32.DRAWTEXTPARAMS dtparams = GetTextMargins(font); bounds = AdjustForVerticalAlignment(hdc, text, bounds, flags, ref dtparams); // Adjust unbounded rect to avoid overflow since Rectangle ctr does not do param validation. if (bounds.Width == MaxSize.Width) { bounds.Width -= bounds.X; } if (bounds.Height == MaxSize.Height) { bounds.Height -= bounds.Y; } var rect = new RECT(bounds); User32.DrawTextExW(hdc, text, text.Length, ref rect, flags, ref dtparams); // No need to restore previous objects into the dc (see comments on top of the class). }