In this lab, you will create a program that allows the user to draw lines on a canvas using the mouse.

Getting Started

Begin by downloading CS101_Lab24.zip. Save the zip file in the H:\CS101 directory.

Start the Cygwin Bash Shell and run the following commands:

cd h:
cd CS101
unzip CS101_Lab24.zip
cd CS101_Lab24

Leave the Cygwin Bash Shell open.

Start Visual Studio 2012 (from the Start menu, Programming->All Programs->Visual Studio 2012. Choose File->Open->Project/Solution…. Choose the file


In the Solution Explorer, double click the file Form1.cs. You should see the form in a tab called Form1.cs [Design].

Your Task

Create a GUI that allows the user to draw lines using the mouse.

Here is what you should do:

  1. Grab a PictureBox from the Toolbox and place it on your form. Size the PictureBox such that it covers most of the form.

  2. Change the BackColor property of the PictureBox to something a little less ugly than the standard gray background color. You may even like to change the BorderStyle property of the PictureBox to add a more finished look.

  3. Add two event handlers for your PictureBox control, a MouseDown event handler and a MouseUp event handler. To automatically generate the skeleton code for the event handlers, go into the Events tab of the PictureBox properties. Don’t worry about filling in the code for these event handlers just yet.

  4. Add a third event handler to your PictureBox, the Paint event handler. Again, skeleton code will be automatically generated for you. You will need to fill in this method later.

  5. In the source code view for Form1.cs you will see a struct definition for a Shape. The Shape struct contains two Point fields that each have coordinate values X and Y. You can access the X and Y values of a Point using the dot-notation startPoint.X and startPoint.Y. Next, you will need to declare an instance of this struct and initialize it. Declare an instance of this struct globally, by inserting the following code immediately below the struct definition:

    > Shape myShape;
  6. This struct still needs to be initialized. Initialize this struct inside the Form1() method by inserting the following code immediately below the function call to InitializeComponent().

    > myShape = new Shape();
  7. You should test your program at this point to ensure that everything is running smoothly.

  8. Now it’s time to fill in the event handler methods that were created for the PictureBox. You should have three event handlers for the PictureBox: MouseDown, MouseUp, and Paint. Fill in the first event handler, MouseDown, such that it stores the X and Y coordinate of a mouse click inside that startPoint of your Shape.

  9. Fill in the second event handler, MouseUp, such that it stores the X and Y coordinates of the mouse location when the user releases the mouse button. These coordinates should be stored in the endPoint of your Shape. This event handler should also call the Invalidate() method of your PictureBox. The Invalidate() method alerts your program that the PictureBox has been changed and may need to be re-painted. Calling the Invalidate() method produces another event that is handled by the Paint event handler. Invalidating your PictureBox when the user releases the mouse button will cause the PictureBox to be redrawn with the new line that the user has just created.

  10. Finally, fill in the Paint event handler. This event handler should draw the graphics, in this case a line, onto your PictureBox. The PaintEventArgs e will contain a reference to a Graphics object onto which you should draw your line.

Here is a screenshot:

kamisama wechat