public void DrawLine(WindowsPen pen, int x1, int y1, int x2, int y2) { HandleRef hdc = new HandleRef(this.dc, this.dc.Hdc); DeviceContextBinaryRasterOperationFlags binaryRasterOperation = this.dc.BinaryRasterOperation; DeviceContextBackgroundMode backgroundMode = this.dc.BackgroundMode; if (binaryRasterOperation != DeviceContextBinaryRasterOperationFlags.CopyPen) { binaryRasterOperation = this.dc.SetRasterOperation(DeviceContextBinaryRasterOperationFlags.CopyPen); } if (backgroundMode != DeviceContextBackgroundMode.Transparent) { backgroundMode = this.dc.SetBackgroundMode(DeviceContextBackgroundMode.Transparent); } if (pen != null) { this.dc.SelectObject(pen.HPen, GdiObjectType.Pen); } IntNativeMethods.POINT pt = new IntNativeMethods.POINT(); IntUnsafeNativeMethods.MoveToEx(hdc, x1, y1, pt); IntUnsafeNativeMethods.LineTo(hdc, x2, y2); if (backgroundMode != DeviceContextBackgroundMode.Transparent) { this.dc.SetBackgroundMode(backgroundMode); } if (binaryRasterOperation != DeviceContextBinaryRasterOperationFlags.CopyPen) { this.dc.SetRasterOperation(binaryRasterOperation); } IntUnsafeNativeMethods.MoveToEx(hdc, pt.x, pt.y, null); }
public void DrawText(string text, WindowsFont font, Rectangle bounds, Color foreColor, Color backColor, IntTextFormatFlags flags) { if (!string.IsNullOrEmpty(text) && (foreColor != Color.Transparent)) { HandleRef hdc = new HandleRef(this.dc, this.dc.Hdc); if (this.dc.TextAlignment != DeviceContextTextAlignment.Top) { this.dc.SetTextAlignment(DeviceContextTextAlignment.Top); } if (!foreColor.IsEmpty && (foreColor != this.dc.TextColor)) { this.dc.SetTextColor(foreColor); } if (font != null) { this.dc.SelectFont(font); } DeviceContextBackgroundMode newMode = (backColor.IsEmpty || (backColor == Color.Transparent)) ? DeviceContextBackgroundMode.Transparent : DeviceContextBackgroundMode.Opaque; if (this.dc.BackgroundMode != newMode) { this.dc.SetBackgroundMode(newMode); } if ((newMode != DeviceContextBackgroundMode.Transparent) && (backColor != this.dc.BackgroundColor)) { this.dc.SetBackgroundColor(backColor); } IntNativeMethods.DRAWTEXTPARAMS textMargins = this.GetTextMargins(font); bounds = AdjustForVerticalAlignment(hdc, text, bounds, flags, textMargins); if (bounds.Width == MaxSize.Width) { bounds.Width -= bounds.X; } if (bounds.Height == MaxSize.Height) { bounds.Height -= bounds.Y; } IntNativeMethods.RECT lpRect = new IntNativeMethods.RECT(bounds); IntUnsafeNativeMethods.DrawTextEx(hdc, text, ref lpRect, (int)flags, textMargins); } }
public unsafe void DrawLine(WindowsPen pen, int x1, int y1, int x2, int y2) { HandleRef hdc = new HandleRef(dc, dc.Hdc); DeviceContextBinaryRasterOperationFlags rasterOp = dc.BinaryRasterOperation; DeviceContextBackgroundMode bckMode = dc.BackgroundMode; if (rasterOp != DeviceContextBinaryRasterOperationFlags.CopyPen) { rasterOp = dc.SetRasterOperation(DeviceContextBinaryRasterOperationFlags.CopyPen); } if (bckMode != DeviceContextBackgroundMode.Transparent) { bckMode = dc.SetBackgroundMode(DeviceContextBackgroundMode.Transparent); } if (pen != null) { dc.SelectObject(pen.HPen, GdiObjectType.Pen); } Point oldPoint = new Point(); IntUnsafeNativeMethods.MoveToEx(hdc, x1, y1, &oldPoint); IntUnsafeNativeMethods.LineTo(hdc, x2, y2); if (bckMode != DeviceContextBackgroundMode.Transparent) { dc.SetBackgroundMode(bckMode); } if (rasterOp != DeviceContextBinaryRasterOperationFlags.CopyPen) { dc.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.TextFormatFlags 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.TextFormatFlags.DT_CALCRECT) == 0, "DT_CALCRECT flag is set, text won't be drawn"); HandleRef hdc = new HandleRef(dc, dc.Hdc); // DrawText requires default text alignment. if (dc.TextAlignment != DeviceContextTextAlignment.Default) { dc.SetTextAlignment(DeviceContextTextAlignment.Default); } // color empty means use the one currently selected in the dc. if (!foreColor.IsEmpty && foreColor != dc.TextColor) { dc.SetTextColor(foreColor); } if (font != null) { dc.SelectFont(font); } DeviceContextBackgroundMode newBackGndMode = (backColor.IsEmpty || backColor == Color.Transparent) ? DeviceContextBackgroundMode.Transparent : DeviceContextBackgroundMode.Opaque; if (dc.BackgroundMode != newBackGndMode) { dc.SetBackgroundMode(newBackGndMode); } if (newBackGndMode != DeviceContextBackgroundMode.Transparent && backColor != dc.BackgroundColor) { dc.SetBackgroundColor(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). }
/// <devdoc> /// Sets the DC background mode and returns the old value. /// </devdoc> public DeviceContextBackgroundMode SetBackgroundMode(DeviceContextBackgroundMode newMode) { return((DeviceContextBackgroundMode)IntUnsafeNativeMethods.SetBkMode(new HandleRef(this, this.Hdc), (int)newMode)); }
/// <devdoc> /// 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. /// </devdoc> public void DrawText(string text, WindowsFont font, Rectangle bounds, Color foreColor, Color backColor, IntTextFormatFlags 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 & IntTextFormatFlags.CalculateRectangle) == 0, "CalculateRectangle flag is set, text won't be drawn"); HandleRef hdc = new HandleRef(this.dc, this.dc.Hdc); // DrawText requires default text alignment. if (this.dc.TextAlignment != DeviceContextTextAlignment.Default) { this.dc.SetTextAlignment(DeviceContextTextAlignment.Default); } // color empty means use the one currently selected in the dc. if (!foreColor.IsEmpty && foreColor != this.dc.TextColor) { this.dc.SetTextColor(foreColor); } if (font != null) { this.dc.SelectFont(font); } DeviceContextBackgroundMode newBackGndMode = (backColor.IsEmpty || backColor == Color.Transparent) ? DeviceContextBackgroundMode.Transparent : DeviceContextBackgroundMode.Opaque; if (this.dc.BackgroundMode != newBackGndMode) { this.dc.SetBackgroundMode(newBackGndMode); } if (newBackGndMode != DeviceContextBackgroundMode.Transparent && backColor != this.dc.BackgroundColor) { this.dc.SetBackgroundColor(backColor); } IntNativeMethods.DRAWTEXTPARAMS dtparams = GetTextMargins(font); bounds = AdjustForVerticalAlignment(hdc, text, bounds, flags, dtparams); // Adjust unbounded rect to avoid overflow since Rectangle ctr does not do param validation. if (bounds.Width == MaxSize.Width) { bounds.Width = bounds.Width - bounds.X; } if (bounds.Height == MaxSize.Height) { bounds.Height = bounds.Height - bounds.Y; } IntNativeMethods.RECT rect = new IntNativeMethods.RECT(bounds); IntUnsafeNativeMethods.DrawTextEx(hdc, text, ref rect, (int)flags, dtparams); /* No need to restore previous objects into the dc (see comments on top of the class). * * if (hOldFont != IntPtr.Zero) * { * IntUnsafeNativeMethods.SelectObject(hdc, new HandleRef( null, hOldFont)); * } * * if( foreColor != textColor ) * { * this.dc.SetTextColor(textColor); * } * * if( backColor != bkColor ) * { * this.dc.SetBackgroundColor(bkColor); * } * * if( bckMode != newMode ) * { * this.dc.SetBackgroundMode(bckMode); * } * * if( align != DeviceContextTextAlignment.Default ) * { * // Default text alignment required by DrewText. * this.dc.SetTextAlignment(align); * } */ }
public DeviceContextBackgroundMode SetBackgroundMode(DeviceContextBackgroundMode newMode) { return (DeviceContextBackgroundMode) IntUnsafeNativeMethods.SetBkMode(new HandleRef(this, this.Hdc), (int) newMode); }