private void InitializeGraphics() { // get the size in both pixels and millimeters and use them to calibrate the unit converter // which will be used by other components when they need to convert units. Location size = MapToWorld(ClientRectangle.Width, ClientRectangle.Height); UnitsConverter.Calibrate(new Location(ClientRectangle.Width, ClientRectangle.Height), size); // create grid bitmap CreateGridBitmap(); // create final bitmap _finalBitmap = new Bitmap(ClientRectangle.Width, ClientRectangle.Height, PixelFormat.Format32bppArgb); _liPoints.DrawingArea = _drawingArea; _currentPointClicked = PointF.Empty; _selectedPoint = -1; // this line is necessary to prevent flicker if (!this.DesignMode) // noticed that in design mode we need to erase background or // things will be overwritten and get messy! { SetStyle(ControlStyles.Opaque, true); } }
private void UserControl1_MouseMove(object sender, MouseEventArgs e) { if (_selectedPoint >= 0) { PointF to = new PointF(UnitsConverter.Pixel2MM(e.X), UnitsConverter.Pixel2MM(e.Y)); if (_drawingArea.Contains(to) && _liPoints.IsPointTrapped(to, _selectedPoint)) { _liPoints.MovePoint(_selectedPoint, to); Invalidate(); NotifyListeners(); } } // moving the mouse cancels the add point functionality _currentPointClicked = PointF.Empty; }
private void InitializeGraphics() { // get the size in both pixels and millimeters and use them to calibrate the unit converter // which will be used by other components when they need to convert units. Location size = MapToWorld(ClientRectangle.Width, ClientRectangle.Height); UnitsConverter.Calibrate(new Location(ClientRectangle.Width, ClientRectangle.Height), size); // create grid bitmap CreateGridBitmap(); // this line is necessary to prevent flicker if (!this.DesignMode) // noticed that in design mode we need to erase background or // things will be overwritten and get messy! { SetStyle(ControlStyles.Opaque, true); } }
private void DrawGraph() { if (DataAvailable) { Graphics g = Graphics.FromImage(_gridBitmap); g.PageUnit = GraphicsUnit.Pixel; int xo = UnitsConverter.MM2Pixel(_drawingArea.Left); int yo = UnitsConverter.MM2Pixel(_drawingArea.Bottom); for (int i = 0; i < _xVector.Length; i++) { int x = (int)((_xVector[i] - _xVector[0]) * _xScale); if (_yVector[i] > 0) // binned histogram might have zero frequencies { int y = (int)(_yScale * Math.Log10(_yVector[i])); g.DrawLine(new Pen(ForeColor), xo + x, yo, xo + x, yo - y); } } } }
private void UserControl1_MouseDown(object sender, MouseEventArgs e) { PointF p = new PointF(UnitsConverter.Pixel2MM(e.X), UnitsConverter.Pixel2MM(e.Y)); if (e.Button == MouseButtons.Left) { _selectedPoint = _liPoints.FindPoint(p); if (_selectedPoint < 0) { // we didn't click an already existing point, we want to create new one _currentPointClicked = p; } } else if (e.Button == MouseButtons.Right) { _liPoints.RemovePoint(p); Invalidate(); NotifyListeners(); } }
private void CreateGridBitmap() { float axesOffset = 5F; // create the bitmap object _gridBitmap = new Bitmap(ClientRectangle.Width, ClientRectangle.Height, PixelFormat.Format32bppArgb); Graphics g = Graphics.FromImage(_gridBitmap); g.PageUnit = GraphicsUnit.Millimeter; // clear its background Brush br = new SolidBrush(BackColor); g.FillRectangle(br, 0F, 0F, _gridBitmap.Width, _gridBitmap.Height); _origin = new PointF(axesOffset, UnitsConverter.Pixel2MM(ClientRectangle.Bottom) - axesOffset); _drawingArea = new RectangleF(axesOffset, 0, UnitsConverter.Pixel2MM(ClientRectangle.Width) - axesOffset, UnitsConverter.Pixel2MM(ClientRectangle.Height) - axesOffset); // draw 2 axes Pen axisPen = new Pen(AxisColor, AxisThickness); g.DrawLine(Pens.Black, UnitsConverter.Pixel2MM(ClientRectangle.Left), UnitsConverter.Pixel2MM(ClientRectangle.Bottom) - axesOffset + AxisThickness, UnitsConverter.Pixel2MM(ClientRectangle.Right), UnitsConverter.Pixel2MM(ClientRectangle.Bottom) - axesOffset + AxisThickness); g.DrawLine(Pens.Black, UnitsConverter.Pixel2MM(ClientRectangle.Left) + axesOffset - AxisThickness, UnitsConverter.Pixel2MM(ClientRectangle.Top), UnitsConverter.Pixel2MM(ClientRectangle.Left) + axesOffset - AxisThickness, UnitsConverter.Pixel2MM(ClientRectangle.Bottom)); // draw grid Pen p = new Pen(GridColor, GridLineThickness); float x = axesOffset; while (x <= _drawingArea.Right) { x += GridSpacing; g.DrawLine(p, x, 0, x, _gridBitmap.Height); } float y = AxisThickness; while (y < _drawingArea.Bottom) { y += GridSpacing; g.DrawLine(p, 0, y, _drawingArea.Right, y); } }
private void CreateGridBitmap() { float axesOffset = _drawAxes ? _axesOffset : 0; // create the bitmap object _gridBitmap = new Bitmap(ClientRectangle.Width, ClientRectangle.Height, PixelFormat.Format32bppArgb); Graphics g = Graphics.FromImage(_gridBitmap); g.PageUnit = GraphicsUnit.Millimeter; // clear its background Brush br = new SolidBrush(BackColor); g.FillRectangle(br, 0F, 0F, _gridBitmap.Width, _gridBitmap.Height); _origin = new PointF(axesOffset, UnitsConverter.Pixel2MM(ClientRectangle.Bottom) - axesOffset); // I'm subtracting 1 from width as the 3d frame of the control overrites the last pixel (odd! it shouldn't be part of the client area // but this what i noticed). This issue started to show up once i changed to 3D frame and wasn't these with thin frame. _drawingArea = new RectangleF(axesOffset, 0, UnitsConverter.Pixel2MM(ClientRectangle.Width - 1) - axesOffset, UnitsConverter.Pixel2MM(ClientRectangle.Height) - axesOffset); int axesOffsetPixels = UnitsConverter.MM2Pixel(axesOffset); _drawingAreaPixels = new Rectangle(axesOffsetPixels, 0, ClientRectangle.Width - axesOffsetPixels - 1, ClientRectangle.Height - axesOffsetPixels); // draw 2 axes if (_drawAxes) { Pen axisPen = new Pen(AxisColor, AxisThickness); g.DrawLine(Pens.Black, UnitsConverter.Pixel2MM(ClientRectangle.Left), UnitsConverter.Pixel2MM(ClientRectangle.Bottom) - axesOffset + AxisThickness, UnitsConverter.Pixel2MM(ClientRectangle.Right), UnitsConverter.Pixel2MM(ClientRectangle.Bottom) - axesOffset + AxisThickness); g.DrawLine(Pens.Black, UnitsConverter.Pixel2MM(ClientRectangle.Left) + axesOffset - AxisThickness, UnitsConverter.Pixel2MM(ClientRectangle.Top), UnitsConverter.Pixel2MM(ClientRectangle.Left) + axesOffset - AxisThickness, UnitsConverter.Pixel2MM(ClientRectangle.Bottom)); } // draw grid. I draw starting from axes so I'm sure both axis wil be snapped to the grid Pen p = new Pen(GridColor, GridLineThickness); float x = _drawingArea.Left; while (x <= _drawingArea.Right) { x += GridSpacing; g.DrawLine(p, x, 0, x, _gridBitmap.Height); } if (!_logYScale) { float y = _drawingArea.Bottom; while (y > _drawingArea.Top) { y -= GridSpacing; g.DrawLine(p, 0, y, _drawingArea.Right, y); } } else { float decades = 4; float dy = _drawingArea.Height / decades; for (float yy = _drawingArea.Bottom; yy > _drawingArea.Top; yy -= dy) { for (int i = 1; i <= 10; i++) { float y = yy - (float)Math.Log10(i) * dy; g.DrawLine(p, 0, y, _drawingArea.Right, y); } } } }