1

I'm developing a basic GUI Windows Form Application. It allows the user to change colors using sliding bars, change the size of shapes using scroll bars, and click a check box to determine if the shape should be filled, clear the screen if the clear button is clicked, and switch between shapes with buttons. I'm currently using the MouseMove event to cause the shape to follow the mouse around inside of the panel. I also have an event for a MouseClick. When there is a mouseclick, I want it to "place" that shape so that the graphic persists. I've been able to move a single shape to where the user clicks and make it follow the mouse, I just don't know how to combine both.

This is what I have so far:

        void DrawShape()
        {
            //g.Clear(Color.White);
            p.Color = Color.FromArgb(r, gr, b);
            if (IsChecked) // If checkbox is checked for fill
            {
                if (IsRect)
                {
                    g.FillRectangle(myBrush, left, top, wid, ht); // Filled Rectangle
                }
                else if (IsEllipse)
                {
                    g.FillEllipse(myBrush, left, top, wid, ht); // Filled Ellipse
                }
            }
            else // not filled
            {
                if (IsRect) 
                {
                    g.DrawRectangle(p, left, top, wid, ht); // empty rectangle
                }
                else if (IsEllipse)// empty ellipse
                {
                    g.DrawEllipse(p, left, top, wid, ht);
                }
            }

        }
        private void panel1_MouseMove(object sender, MouseEventArgs e) // when mouse moves
        {
            left = e.X;
            top = e.Y;
            DrawShape();
        }
        private void panel1_MouseClick(object sender, MouseEventArgs e) //when mouse clicks
        {
            left = e.X;
            top = e.Y;
            IsClick = true;
            DrawShape();
            IsClick = false;
        }

Brenden Price
  • 517
  • 2
  • 9
  • [How to drag and move shapes in C#](https://stackoverflow.com/a/38749134/14171304) – dr.null Sep 21 '20 at 20:53
  • @dr.null I took a look at this, I'm not really sure if it will help me. As I mentioned, I succeeded in getting the shape to follow my mouse. I also have managed to move the shape with clicks. I need the shape to both be displayed as the mouse moves, but if I click and move the mouse away, I should be able to see a rectangle where I clicked that does not follow my mouse. – Brenden Price Sep 21 '20 at 21:34
  • You need to store the INFORMATION about EACH of your shapes at form level, then redraw all of the shapes using the supplied `e.Graphics` in the `Paint()` event of whatever control you are drawing onto. This is typically done with a `List` that you iterate over in the `Paint()` event. Right now it looks like you are simply storing the coords and size of ONE "thing", in one set of variables. You need to encapsulate that information, along with the shape and whether it is filled in a CLASS, then store those instances in the previously mentioned `List<>`. – Idle_Mind Sep 21 '20 at 23:23
  • As mentioned, you need to do something like [How to use the Paint event to draw shapes at mouse coordinates](https://stackoverflow.com/a/53708936/14171304). Another example [here](https://stackoverflow.com/a/48484388/14171304), Its vb.net if you don't mind. You already know the moving part. – dr.null Sep 22 '20 at 01:08

0 Answers0