3

I've been working on a paint application with C#; and I want to add an option that toggles drawing an ellipse with your current brush. I've been stumped on how to make it so the size and Y location of the ellipse changes while holding the mouse down. Any ideas? Here is my code.

    using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ZSPainter
{
    public partial class Form1 : Form
    {
        string drawToolString;
        Graphics g;
        bool c = false;
        bool drawEllipse = false;
        Point sp = new Point(0, 0);
        Point ep = new Point(0, 0);
        Pen p = new Pen(Color.Black, 1);
        Brush b = new SolidBrush(Color.Black);
        public Form1()
        {
            InitializeComponent();
        }

        private void penToolStripMenuItem_Click(object sender, EventArgs e)
        {
            drawToolString = "pen";
            toolString.Text = "Current Tool: Pen";
        }

        private void brushToolStripMenuItem_Click(object sender, EventArgs e)
        {
            drawToolString = "brush";
            toolString.Text = "Current Tool: Brush";
        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            sp = e.Location;
            if(e.Button == MouseButtons.Left)
                c = true;
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (drawToolString == "pen" && c)
            {
                ep = e.Location;
                g = this.CreateGraphics();
                if(!drawEllipse)
                g.DrawLine(p, sp, ep);
                /*else
                 * 
                  *------Here is where I want an ellipse to drawn if drawEllipse is true.------

                 */


            }
            else if (drawToolString == "brush" && c)
            {
                ep = e.Location;
                g = this.CreateGraphics();
                if(!drawEllipse)
                g.DrawLine(new Pen(b, 3), sp, ep);
                /*else
                 * 
                  *------Here is where I want an ellipse to drawn if drawEllipse is true.------

                 */
            }
            sp = ep;
        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            c = false;
        }

        private void ellipse_Click(object sender, EventArgs e)
        {
            drawEllipse = !drawEllipse;
        }
    }
}
ScottMcGready
  • 1,612
  • 2
  • 24
  • 33

1 Answers1

0

Use a picturebox with transparent background and your ellipse already drawn from an image file (per example) and put it at the mouse coordinates.

But beware, this will yield a lot of troubles with your code, you are drawing to the form, when it gets refreshed everything will be cleared in the refreshed region, so if you put and move a control over it, it will be cleared.

I recommend to create a Bitmap, set this bitmap as background of your form and draw to that bitmap, then it will be persistent (and also will be easier to save).

Also, you will need to set mouse capture on MouseDown event and release on MouseUp or the form will not trap the MouseMove events after showing the picture box.

If you don't want to use a picturebox but you switch your draw to a bitmap, you can paint over the form and refresh previous areas where you did draw the ellipse, in this way the drawing will be preserved and the ellipse will move over it.

Gusman
  • 14,905
  • 2
  • 34
  • 50