Successfully reported this slideshow.
Your SlideShare is downloading. ×

C++ Windows Forms L08 - GDI P1

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Loading in …3
×

Check these out next

1 of 64 Ad

More Related Content

Slideshows for you (19)

Advertisement

More from Mohammad Shaker (20)

Recently uploaded (20)

Advertisement

C++ Windows Forms L08 - GDI P1

  1. 1. C++.NET Windows Forms Course L08- GDI Part 1 Mohammad Shaker mohammadshakergtr.wordpress.com C++.NET Windows Forms Course @ZGTRShaker
  2. 2. GDI - Part 1 -
  3. 3. What do u need to draw sth? • Pen (stroke width, color, etc) • Paper • Brush to filling your drawing
  4. 4. Drawing::Graphic • Encapsulates a GDI* + drawing surface. • This class cannot be inherited. ___________________ • GDI* : Graphical Device Interface
  5. 5. Drawing::Graphics private: System::Void button1_Click_5(System::Object^ sender, System::EventArgs^ e) { Drawing::Graphics ^MyGraphics; MyGraphics = pictureBox1->CreateGraphics(); }
  6. 6. 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); }
  7. 7. Method AddMetafileComment BeginContainer() Description BeginContainer(Rectangle, Rectangle, GraphicsUnit) Adds a comment to the current Metafile. Saves a graphics container with the current state of this Graphics and opens and uses a new graphics container. 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. 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) 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.) Releases all resources used by this Graphics. Draws an arc representing a portion of an ellipse specified by a Rectangle structure. Draws an arc representing a portion of an ellipse specified by a RectangleF Dispose DrawArc(Pen, Rectangle, Single, Single) DrawArc(Pen, RectangleF, Single, Single)
  8. 8. 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, Draws a Bézier spline defined by four ordered pairs of coordinates that represent points. Single, Single, Single) 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.
  9. 9. DrawCurve(Pen, PointF[], Single) DrawCurve(Pen, PointF[], Int32, Int32) Draws a cardinal spline through a specified array of PointF structures using a specified tension. 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) DrawEllipse(Pen, Int32, Int32, Int32, Int32) Draws an ellipse defined by a bounding RectangleF. 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) DrawIconUnstretched DrawImage(Image, Point) DrawImage(Image, Point[]) Draws the image represented by the specified Icon at the specified coordinates. Draws the image represented by the specified Icon without scaling the image. Draws the specified Image, using its original physical size, at the specified location. Draws the specified Image at the specified location and with the specified shape
  10. 10. System::Drawing
  11. 11. System::Drawing • Graphics Class : – Can’t be inherited • Inheritance Hierarchy : – System::Object System::MarshalByRefObject System.Drawing::Graphics
  12. 12. Drawing::Point • What will happen now? private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { button1->Location = 30,120; }
  13. 13. Drawing::Point
  14. 14. Drawing::Point • What will happen now? private: System::Void button1_Click(System::Object^ System::EventArgs^ e) { Drawing::Point P; P.X = 2; P.Y = 23; button1->Location = P; } sender,
  15. 15. Drawing::Point
  16. 16. Changing the size of the form at run time So, what should we do?
  17. 17. Drawing::Size • Can we do this? Form’s size? private: System::Void button1_Click_1(System::Object^ System::EventArgs^ e) { Drawing::Size S; S.Height = 200; S.Width = 300; this->Size = S; } Yep! sender,
  18. 18. Drawing::Size • Can we do this? private: System::Void button1_Click_1(System::Object^ System::EventArgs^ e) { Drawing::Size ^S; S->Height = 200; S->Width = 300; this->Size = S; } sender, 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:userszgtrdocumentsvisual studio 2008projectsdotnet4dotnet4Form1.h 129 dotNet4
  19. 19. Drawing::Size • Do this: private: System::Void button1_Click_1(System::Object^ sender, System::EventArgs^ e) { this->Size = Drawing::Size(200,300); } • Or Add Drawing in using
  20. 20. Drawing::Size
  21. 21. System::Drawing::Pen private: System::Void button1_Click_5(System::Object^ sender, System::EventArgs^ e) { System::Drawing::Pen ^ MyPen = gcnew Pen(Color::Red); }
  22. 22. Let’s draw a line!
  23. 23. Graphics.DrawLine() Method private: System::Void button1_Click_5(System::Object^ System::EventArgs^ e) { System::Drawing::Graphics::DrawLine( } sender,
  24. 24. System::Drawing::Pen 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 ); }
  25. 25. 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);
  26. 26. 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 ..
  27. 27. 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
  28. 28. 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!
  29. 29. System::Drawing • After clicking the button
  30. 30. 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
  31. 31. System::Drawing • Can we do this? private: System::Void button1_Click_5(System::Object^ 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 sender,
  32. 32. System::Drawing • Can we do this? private: System::Void button1_Click_5(System::Object^ System::EventArgs^ e) { System::Drawing::Pen MyPen(Color::Red ); Drawing::Graphics ^MyGraphics; MyGraphics = pictureBox1->CreateGraphics(); MyGraphics->DrawLine(MyPen, 2, 3, 50,105 ); } Compiler error sender,
  33. 33. System::Drawing • Can we do this? private: System::Void button1_Click_5(System::Object^ System::EventArgs^ e) { System::Drawing::Pen ^MyPen(Color::Red); Drawing::Graphics ^MyGraphics; MyGraphics = pictureBox1->CreateGraphics(); MyGraphics->DrawLine(MyPen, 2, 3, 50,105 ); } Compiler error! sender,
  34. 34. System::Drawing • Can we do this? private: System::Void button1_Click_5(System::Object^ System::EventArgs^ e) { System::Drawing::Pen ^MyPen = gcnew Pen; Drawing::Graphics ^MyGraphics; MyGraphics = pictureBox1->CreateGraphics(); MyGraphics->DrawLine(MyPen, 2, 3, 50,105 ); } Compiler error sender,
  35. 35. 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!
  36. 36. 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
  37. 37. 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 . ^
  38. 38. 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
  39. 39. 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 ); }
  40. 40. 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?
  41. 41. System::Drawing
  42. 42. 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
  43. 43. 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
  44. 44. 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!
  45. 45. 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!
  46. 46. 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 ); }
  47. 47. System::Drawing • What’s missing? Next Lesson we’ll find out!
  48. 48. 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); }
  49. 49. That’s it for today!

×