C GDI method of drawing on a control

  • 2020-09-28 09:06:18
  • OfStack

This article shows how C# GDI paints on chart controls, using rectangles on them and on forms as examples. Share to everybody for everybody reference. The specific methods are as follows:

Specific implementation methods are not explained, notes are very detailed, the code is very simple.

The main function codes are as follows:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Configuration;

namespace WFApp2
{
  public partial class data : Form
  {
    public data()
    {
      InitializeComponent();
      // The form 
      g = this.CreateGraphics();
      //chart controls 
      g2 = this.chart1.CreateGraphics();
    }
   
    public Point firstPoint = new Point(0, 0); // The mouse first 1 point  
    public Point secondPoint = new Point(0, 0); // The mouse first 2 point  
    public bool begin = false;  // Whether to start drawing rectangles  
    /// <summary>
    ///  in from Draw a rectangular 
    /// </summary>
    Graphics g;

    /// <summary>
    ///  in chart1 Draws a rectangle on a control 
    /// </summary>
    Graphics g2;

    /// <summary>
    ///  Click the mouse event on the form 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void data_MouseDown(object sender, MouseEventArgs e)
    {
      begin = true;
      firstPoint = new Point(e.X, e.Y);
    }

    /// <summary>
    ///  Start drawing by moving the mouse over the form 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void data_MouseMove(object sender, MouseEventArgs e)
    {
      if (begin)
      {
        // Clear the form drawing surface , It's like a refresh 1 The secondary form interface is then redrawn 
        g.Clear(this.BackColor);
        // Gets the new lower right coordinate  
        secondPoint = new Point(e.X, e.Y);
        // Gets the larger or smaller of two Numbers 
        int minX = Math.Min(firstPoint.X, secondPoint.X);
        int minY = Math.Min(firstPoint.Y, secondPoint.Y);
        int maxX = Math.Max(firstPoint.X, secondPoint.X);
        int maxY = Math.Max(firstPoint.Y, secondPoint.Y);

        // The frame  
        g.DrawRectangle(new Pen(Color.Red), minX, minY, maxX - minX, maxY - minY);
        //ControlPaint.DrawReversibleFrame(new Rectangle(minX, minY, maxX - minX, maxY - minY),this.BackColor,FrameStyle.Dashed);

      }
    }

    /// <summary>
    ///  Release the mouse to stop drawing 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void data_MouseUp(object sender, MouseEventArgs e)
    {
      begin = false;
    }

    /// <summary>
    ///  in chart Move mouse drawing over control 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void chart1_MouseMove(object sender, MouseEventArgs e)
    {
      if (begin)
      {
        // Back in the chart Drawing above, not available here clear Method, clear Clears the entire drawing interface chart The control is cleared 
        this.Refresh();
        // Gets the new lower right coordinate  
        secondPoint = new Point(e.X, e.Y);
        int minX = Math.Min(firstPoint.X, secondPoint.X);
        int minY = Math.Min(firstPoint.Y, secondPoint.Y);
        int maxX = Math.Max(firstPoint.X, secondPoint.X);
        int maxY = Math.Max(firstPoint.Y, secondPoint.Y);

        // Draw a rectangular 
        g2.DrawRectangle(new Pen(Color.Red), minX, minY, maxX - minX, maxY - minY);

      }
    }

    /// <summary>
    ///  Release the mouse to stop drawing 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void chart1_MouseUp(object sender, MouseEventArgs e)
    {
      begin = false;
    }

    /// <summary>
    ///  in chart Press the mouse over the control 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void chart1_MouseDown(object sender, MouseEventArgs e)
    {
      begin = true;
      firstPoint = new Point(e.X, e.Y);
    }
  }
}

Hopefully this article has helped you with your C# programming


Related articles: