c++ windows forms l08 - gdi p1

64
Mohammad Shaker mohammadshakergtr.wordpress.com C++.NET Windows Forms Course @ZGTRShaker C++.NET Windows Forms Course L08- GDI Part 1

Upload: mohammad-shaker

Post on 25-Dec-2014

196 views

Category:

Technology


1 download

DESCRIPTION

C++ Windows Forms L08 - GDI P1 of C++ Windows Forms Light Course

TRANSCRIPT

Page 1: C++ Windows Forms L08 - GDI P1

Mohammad Shakermohammadshakergtr.wordpress.com

C++.NET Windows Forms Course@ZGTRShaker

C++.NET Windows Forms Course

L08- GDI Part 1

Page 2: C++ Windows Forms L08 - GDI P1
Page 3: C++ Windows Forms L08 - GDI P1

GDI- Part 1 -

Page 4: C++ Windows Forms L08 - GDI P1

What do u need to draw sth?

• Pen (stroke width, color, etc)• Paper• Brush to filling your drawing

Page 5: C++ Windows Forms L08 - GDI P1

Drawing::Graphic

• Encapsulates a GDI* + drawing surface.• This class cannot be inherited.

___________________• GDI* : Graphical Device Interface

Page 6: C++ Windows Forms L08 - GDI P1

Drawing::Graphics

private: System::Void button1_Click_5(System::Object^

sender, System::EventArgs^ e)

{

Drawing::Graphics ^MyGraphics;

MyGraphics = pictureBox1->CreateGraphics();

}

Page 7: C++ Windows Forms L08 - GDI P1

Drawing::Graphics

private void DrawImagePointF(PaintEventArgs e)

{

// Create image.

Image newImage = Image.FromFile("SampImag.jpg");

// Create point for upper-left corner of image.

PointF ulCorner = new PointF(100.0F, 100.0F);

// Draw image to screen.

e.Graphics.DrawImage(newImage, ulCorner);

}

Page 8: C++ Windows Forms L08 - GDI P1

Method DescriptionAddMetafileComment Adds a comment to the current Metafile.BeginContainer() Saves a graphics container with the current state of this Graphics and opens

and uses a new graphics container.BeginContainer(Rectangle, Rectangle, GraphicsUnit)

Saves a graphics container with the current state of this Graphics and opens and uses a new graphics container with the specified scale transformation.

BeginContainer(RectangleF, RectangleF, GraphicsUnit)

Saves a graphics container with the current state of this Graphics and opens and uses a new graphics container with the specified scale transformation.

Clear Clears the entire drawing surface and fills it with the specified background color.

CopyFromScreen(Point, Point, Size) Performs a bit-block transfer of color data, corresponding to a rectangle of pixels, from the screen to the drawing surface of the Graphics.

CopyFromScreen(Point, Point, Size, CopyPixelOperation)

Performs a bit-block transfer of color data, corresponding to a rectangle of pixels, from the screen to the drawing surface of the Graphics.

CopyFromScreen(Int32, Int32, Int32, Int32, Size)

Performs a bit-block transfer of the color data, corresponding to a rectangle of pixels, from the screen to the drawing surface of the Graphics.

CopyFromScreen(Int32, Int32, Int32, Int32, Size, CopyPixelOperation)

Performs a bit-block transfer of the color data, corresponding to a rectangle of pixels, from the screen to the drawing surface of the Graphics.

CreateObjRef Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. (Inherited fromMarshalByRefObject.)

Dispose Releases all resources used by this Graphics.DrawArc(Pen, Rectangle, Single, Single) Draws an arc representing a portion of an ellipse specified by

a Rectangle structure.DrawArc(Pen, RectangleF, Single, Single) Draws an arc representing a portion of an ellipse specified by a RectangleF

Page 9: C++ Windows Forms L08 - GDI P1

DrawArc(Pen, Int32, Int32, Int32, Int32, Int32, Int32) Draws an arc representing a portion of an ellipse specified by a pair of coordinates, a width, and a height.

DrawArc(Pen, Single, Single, Single, Single, Single, Single)

Draws an arc representing a portion of an ellipse specified by a pair of coordinates, a width, and a height.

DrawBezier(Pen, Point, Point, Point, Point) Draws a Bézier spline defined by four Point structures.

DrawBezier(Pen, PointF, PointF, PointF, PointF) Draws a Bézier spline defined by four PointF structures.

DrawBezier(Pen, Single, Single, Single, Single, Single, Single, Single, Single)

Draws a Bézier spline defined by four ordered pairs of coordinates that represent points.

DrawBeziers(Pen, Point[]) Draws a series of Bézier splines from an array of Point structures.

DrawBeziers(Pen, PointF[]) Draws a series of Bézier splines from an array of PointF structures.

DrawClosedCurve(Pen, Point[]) Draws a closed cardinal spline defined by an array of Point structures.

DrawClosedCurve(Pen, PointF[]) Draws a closed cardinal spline defined by an array of PointF structures.

DrawClosedCurve(Pen, Point[], Single, FillMode) Draws a closed cardinal spline defined by an array of Point structures using a specified tension.

DrawClosedCurve(Pen, PointF[], Single, FillMode) Draws a closed cardinal spline defined by an array of PointF structures using a specified tension.

DrawCurve(Pen, Point[]) Draws a cardinal spline through a specified array of Point structures.

DrawCurve(Pen, PointF[]) Draws a cardinal spline through a specified array of PointF structures.

DrawCurve(Pen, Point[], Single) Draws a cardinal spline through a specified array of Point structures using a specified tension.

Page 10: C++ Windows Forms L08 - GDI P1

DrawCurve(Pen, PointF[], Single) Draws a cardinal spline through a specified array of PointF structures using a specified tension.

DrawCurve(Pen, PointF[], Int32, Int32) Draws a cardinal spline through a specified array of PointF structures. The drawing begins offset from the beginning of the array.

DrawCurve(Pen, Point[], Int32, Int32, Single) Draws a cardinal spline through a specified array of Point structures using a specified tension.

DrawCurve(Pen, PointF[], Int32, Int32, Single) Draws a cardinal spline through a specified array of PointF structures using a specified tension. The drawing begins offset from the beginning of the array.

DrawEllipse(Pen, Rectangle) Draws an ellipse specified by a bounding Rectangle structure.

DrawEllipse(Pen, RectangleF) Draws an ellipse defined by a bounding RectangleF.

DrawEllipse(Pen, Int32, Int32, Int32, Int32) Draws an ellipse defined by a bounding rectangle specified by coordinates for the upper-left corner of the rectangle, a height, and a width.

DrawEllipse(Pen, Single, Single, Single, Single) Draws an ellipse defined by a bounding rectangle specified by a pair of coordinates, a height, and a width.

DrawIcon(Icon, Rectangle) Draws the image represented by the specified Icon within the area specified by aRectangle structure.

DrawIcon(Icon, Int32, Int32) Draws the image represented by the specified Icon at the specified coordinates.

DrawIconUnstretched Draws the image represented by the specified Icon without scaling the image.

DrawImage(Image, Point) Draws the specified Image, using its original physical size, at the specified location.

DrawImage(Image, Point[]) Draws the specified Image at the specified location and with the specified shape

Page 11: C++ Windows Forms L08 - GDI P1

System::Drawing

Page 12: C++ Windows Forms L08 - GDI P1

System::Drawing

• Graphics Class :– Can’t be inherited

• Inheritance Hierarchy :– System::Object

System::MarshalByRefObjectSystem.Drawing::Graphics

Page 13: C++ Windows Forms L08 - GDI P1

Drawing::Point

• What will happen now?

private: System::Void button1_Click(System::Object^

sender, System::EventArgs^ e)

{

button1->Location = 30,120;

}

Page 14: C++ Windows Forms L08 - GDI P1

Drawing::Point

Page 15: C++ Windows Forms L08 - GDI P1

Drawing::Point

• What will happen now?

private: System::Void button1_Click(System::Object^ sender,

System::EventArgs^ e)

{

Drawing::Point P;

P.X = 2;

P.Y = 23;

button1->Location = P;

}

Page 16: C++ Windows Forms L08 - GDI P1

Drawing::Point

Page 17: C++ Windows Forms L08 - GDI P1

Changing the size of the form at run time

So, what should we do?

Page 18: C++ Windows Forms L08 - GDI P1

Drawing::Size

• Can we do this? Form’s size?

private: System::Void button1_Click_1(System::Object^ sender,

System::EventArgs^ e)

{

Drawing::Size S;

S.Height = 200;

S.Width = 300;

this->Size = S;

}

Yep!

Page 19: C++ Windows Forms L08 - GDI P1

Drawing::Size

• Can we do this?

private: System::Void button1_Click_1(System::Object^ sender,

System::EventArgs^ e)

{

Drawing::Size ^S;

S->Height = 200;

S->Width = 300;

this->Size = S;

}

Compile error

Error 1 error C2664: 'void

System::Windows::Forms::Control::Size::set(System::Drawing::Size)'

: cannot convert parameter 1 from 'System::Drawing::Size ^' to

'System::Drawing::Size' c:\users\zgtr\documents\visual studio

2008\projects\dotnet4\dotnet4\Form1.h 129 dotNet4

Page 20: C++ Windows Forms L08 - GDI P1
Page 21: C++ Windows Forms L08 - GDI P1

Drawing::Size

• Do this:

• Or Add Drawing in using

private: System::Void button1_Click_1(System::Object^

sender, System::EventArgs^ e)

{

this->Size = Drawing::Size(200,300);

}

Page 22: C++ Windows Forms L08 - GDI P1
Page 23: C++ Windows Forms L08 - GDI P1

Drawing::Size

Page 24: C++ Windows Forms L08 - GDI P1

System::Drawing::Penprivate: System::Void button1_Click_5(System::Object^ sender,

System::EventArgs^ e)

{

System::Drawing::Pen ^ MyPen = gcnew Pen(Color::Red);

}

Page 25: C++ Windows Forms L08 - GDI P1
Page 26: C++ Windows Forms L08 - GDI P1
Page 27: C++ Windows Forms L08 - GDI P1

Let’s draw a line!

Page 28: C++ Windows Forms L08 - GDI P1

Graphics.DrawLine() Methodprivate: System::Void button1_Click_5(System::Object^ sender,

System::EventArgs^ e)

{

System::Drawing::Graphics::DrawLine(

}

Page 29: C++ Windows Forms L08 - GDI P1
Page 30: C++ Windows Forms L08 - GDI P1

System::Drawing::Penprivate: System::Void button1_Click_5(System::Object^ sender,

System::EventArgs^ e)

{

System::Drawing::Pen ^ MyPen = gcnew Pen(Color::Red);

Drawing::Graphics::DrawLine( MyPen, 2, 3, 4,5 );

}

Page 31: C++ Windows Forms L08 - GDI P1

Graphics.DrawLine Method

• public: void DrawLine(Pen*, Point, Point);• public: void DrawLine(Pen*, PointF, PointF);• public: void DrawLine(Pen*, int, int, int, int);• public: void DrawLine(Pen*, float, float, float, float);

Page 32: C++ Windows Forms L08 - GDI P1
Page 33: C++ Windows Forms L08 - GDI P1
Page 34: C++ Windows Forms L08 - GDI P1

System::Drawing

• What will happen now?

private: System::Void button1_Click_5(System::Object^ sender,

System::EventArgs^ e)

{

System::Drawing::Pen ^ MyPen = gcnew Pen(Color::Red);

Drawing::Graphics::DrawLine( MyPen, 2, 3, 4,5 );

}

Compiler error ..

Page 35: C++ Windows Forms L08 - GDI P1
Page 36: C++ Windows Forms L08 - GDI P1

System::Drawing

• What will happen now? private: System::Void button1_Click_5(System::Object^ sender,

System::EventArgs^ e)

{

System::Drawing::Pen ^ MyPen = gcnew Pen(Color::Red);

Drawing::Graphics ^MyGraphics;

MyGraphics->DrawLine( MyPen, 2, 3, 4,5 );

}

No compiler error but Runtime error when clicking button1

Page 37: C++ Windows Forms L08 - GDI P1
Page 38: C++ Windows Forms L08 - GDI P1

System::Drawing

• What will happen now?

private: System::Void button1_Click_5(System::Object^ sender, System::EventArgs^

e)

{

System::Drawing::Pen ^ MyPen = gcnew Pen(Color::Red);

Drawing::Graphics ^MyGraphics;

MyGraphics = pictureBox1->CreateGraphics();

MyGraphics->DrawLine( MyPen, 2, 3, 50,105 );

}

A line will be drawn!

Page 39: C++ Windows Forms L08 - GDI P1

System::Drawing

• After clicking the button

Page 40: C++ Windows Forms L08 - GDI P1

System::Drawing

• Can we do this? private: System::Void button1_Click_5(System::Object^ sender,

System::EventArgs^ e)

{

Drawing::Graphics ^MyGraphics;

MyGraphics = pictureBox1->CreateGraphics();

MyGraphics->DrawLine(System::Drawing::Pen(Color::Red),2,3,50,105);

}

Compiler error

Page 41: C++ Windows Forms L08 - GDI P1
Page 42: C++ Windows Forms L08 - GDI P1

System::Drawing

• Can we do this? private: System::Void button1_Click_5(System::Object^ sender,

System::EventArgs^ e)

{

System::Drawing::Pen MyPen();

MyPen.Color = Color::Red;

Drawing::Graphics ^MyGraphics;

MyGraphics = pictureBox1->CreateGraphics();

MyGraphics->DrawLine(MyPen, 2, 3, 50,105 );

}

Compiler error

Page 43: C++ Windows Forms L08 - GDI P1
Page 44: C++ Windows Forms L08 - GDI P1

System::Drawing

• Can we do this? private: System::Void button1_Click_5(System::Object^ sender,

System::EventArgs^ e)

{

System::Drawing::Pen MyPen(Color::Red );

Drawing::Graphics ^MyGraphics;

MyGraphics = pictureBox1->CreateGraphics();

MyGraphics->DrawLine(MyPen, 2, 3, 50,105 );

}

Compiler error

Page 45: C++ Windows Forms L08 - GDI P1
Page 46: C++ Windows Forms L08 - GDI P1

System::Drawing

• Can we do this?private: System::Void button1_Click_5(System::Object^ sender,

System::EventArgs^ e)

{

System::Drawing::Pen ^MyPen(Color::Red);

Drawing::Graphics ^MyGraphics;

MyGraphics = pictureBox1->CreateGraphics();

MyGraphics->DrawLine(MyPen, 2, 3, 50,105 );

}

Compiler error!

Page 47: C++ Windows Forms L08 - GDI P1
Page 48: C++ Windows Forms L08 - GDI P1

System::Drawing

• Can we do this? private: System::Void button1_Click_5(System::Object^ sender,

System::EventArgs^ e)

{

System::Drawing::Pen ^MyPen = gcnew Pen;

Drawing::Graphics ^MyGraphics;

MyGraphics = pictureBox1->CreateGraphics();

MyGraphics->DrawLine(MyPen, 2, 3, 50,105 );

}

Compiler error

Page 49: C++ Windows Forms L08 - GDI P1
Page 50: C++ Windows Forms L08 - GDI P1

System::Drawing

• Can we do this? private: System::Void button1_Click_5(System::Object^ sender,

System::EventArgs^ e)

{

System::Drawing::Color ^MyColor = gcnew Color;

MyColor = Drawing::Color::Red;

System::Drawing::Pen ^MyPen = gcnew Pen(*MyColor);

Drawing::Graphics ^MyGraphics;

MyGraphics = pictureBox1->CreateGraphics();

MyGraphics->DrawLine(MyPen, 2, 3, 50,105 );

}

Working!

Page 51: C++ Windows Forms L08 - GDI P1

System::Drawing

• Can we do this? private: System::Void button1_Click_5(System::Object^ sender,

System::EventArgs^ e)

{

System::Drawing::Color ^MyColor = gcnew Color; // 1

MyColor = Drawing::Color::Red; // 2

System::Drawing::Pen ^MyPen = gcnew Pen(MyColor); // 3

Drawing::Graphics ^MyGraphics;

MyGraphics = pictureBox1->CreateGraphics();

MyGraphics->DrawLine(MyPen, 2, 3, 50,105 );

}

Compiler error in // 3

Page 52: C++ Windows Forms L08 - GDI P1

System::Drawing

• Can we do this? private: System::Void button1_Click_5(System::Object^ sender,

System::EventArgs^ e)

{

System::Drawing::Color MyColor = gcnew Color; // 1

MyColor = Drawing::Color::Red; // 2

System::Drawing::Pen ^MyPen = gcnew Pen(*MyColor); // 3

Drawing::Graphics ^MyGraphics;

MyGraphics = pictureBox1->CreateGraphics();

MyGraphics->DrawLine(MyPen, 2, 3, 50,105 );

}

Compiler error in // 1 . ^

Page 53: C++ Windows Forms L08 - GDI P1

System::Drawing

• Can we do this? private: System::Void button1_Click_5(System::Object^ sender,

System::EventArgs^ e)

{

System::Drawing::Color ^MyColor = gcnew Color; // 1

MyColor = Red; // 2

System::Drawing::Pen ^MyPen = gcnew Pen(*MyColor); // 3

Drawing::Graphics ^MyGraphics;

MyGraphics = pictureBox1->CreateGraphics();

MyGraphics->DrawLine(MyPen, 2, 3, 50,105 );

}

Compiler error in // 2 un-declared Red

Page 54: C++ Windows Forms L08 - GDI P1

System::Drawing

• But remember we can just do this

private: System::Void button1_Click_5(System::Object^ sender,

System::EventArgs^ e)

{

System::Drawing::Pen ^ MyPen = gcnew Pen(Color::Red);

Drawing::Graphics ^MyGraphics;

MyGraphics = pictureBox1->CreateGraphics();

MyGraphics->DrawLine( MyPen, 2, 3, 50,105 );

}

Page 55: C++ Windows Forms L08 - GDI P1

System::Drawing

• Another way!private: System::Void button1_Click_5(System::Object^ sender,

System::EventArgs^ e)

{

System::Drawing::Pen ^ MyPen = gcnew Pen(Color::Red);

Drawing::Graphics ^MyGraphics;

MyGraphics = pictureBox1->CreateGraphics();

Drawing::Point ^P1 = gcnew Point(50,60);

Drawing::Point ^P2 = gcnew Point(150,160);

MyGraphics->DrawLine( MyPen, *P1, *P2 );

}

What will happen?

Page 56: C++ Windows Forms L08 - GDI P1

System::Drawing

Page 57: C++ Windows Forms L08 - GDI P1

System::Drawing

• Another way!

private: System::Void button1_Click_5(System::Object^ sender,

System::EventArgs^ e)

{

System::Drawing::Pen ^ MyPen = gcnew Pen(Color::Red);

Drawing::Graphics ^MyGraphics;

MyGraphics = pictureBox1->CreateGraphics();

Drawing::Point P1 = gcnew Point(50,60);

Drawing::Point P2 = gcnew Point(150,160);

MyGraphics->DrawLine( MyPen, *P1, *P2 );

}

What will happen?

Compiler error

Page 58: C++ Windows Forms L08 - GDI P1

System::Drawing

• Another way!

private: System::Void button1_Click_5(System::Object^ sender,

System::EventArgs^ e)

{

System::Drawing::Pen ^ MyPen = gcnew Pen(Color::Red);

Drawing::Graphics ^MyGraphics;

MyGraphics = pictureBox1->CreateGraphics();

Drawing::Point P1 = gcnew Point(50,60);

Drawing::Point P2 = gcnew Point(150,160);

MyGraphics->DrawLine( MyPen, P1, P2 );

}

What will happen?

Compiler error

Page 59: C++ Windows Forms L08 - GDI P1

System::Drawing

• Can we do this?

private: System::Void button1_Click_5(System::Object^ sender,

System::EventArgs^ e)

{

System::Drawing::Pen ^ MyPen = gcnew Pen(Color::Red);

Drawing::Graphics ^MyGraphics;

MyGraphics = pictureBox1->CreateGraphics();

Drawing::Point P1(50,60);

Drawing::Point P2(150,160);

MyGraphics->DrawLine( MyPen, P1, P2 );

}

Works!

Page 60: C++ Windows Forms L08 - GDI P1

System::Drawing

• Sth wrong?private: System::Void button1_Click_5(System::Object^ sender,

System::EventArgs^ e)

{

System::Drawing::Pen ^ MyPen = gcnew Pen(Color::Red);

Drawing::Graphics ^MyGraphics;

MyGraphics = pictureBox1->CreateGraphics();

Drawing::Point P1; P1.X = 50; P1.Y = 60;

Drawing::Point P2; P2.X = 150; P2.Y = 160;

MyGraphics->DrawLine( MyPen, P1, P2 );

}

Works!

Page 61: C++ Windows Forms L08 - GDI P1

System::Drawing

• What will happen now?

private: System::Void button1_Click_5(System::Object^ sender,

System::EventArgs^ e)

{

System::Drawing::Pen ^ MyPen = gcnew Pen(Color::Red);

Drawing::Graphics ^MyGraphics;

MyGraphics = pictureBox1->CreateGraphics();

Drawing::Point P1, P2;

P1.X = 50; P1.Y = 60;

P2.X = 150; P2.Y = 160;

MyGraphics->DrawLine( MyPen, P1, P2 );

P1.X = 50; P1.Y = 60;

P2.X = 510; P2.Y = 610;

MyGraphics->DrawLine( MyPen, P1, P2 );

}

Page 62: C++ Windows Forms L08 - GDI P1

System::Drawing

• What’s missing? Next Lesson we’ll find out!

Page 63: C++ Windows Forms L08 - GDI P1

Mouse!

private: System::Void panel1_MouseClick(System::Object^ sender,

System::Windows::Forms::MouseEventArgs^ e)

{

System::Drawing::Point P(0,0);

MyGraphics->DrawEllipse(MyPen,e->X,e->Y,10,10);

MyGraphics->FillEllipse(MyBrush,e->X,e->Y,10,10);

}

Page 64: C++ Windows Forms L08 - GDI P1

That’s it for today!