Hi! I'm making a paint application and i can't figure out how to draw a new line or any other shape without clearing the previous drawings please help as soon as possible.
CODE
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication3
{
public partial class Form1 : Form
{
private void Form1_Load(object sender, EventArgs e)
{
}
private Graphics DrawingSurface;
private System.Drawing.Pen ErasePen;
private System.Drawing.Pen DrawPen;
private System.Drawing.Point ptsStart;
private System.Drawing.Point ptsPrevious;
public Form1()
{
InitializeComponent();
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.ResizeRedraw, true);
DrawingSurface = CreateGraphics();
// Pen to erase a previous line
ErasePen = new System.Drawing.Pen(BackColor);
// Pen to draw a new line
DrawPen = new System.Drawing.Pen(Color.Blue);
//ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint |ControlStyles.UserPaint, true);
}
protected override void OnMouseDown(MouseEventArgs e)
{
//Check if the left mouse button has
//generated the associated event.
if (e.Button == MouseButtons.Left)
{
//Store the starting point for your rubber-band line.
ptsStart.X = e.X;
ptsStart.Y = e.Y;
//Store the previous endpoint for your rubber-band line.
ptsPrevious = ptsStart;
}
}
protected override void OnMouseMove(MouseEventArgs e)
{
//Check if the left mouse button is pressed.
if (e.Button == MouseButtons.Left)
{
//Declare a local variable for the current endpoint '
//of your rubber-band line.
Point ptsCurrent = new Point();
//Store the current endpoint of your rubber-band line.
ptsCurrent.X = e.X;
ptsCurrent.Y = e.Y;
//Draw your actual rubber-band lines.
DrawingSurface.DrawLine(ErasePen, ptsStart.X, ptsStart.Y,
ptsPrevious.X, ptsPrevious.Y);
DrawingSurface.DrawLine(DrawPen, ptsStart.X, ptsStart.Y,
ptsCurrent.X, ptsCurrent.Y);
//Store the current endpoint for the next call to OnMouseMove.
ptsPrevious = ptsCurrent;
}
}
}
}
Form1.txt ( 2.85k )
Number of downloads: 31