< C++ .NET Drawing & Printing 1 | Main | C++ .NET Drawing & Printing 3 >


 

 

Graphical Output: Simple Drawing and Printing 2

 

 

What we have in this page?

 

  1. More on Paint Events

 

1.        Start a new C++ Windows Forms Application project named CppDraw.

 

A new C++ Windows Forms Application project named CppDraw

 

2.        Use the Properties editor to set the title of the form to Drawing.

 

Using the Properties editor to set the title of the form to Drawing

 

 

 

If you examine the properties of the form, you will see that the size of the client area is set to the default value of 300 by 300 pixels. The client area is that part of the form that you are responsible for maintaining and represents the area of the form inside the scroll bars and frame.

 

3.        Drag a button from the Toolbox onto the form. Using the Properties editor, set its Name to drawBtn, set its Location property to 200,200, and give it the Text Draw.

 

Setting a button properties

 

Now add a handler for the Click event. Go to the Events page and double click the empty field of the Click event. You’ve seen how to add buttons and set up handlers before and can refer to previous modules if you need to refresh your memory. The button is positioned toward the lower right of the window, and when it’s clicked, graphics code will execute. Here’s the code for the handler:

private: System::Void drawBtn_Click(System::Object^  sender, System::EventArgs^  e)

{

      // Get a Graphics object

      Graphics^ pg = CreateGraphics();

      // Get a Pen

      Pen^ pen1 = gcnew Pen(Color::Red);

      // Draw a line

      pg->DrawLine(pen1, 20, 20, 100, 250);

      // Dispose of the Graphics object

      delete pg;

}

 

 

 

 

Adding an event handler for the Click event

 

When the button is clicked, a Graphics object is created, along with a one-pixel-wide black pen. The DrawLine method takes a pointer to a Pen, plus the beginning and end coordinates of the line in pixels. Once the line has been drawn, you no longer need the Graphics object, so you can delete it. Coordinates are in pixels, with (0, 0) at the upper left of the form. So X coordinates increase to the right, and Y coordinates increase downward.

 

X coordinates increase to the right, and Y coordinates increase downward for graphics (x,y) coordinate.

 

4.        Build and run the program. You should see a line drawn on the screen when you click the button.

 

A line drawn on a Windows form

 

5.        You can now add a few more calls to Graphics drawing methods. Before you do, you should add a using directive for the System::Drawing::Drawing2D namespace at the top of the code. You’ll want this directive because some of the data structures used in the code are from this namespace.

using namespace System::Drawing::Drawing2D;

 

Adding a using directive for the System::Drawing::Drawing2D namespace

 

6.        Next, make sure you put the following code after the first call to DrawLine in the drawBtn_Click method and before the call to delete.

 

      // Draw a styled line

      Pen^ pen2 = gcnew Pen(Color::Blue, 3.0);

      pen2->DashStyle = DashStyle::DashDotDot;

      pg->DrawLine(pen2, 10, 120, 250, 60);

 

      // Draw a filled rectangle

      SolidBrush^ sb1 = gcnew SolidBrush(Color::Red);

      pg->FillRectangle(sb1, 60,30, 40,40);

 

      // Draw a filled and outlined rectangle

      SolidBrush^ sb2 = gcnew SolidBrush(Color::Yellow);

      pg->FillRectangle(sb2, 90, 40, 45, 45);

      pg->DrawRectangle(pen1, 90, 40, 45, 45);

 

      // Draw a filled ellipse

      SolidBrush^ sb3 = gcnew SolidBrush(Color::Green);

      pg->FillEllipse(sb3, 30,100, 65, 50);

 

 

 

 

Adding more calls to Graphics drawing methods

 

The line uses a new Pen that draws dashed lines. There are several members of the DashStyle enumeration, and you can also produce custom dashed lines by defining the dash pattern. Two rectangles are drawn, one red and one yellow, and the second one is outlined in red.

 

7.        Build and run the application. You should get output similar to the following figure when you click the Draw button.

 

More objects drawn on Windows forms

 

 

Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6

 

 


< C++ .NET Drawing & Printing 1 | Main | C++ .NET Drawing & Printing 3 >