(C#)GDI+绘图之鼠标移动画图

主要在鼠标按下,鼠标移动,松开鼠标几个事件中编写。
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;

namespace MouseDrawOne
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            g = this.pictureBox1.CreateGraphics();
        }

        int lineStartX = 0;
        int lineStartY = 0;
        bool drawkine = false;
        Graphics g;
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                lineStartX = e.X;
                lineStartY = e.Y;
                drawkine = true;
            }
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (drawkine)
            {
                Pen p =new Pen(Color.Black,2);
                g.DrawLine(p, lineStartX, lineStartY, e.X, e.Y);
                lineStartX = e.X;
                lineStartY = e.Y;
            }
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            drawkine = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            pictureBox1.Refresh();
        }

    }

原文地址:https://www.cnblogs.com/finlay/p/3234758.html