public static void DrawLine(Graphics g, Pen pen, int x1, int y1, int x2, int y2) { if (g == null) { throw new ArgumentNullException("g"); } if (pen == null) { throw new ArgumentNullException("pen"); } IntPtr hDC = g.GetHdc(); IntPtr hPen = Gdi32.CreatePen(pen); IntPtr oldBrush = Gdi32.SelectObject(hDC, Gdi32.GetStockObject(WinGdi.NULL_BRUSH)); IntPtr oldPen = Gdi32.SelectObject(hDC, hPen); Gdi32.MoveTo(hDC, x1, y1); Gdi32.LineTo(hDC, x2, y2); Gdi32.SelectObject(hDC, oldBrush); Gdi32.SelectObject(hDC, oldPen); Gdi32.DeleteObject(hPen); g.ReleaseHdc(hDC); }
private unsafe void CreatePen() { if (_width > 1) { // Geometric pen. // From MSDN: if width > 1, the style must be PS_NULL, PS_SOLID, or PS_INSIDEFRAME. _style |= Gdi32.PS.GEOMETRIC | Gdi32.PS.SOLID; } if (_wndBrush == null) { _nativeHandle = Gdi32.CreatePen(_style, _width, ColorTranslator.ToWin32(_color)); } else { var lb = new Gdi32.LOGBRUSH { lbColor = ColorTranslator.ToWin32(_wndBrush.Color), lbStyle = Gdi32.BS.SOLID, lbHatch = IntPtr.Zero }; // Note: We currently don't support custom styles, that's why 0 and null for last two params. _nativeHandle = Gdi32.ExtCreatePen(_style, _width, ref lb, 0, null); } }
/* * DrawGridGdi */ /// <summary> /// Draw the grid overlay using native GDI. /// </summary> /// <param name="dc">Specifies drawing context for this <see cref="T:Genetibase.UI.NuGenPushGraphBar"/>.</param> /// <param name="rect">Specifies the <see cref="T:System.Drawing.Rectangle"/> to draw within.</param> /// <param name="gridColor">Specifies the grid color.</param> /// <param name="gridStep">Specifies the grid step.</param> protected virtual void DrawGridGdi(IntPtr dc, Rectangle rect, Color gridColor, int gridStep) { using (Pen pen = new Pen(gridColor)) { IntPtr hPen = Gdi32.CreatePen(pen); IntPtr hOldPen = Gdi32.SelectObject(dc, hPen); for (int row = rect.Height; row >= 0; row -= gridStep) { Gdi32.MoveTo(dc, rect.Left, row); Gdi32.LineTo(dc, rect.Right, row); } for (int col = rect.Left + this.pushOffset; col < rect.Right; col += gridStep) { if (col < rect.Left) { continue; } Gdi32.MoveTo(dc, col, 0); Gdi32.LineTo(dc, col, rect.Height); } Gdi32.SelectObject(dc, hOldPen); Gdi32.DeleteObject(hPen); } }
static public void DrawXORRectangle(Graphics graphics, Pen pen, Rectangle rectangle) { IntPtr hDC = graphics.GetHdc(); IntPtr hPen = Gdi32.CreatePen(0, (int)pen.Width, ArgbToRGB(pen.Color.ToArgb())); Gdi32.SelectObject(hDC, hPen); Gdi32.SetROP2(hDC, (int)Gdi32.DrawingMode.R2_NOTXORPEN); Gdi32.Rectangle(hDC, rectangle.Left, rectangle.Top, rectangle.Right, rectangle.Bottom); Gdi32.DeleteObject(hPen); graphics.ReleaseHdc(hDC); }
static public void DrawXORLine(Graphics graphics, Pen pen, int x1, int y1, int x2, int y2) { IntPtr hDC = graphics.GetHdc(); IntPtr hPen = Gdi32.CreatePen(0, (int)pen.Width, ArgbToRGB(pen.Color.ToArgb())); Gdi32.SelectObject(hDC, hPen); Gdi32.SetROP2(hDC, (int)Gdi32.DrawingMode.R2_NOTXORPEN); Gdi32.MoveToEx(hDC, x1, y1, ref nullPoint); Gdi32.LineTo(hDC, x2, y2); Gdi32.DeleteObject(hPen); graphics.ReleaseHdc(hDC); }
private void DrawReversibleFrame(IntPtr handle, Rectangle rectangle, Color backColor) { //Bug # 71547 <subhag> to make drag rect visible if any the dimensions of the control are 0 if (rectangle.Width == 0) { rectangle.Width = 5; } if (rectangle.Height == 0) { rectangle.Height = 5; } // Copy of ControlPaint.DrawReversibleFrame, see VSWhidbey 581670 // If ControlPaint ever gets overrloaded, we should replace the code below by calling it: // ControlPaint.DrawReversibleFrame(handle, rectangle, backColor, FrameStyle.Thick); // ------ Duplicate code---------------------------------------------------------- Gdi32.R2 rop2; Color graphicsColor; if (backColor.GetBrightness() < .5) { rop2 = Gdi32.R2.NOTXORPEN; graphicsColor = Color.White; } else { rop2 = Gdi32.R2.XORPEN; graphicsColor = Color.Black; } using var dc = new User32.GetDcScope(handle); using var pen = new Gdi32.ObjectScope(Gdi32.CreatePen(Gdi32.PS.SOLID, 2, ColorTranslator.ToWin32(backColor))); using var rop2Scope = new Gdi32.SetRop2Scope(dc, rop2); using var brushSelection = new Gdi32.SelectObjectScope(dc, Gdi32.GetStockObject(Gdi32.StockObject.NULL_BRUSH)); using var penSelection = new Gdi32.SelectObjectScope(dc, pen); Gdi32.SetBkColor(dc, ColorTranslator.ToWin32(graphicsColor)); Gdi32.Rectangle(dc, rectangle.X, rectangle.Y, rectangle.Right, rectangle.Bottom); // ------ Duplicate code---------------------------------------------------------- }
/* * DrawReversibleRectangle */ /// <summary> /// Draws a reversible rectangle with the specified bounds on the specified <see cref="T:Control"/>. /// </summary> /// <exception cref="ArgumentNullException"> /// <paramref name="controlToDrawOn"/> is <see langword="null"/>. /// </exception> public static void DrawReversibleRectangle(Rectangle reversibleRectangleBounds, Control ctrlToDrawOn) { if (ctrlToDrawOn == null) { throw new ArgumentNullException("ctrlToDrawOn"); } if (!reversibleRectangleBounds.IsEmpty) { IntPtr hDC = User32.GetDCEx(ctrlToDrawOn.Handle, IntPtr.Zero, WinUser.DCX_PARENTCLIP); Gdi32.SetROP2(hDC, WinGdi.R2_XORPEN); IntPtr hPen = Gdi32.CreatePen(WinGdi.PS_DOT, 1, ColorTranslator.ToWin32(Color.Black)); IntPtr oldBrush = Gdi32.SelectObject(hDC, Gdi32.GetStockObject(WinGdi.NULL_BRUSH)); IntPtr oldPen = Gdi32.SelectObject(hDC, hPen); Gdi32.SetBkColor(hDC, ColorTranslator.ToWin32(Color.White)); /* * Do not use Gdi32.Rectangle here to get more regular frame corners. */ Gdi32.MoveTo(hDC, reversibleRectangleBounds.Left, reversibleRectangleBounds.Top); Gdi32.LineTo(hDC, reversibleRectangleBounds.Right, reversibleRectangleBounds.Top); Gdi32.LineTo(hDC, reversibleRectangleBounds.Right, reversibleRectangleBounds.Bottom); Gdi32.LineTo(hDC, reversibleRectangleBounds.Left, reversibleRectangleBounds.Bottom); Gdi32.LineTo(hDC, reversibleRectangleBounds.Left, reversibleRectangleBounds.Top); Gdi32.SetROP2(hDC, WinGdi.R2_NOP); Gdi32.SelectObject(hDC, oldBrush); Gdi32.SelectObject(hDC, oldPen); Gdi32.DeleteObject(hPen); User32.ReleaseDC(ctrlToDrawOn.Handle, hDC); } }
/* * DrawGraphGdi */ /// <summary> /// Draws the graph using native GDI. /// </summary> /// <param name="dc">Specifies drawing context for this <see cref="T:Genetibase.UI.NuGenPushGraphBar"/>.</param> /// <param name="rect">Specifies the <see cref="T:System.Drawing.Rectangle"/> to fit the graph in.</param> protected virtual void DrawGraphGdi(IntPtr dc, Rectangle rect) { int maxPoints = 0; if (this.maxCoords == -1) { /* Maximum push points not yet calculated. */ this.maxCoords = (rect.Width / this.Step) + 2 + ((rect.Width % this.Step > 0) ? 1 : 0); if (this.maxCoords <= 0) { this.maxCoords = 1; } } for (int lineIndex = 0; lineIndex < this.lines.Count; lineIndex++) { if (maxPoints < this.lines[lineIndex].Values.Count) { maxPoints = this.lines[lineIndex].Values.Count; } } if (maxPoints == 0 /* No lines to draw. */) { return; } for (int lineIndex = 0; lineIndex < this.lines.Count; lineIndex++) { /* * If the line has less push points than the line with the greatest * number of push points, new push points are appended with * the same value as the previous push point. If no push points * exist for the line, one is added with the least value possible. */ NuGenGraphLine pushGraphLine = this.lines[lineIndex]; if (pushGraphLine.Values.Count == 0) { pushGraphLine.Values.Add(this.Minimum); } while (pushGraphLine.Values.Count < maxPoints) { pushGraphLine.Values.Add(pushGraphLine.Values[pushGraphLine.Values.Count - 1]); } while (this.lines[lineIndex].Values.Count >= this.maxCoords) { this.lines[lineIndex].Values.RemoveAt(0); } if (this.lines[lineIndex].Values.Count == 0 /* No push points to draw. */) { return; } /* Now prepare to draw the line or bar. */ IntPtr penLine = Gdi32.CreatePen( WinGdi.PS_SOLID, PEN_WIDTH, ColorTranslator.ToWin32(this.lines[lineIndex].LineColor) ); IntPtr hOldPen = Gdi32.SelectObject(dc, penLine); if (pushGraphLine.IsBar) { Gdi32.MoveTo(dc, rect.Left, rect.Height); } else { float initialValue = pushGraphLine.Values[0]; float percent = (float)(rect.Height - PEN_WIDTH) / (this.Maximum - this.Minimum); float relValue = (float)(rect.Height - PEN_WIDTH) - initialValue * percent; Gdi32.MoveTo(dc, (int)rect.Left, (int)(maxPoints == 1 ? rect.Height : relValue)); } for (int valueIndex = 0; valueIndex < pushGraphLine.Values.Count; valueIndex++) { int xOffset = rect.Left + (valueIndex * this.Step); float initialValue = pushGraphLine.Values[valueIndex]; float percent = (float)rect.Height / (float)(this.Maximum - this.Minimum); int relValue = Math.Max(PEN_WIDTH * 2, (int)(rect.Height - initialValue * percent)); if (pushGraphLine.IsBar) { /* Draw a bar. */ Rectangle rectBar = new Rectangle( xOffset, relValue, this.Step, rect.Height ); this.DrawBarGdi(dc, rectBar, pushGraphLine); } else { /* Draw a line. */ Gdi32.LineTo(dc, xOffset, relValue); } } Gdi32.SelectObject(dc, hOldPen); Gdi32.DeleteObject(penLine); } }