0
C++.NET
Windows Forms Course
L09- GDI Part 2

Mohammad Shaker
mohammadshakergtr.wordpress.com
C++.NET Windows Forms Course...
GDI
- Part 1 Quick Revision -
What do u need to draw sth?
• Pen(stroke width, color, etc)
• Paper
• Brush to filling your drawing
Drawing::Graphic
• Encapsulates a GDI* + drawing surface.
• This class cannot be inherited.

___________________
• GDI* : ...
Drawing::Graphics

private: System::Void button1_Click_5(System::Object^
sender, System::EventArgs^ e)
{
Drawing::Graphics...
Drawing::Graphics
private void DrawImagePointF(PaintEventArgs e)
{
// Create image.
Image newImage = Image.FromFile("SampI...
System::Drawing
Drawing::Point
• What does this even mean? -_private: System::Void button1_Click(System::Object^
sender, System::EventArgs...
Drawing::Point
Drawing::Point
• What will happen now?
private: System::Void button1_Click(System::Object^ sender,
System::EventArgs^ e)
{...
Drawing::Point
System::Drawing::Pen
private: System::Void button1_Click_5(System::Object^ sender,
System::EventArgs^ e)
{
System::Drawing...
Graphics.DrawLine Method
•
•
•
•

public: void DrawLine(Pen*, Point, Point);
public: void DrawLine(Pen*, PointF, PointF);
...
System::Drawing::Pen
private: System::Void button1_Click_5(System::Object^ sender,
System::EventArgs^ e)
{
System::Drawing...
System::Drawing
System::Drawing
• What will happen now?
private: System::Void button1_Click_5(System::Object^ sender,
System::EventArgs^ e...
System::Drawing
• What will happen now?
private: System::Void button1_Click_5(System::Object^ sender,
System::EventArgs^ e...
System::Drawing
• What will happen now?
private: System::Void button1_Click_5(System::Object^ sender, System::EventArgs^
e...
System::Drawing
• After clicking the button
System::Drawing
• Can we do this?
private: System::Void button1_Click_5(System::Object^ sender,
System::EventArgs^ e)
{
Dr...
System::Drawing
• But remember we can just do this
private: System::Void button1_Click_5(System::Object^ sender, System::E...
System::Drawing
• What will happen now?
private: System::Void button1_Click_5(System::Object^ sender,
System::EventArgs^ e...
System::Drawing
• What’s missing? Now we’ll find out!
System::Drawing
• Now, let’s have the following
private: System::Void button1_Click_5(System::Object^ sender,
System::Even...
System::Drawing
System::Drawing
• Now, when scrolling the line should be appeared, right?
System::Drawing
• Now moving the scroll forward

Nothing!
System::Drawing
• Now moving the scroll backward

Nothing!
Control.Paint() Event
• Control.Paint Event
– Occurs when the control is redrawn
Control.Paint() Event
Control.Paint() Event

private: System::Void pictureBox1_Paint(System::Object^
sender, System::Windows::Forms::PaintEventA...
System::Drawing
• How to solve this?!
– Storing the lines* we have in ??
• Linked list / List for example

– Then, when re...
System::Drawing
• Header of class Line
#pragma once
ref class Line
{
public:
System::Drawing::Point P1;
System::Drawing::P...
System::Drawing
• cpp of class Line
#include "StdAfx.h"
#include "Line.h"
Line::Line()
{
P1 = System::Drawing::Point(0, 0)...
System::Drawing
• Back to Form1
private: Drawing::Graphics ^MyGraphics;
private: Pen ^MyPen;
private: System::Collections:...
System::Drawing
private: System::Void button1_Click_6(System::Object^ sender,
System::EventArgs^ e)
{
Drawing::Point P1(3,...
System::Drawing
• Form1
private: System::Void pictureBox1_Paint(System::Object^
sender, System::Windows::Forms::PaintEvent...
System::Drawing
• Before clicking anything
System::Drawing
• After clicking button6 “filling the linkedList with values”
System::Drawing
• After starting to scroll which means start painting “repainting”
the pictureBox
System::Drawing
•

What should happen now after continuing to scroll?
System::Drawing
• Scroll Forward
System::Drawing
• Now, when re-scrolling back it’s there!
Clear Method
• Clears the entire drawing surface and fills it with the
specified background color.
Clear Method
• What happens now?
private: System::Void button1_Click_5(System::Object^ sender,
System::EventArgs^ e)
{
MyG...
Clear Method
Refresh Method
• What will happen now?
private: System::Void button1_Click_5(System::Object^ sender,
System::EventArgs^ e)...
Refresh Method
Refresh Method
• Now, when pressing “TRY” <<Refresh method caller>> button
Refresh Method
• When scrolling again!
Refresh Method
• Now, when pressing TRY again
Refresh Method
• When scrolling again!
Refresh Method
• We usually use Refresh method with timers to refresh the
data we have in the pictureBox
• But note!
– Ref...
private:
Rectangle RcDraw;
void Form1_MouseDown( Object^ /*sender*/, System::Windows::Forms::MouseEventArgs^ e )
{ // Dete...
Graphics::Draw
• You can draw whatever you want!
Example : Graphics::DrawString
public:
void DrawStringRectangleF( PaintEventArgs^ e )
{
// Create string to draw.
String^ ...
Drawing with timer
private: System::Void timer1_Tick(System::Object^ sender,
System::EventArgs^ e)
{
static int x1 = 0, x2...
Drawing with timer
Drawing with timer
• What will happen now?
private: System::Void timer1_Tick(System::Object^ sender,
System::EventArgs^ e)...
Drawing with timer
Drawing with timer
• What will happen now?
private: System::Void timer1_Tick(System::Object^ sender,
System::EventArgs^ e)...
Drawing with timer
Drawing with sleep
private: System::Void button1_Click_6(System::Object^ sender,
System::EventArgs^ e)
{
for(int i = 0; i ...
Drawing with sleep
Drawing with sleep –
Refresh methods
• What will happens? :D
private: System::Void button1_Click_6(System::Object^ sender,...
Drawing with sleep –
Refresh methods
Refresh Method
• Remember :
– Refresh Method :
• Forces the control to invalidate its client area and immediately redraw
i...
Drawing with sleep and timer!
• What is that?!
private: System::Void timer1_Tick(System::Object^ sender,
System::EventArgs...
Drawing with sleep and timer!
Drawing with sleep and timer!
• What is that?!
private: System::Void timer1_Tick(System::Object^ sender,
System::EventArgs...
Drawing with sleep and timer!
Now, every two
seconds a line will
be drawn and
cleared for another
one to be shown
Drawing with angles?
private: System::Void timer1_Tick(System::Object^ sender,
System::EventArgs^ e)
{
static int x1 = 0, ...
Drawing with angles?
Drawing with angles?
public : Drawing::Pen ^MyPen;
private: System::Void timer1_Tick(System::Object^ sender,
System::Event...
Drawing with angles?
Drawing with angles?
Drawing with angles?
• Precedence?
public : Drawing::Pen ^MyPen;
private: System::Void timer1_Tick(System::Object^ sender,...
Drawing And filling
• Brush :
– Defines objects used to fill the interiors of graphical shapes such as
rectangles, ellipse...
Drawing And filling
Drawing And filling
• What will happen now?
private: System::Void button1_Click_6(System::Object^ sender,
System::EventArg...
Drawing And filling
private: System::Void button1_Click_6(System::Object^ sender,
System::EventArgs^ e)
{
Drawing::Rectang...
Drawing And filling
private: System::Void button1_Click_6(System::Object^ sender,
System::EventArgs^ e)
{
Drawing::Rectang...
Drawing And filling
Drawing And filling
private: System::Void button1_Click_6(System::Object^ sender,
System::EventArgs^ e)
{
Drawing::Rectang...
Drawing And filling
Drawing And filling
Drawing And filling
private: System::Void button1_Click_6(System::Object^ sender,
System::EventArgs^ e)
{
MyGraphics = pan...
Drawing And filling
Drawing And filling
Drawing And filling
Drawing And filling
• Drawing on panel!
Event Handling
Event Handling
• Let’s have the following form
Event Handling
private: System::Void button1_Click(System::Object^ sender,
System::EventArgs^ e)
{
MessageBox::Show(sender...
Event Handling
private: System::Void button1_Click(System::Object^ sender,
System::EventArgs^ e)
{
MessageBox::Show(e->ToS...
Event Handling
• Now, let’s see this with pictureBox and paint event
– Run the project with the following code
private: Sy...
After pressing ok for the first time

Another window re-open!
After pressing ok for the second time
Now, resizing!
Resize again, Nothing happens!, WHY?
Enlarge again
Enlarge again and again and this continues.. But till when?
Use the console to trackdebug values!
Do not use messagebox for tracking!
(see it in utility slide)
More on GDI
Graphics.DrawBezier Method
• Draws a Bézier spline defined by four Point structures.
Name

Description

DrawBezier(Pen, Po...
Graphics.DrawBezier Method
private: System::Void pictureBox1_Paint(System::Object^
sender, System::Windows::Forms::PaintEv...
Graphics.DrawBezier Method
private: System::Void pictureBox1_Paint(System::Object^
sender, System::Windows::Forms::PaintEv...
Region Class
• Describes the interior of a graphics shape composed of
rectangles and paths. This class cannot be inherited...
Path
• Represents a series of connected lines and curves
LinearGradientBrush
Name
LinearGradientBrush(Point, Point, Color, Color)
LinearGradientBrush(PointF, PointF, Color, Color)...
Graphics.DrawBezier Method
• How To draw this?
More Graphics -_-
AddMetafileComment
BeginContainer

Adds a comment to the current Metafile object.
Overloaded. Saves a graphics container w...
DrawIconUnstretched

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

DrawImageSupport...
EndContainer

EnumerateMetafile
Equals(inherited from Object)Supported
by the.NET Compact Framework.
ExcludeClip

Closes t...
private:
void AddShadow( PaintEventArgs^ e )
{
// Create two SizeF objects.
SizeF shadowSize = listBox1->Size;
SizeF addSi...
Drawing And filling - RegionData
• Example :
– The following example is designed for use with Windows Forms, and it
requir...
public:
void GetRegionDataExample( PaintEventArgs^ e )
{
// Create a rectangle and draw it to the screen in black.
Rectang...
That’s it for today!
C++ Windows Forms L09 - GDI P2
C++ Windows Forms L09 - GDI P2
C++ Windows Forms L09 - GDI P2
C++ Windows Forms L09 - GDI P2
C++ Windows Forms L09 - GDI P2
C++ Windows Forms L09 - GDI P2
C++ Windows Forms L09 - GDI P2
C++ Windows Forms L09 - GDI P2
C++ Windows Forms L09 - GDI P2
C++ Windows Forms L09 - GDI P2
C++ Windows Forms L09 - GDI P2
C++ Windows Forms L09 - GDI P2
C++ Windows Forms L09 - GDI P2
C++ Windows Forms L09 - GDI P2
C++ Windows Forms L09 - GDI P2
Upcoming SlideShare
Loading in...5
×

C++ Windows Forms L09 - GDI P2

207

Published on

C++ Windows Forms L09 - GDI P2 of C++ Windows Forms Light Course

Published in: Technology, Education, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
207
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
33
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "C++ Windows Forms L09 - GDI P2"

  1. 1. C++.NET Windows Forms Course L09- GDI Part 2 Mohammad Shaker mohammadshakergtr.wordpress.com C++.NET Windows Forms Course @ZGTRShaker
  2. 2. GDI - Part 1 Quick Revision -
  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. System::Drawing
  8. 8. Drawing::Point • What does this even mean? -_private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { button1->Location = 30,120; }
  9. 9. Drawing::Point
  10. 10. 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; }
  11. 11. Drawing::Point
  12. 12. System::Drawing::Pen private: System::Void button1_Click_5(System::Object^ sender, System::EventArgs^ e) { System::Drawing::Pen ^ MyPen = gcnew Pen(Color::Red); }
  13. 13. 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);
  14. 14. 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); }
  15. 15. System::Drawing
  16. 16. 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
  17. 17. 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
  18. 18. 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!
  19. 19. System::Drawing • After clicking the button
  20. 20. 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
  21. 21. 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 ); }
  22. 22. 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 ); }
  23. 23. System::Drawing • What’s missing? Now we’ll find out!
  24. 24. System::Drawing • Now, let’s have the following 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 = 30; MyGraphics->DrawLine( MyPen, P1, P2 ); }
  25. 25. System::Drawing
  26. 26. System::Drawing • Now, when scrolling the line should be appeared, right?
  27. 27. System::Drawing • Now moving the scroll forward Nothing!
  28. 28. System::Drawing • Now moving the scroll backward Nothing!
  29. 29. Control.Paint() Event • Control.Paint Event – Occurs when the control is redrawn
  30. 30. Control.Paint() Event
  31. 31. Control.Paint() Event private: System::Void pictureBox1_Paint(System::Object^ sender, System::Windows::Forms::PaintEventArgs^ e) { }
  32. 32. System::Drawing • How to solve this?! – Storing the lines* we have in ?? • Linked list / List for example – Then, when re-painting the pictureBox • Re-drawing the hole line all over again! ____________________________________________________ * Note that we don’t have sth called “line” so we need to wrap it oursleves
  33. 33. System::Drawing • Header of class Line #pragma once ref class Line { public: System::Drawing::Point P1; System::Drawing::Point P2; Line(); Line(System::Drawing::Point p1, System::Drawing::Point p2); };
  34. 34. System::Drawing • cpp of class Line #include "StdAfx.h" #include "Line.h" Line::Line() { P1 = System::Drawing::Point(0, 0); P2 = System::Drawing::Point(0, 0); } Line::Line(System::Drawing::Point p1, System::Drawing::Point p2) { P1 = p1; P2 = p2; }
  35. 35. System::Drawing • Back to Form1 private: Drawing::Graphics ^MyGraphics; private: Pen ^MyPen; private: System::Collections::Generic::LinkedList <Line^> ^ MyLinesList; private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { MyGraphics = pictureBox1->CreateGraphics(); MyPen = gcnew Pen(Color::Red); MyLinesList = gcnew System::Collections::Generic::LinkedList <Line^>; }
  36. 36. System::Drawing private: System::Void button1_Click_6(System::Object^ sender, System::EventArgs^ e) { Drawing::Point P1(3,5); Drawing::Point P2(200,50); Line^ L1 = gcnew Line(P1, P2); MyLinesList->AddLast(L1); Drawing::Point P3(30,50); Drawing::Point P4(500,20); Line^ L2 = gcnew Line(P3, P4); MyLinesList->AddLast(L2); }
  37. 37. System::Drawing • Form1 private: System::Void pictureBox1_Paint(System::Object^ sender, System::Windows::Forms::PaintEventArgs^ e) { for each(Line ^MyLine in MyLinesList) { Drawing::Point P1 = MyLine->P1; Drawing::Point P2 = MyLine->P2; MyGraphics->DrawLine(MyPen,P1, P2 ); } }
  38. 38. System::Drawing • Before clicking anything
  39. 39. System::Drawing • After clicking button6 “filling the linkedList with values”
  40. 40. System::Drawing • After starting to scroll which means start painting “repainting” the pictureBox
  41. 41. System::Drawing • What should happen now after continuing to scroll?
  42. 42. System::Drawing • Scroll Forward
  43. 43. System::Drawing • Now, when re-scrolling back it’s there!
  44. 44. Clear Method • Clears the entire drawing surface and fills it with the specified background color.
  45. 45. Clear Method • What happens now? private: System::Void button1_Click_5(System::Object^ sender, System::EventArgs^ e) { MyGraphics->Clear(Color::SkyBlue); }
  46. 46. Clear Method
  47. 47. Refresh Method • What will happen now? private: System::Void button1_Click_5(System::Object^ sender, System::EventArgs^ e) { pictureBox1->Refresh(); }
  48. 48. Refresh Method
  49. 49. Refresh Method • Now, when pressing “TRY” <<Refresh method caller>> button
  50. 50. Refresh Method • When scrolling again!
  51. 51. Refresh Method • Now, when pressing TRY again
  52. 52. Refresh Method • When scrolling again!
  53. 53. Refresh Method • We usually use Refresh method with timers to refresh the data we have in the pictureBox • But note! – Refresh Method : • Forces the control to invalidate its client area and immediately redraw itself and any child controls. – So that, now, when we refresh the pictureBox all the lines will be cleared!!! – So we should store them and redraw them all over again in each paint!!!
  54. 54. private: Rectangle RcDraw; void Form1_MouseDown( Object^ /*sender*/, System::Windows::Forms::MouseEventArgs^ e ) { // Determine the initial rectangle coordinates. RcDraw.X = e->X; RcDraw.Y = e->Y; } void Form1_MouseUp( Object^ /*sender*/, System::Windows::Forms::MouseEventArgs^ e ) { // Determine the width and height of the rectangle. if( e->X < RcDraw.X ) { RcDraw.Width = RcDraw.X - e->X; RcDraw.X = e->X; } else { RcDraw.Width = e->X - RcDraw.X; } if( e->Y < RcDraw.Y ) { RcDraw.Height = RcDraw.Y - e->Y; RcDraw.Y = e->Y; } else { RcDraw.Height = e->Y - RcDraw.Y; } // Force a repaint of the region occupied by the rectangle. this->Invalidate( RcDraw ); } void Form1_Paint( Object^ /*sender*/, System::Windows::Forms::PaintEventArgs^ e ) { // Draw the rectangle. float PenWidth = 5; e->Graphics->DrawRectangle( gcnew Pen( Color::Blue,PenWidth ), RcDraw ); }
  55. 55. Graphics::Draw • You can draw whatever you want!
  56. 56. Example : Graphics::DrawString public: void DrawStringRectangleF( PaintEventArgs^ e ) { // Create string to draw. String^ drawString = "Sample Text"; // Create font and brush. System::Drawing::Font^ drawFont = gcnew System::Drawing::Font( "Arial",16 ); SolidBrush^ drawBrush = gcnew SolidBrush( Color::Black ); // Create rectangle for drawing.(FLOATING POINT NUMBERS) float x = 150.0F; float y = 150.0F; float width = 200.0F; float height = 50.0F; RectangleF drawRect = RectangleF(x,y,width,height); // Draw rectangle to screen. Pen^ blackPen = gcnew Pen( Color::Black ); e->Graphics->DrawRectangle( blackPen, x, y, width, height ); // Draw string to screen. e->Graphics->DrawString( drawString, drawFont, drawBrush, drawRect ); }
  57. 57. Drawing with timer private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e) { static int x1 = 0, x2 = 10; static int y1 = 0, y2 = 10; srand(time(0)); x2+= rand()%30; y2+=rand()%30; MyGraphics->DrawLine(MyPen, x1, y1, x2, y2 ); x1 = x2; y1 = y2; } // We can use. Random R; int x2 = R.Next(150); // No seed
  58. 58. Drawing with timer
  59. 59. Drawing with timer • What will happen now? private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e) { static int x1 = 0, x2 = 10; static int y1 = 0, y2 = 10; srand(time(0)); x2+= rand(); y2+=rand(); // no %30 MyGraphics->DrawLine(MyPen, x1, y1, x2, y2 ); x1 = x2; y1 = y2; }
  60. 60. Drawing with timer
  61. 61. Drawing with timer • What will happen now? private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e) { static int x1 = 0, x2 = 10; static int y1 = 0, y2 = 10; x2+= 30; y2+=10; MyGraphics->DrawLine(MyPen, x1, y1, x2, y2 ); x1 = x2; y1 = y2; }
  62. 62. Drawing with timer
  63. 63. Drawing with sleep private: System::Void button1_Click_6(System::Object^ sender, System::EventArgs^ e) { for(int i = 0; i < 10; i++) { static int x1 = 0, x2 = 10; static int y1 = 0, y2 = 10; srand(time(0)); x2+= rand()%40; y2+= rand()%30; MyGraphics->DrawLine(MyPen, x1, y1, x2, y2 ); Threading::Thread::Sleep(1000); x1 = x2; y1 = y2; } }
  64. 64. Drawing with sleep
  65. 65. Drawing with sleep – Refresh methods • What will happens? :D private: System::Void button1_Click_6(System::Object^ sender, System::EventArgs^ e) { for(int i = 0; i < 10; i++) { static int x1 = 0, x2 = 10; static int y1 = 0, y2 = 10; srand(time(0)); x2+= rand()%40; y2+= rand()%30; MyGraphics->DrawLine(MyPen, x1, y1, x2, y2 ); Threading::Thread::Sleep(1000); x1 = x2; y1 = y2; pictureBox1->Refresh(); } }
  66. 66. Drawing with sleep – Refresh methods
  67. 67. Refresh Method • Remember : – Refresh Method : • Forces the control to invalidate its client area and immediately redraw itself and any child controls. – So that, now, when we refresh the pictureBox all the lines will be cleared!!! – So we should store them and redraw them all over again in each paint!!!
  68. 68. Drawing with sleep and timer! • What is that?! private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e) { static int x1 = 0, x2 = 10; static int y1 = 0, y2 = 10; srand(time(0)); x2+= rand()%30; y2+=rand()%30; MyGraphics->DrawLine(MyPen, x1, y1, x2, y2 ); x1 = x2; y1 = y2; Threading::Thread::Sleep(1000); }
  69. 69. Drawing with sleep and timer!
  70. 70. Drawing with sleep and timer! • What is that?! private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e) { static int x1 = 0, x2 = 10; static int y1 = 0, y2 = 10; srand(time(0)); x2+= rand()%30; y2+=rand()%30; MyGraphics->DrawLine(MyPen, x1, y1, x2, y2 ); x1 = x2; y1 = y2; Threading::Thread::Sleep(1000); pictureBox1->Refresh(); }
  71. 71. Drawing with sleep and timer! Now, every two seconds a line will be drawn and cleared for another one to be shown
  72. 72. Drawing with angles? private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e) { static int x1 = 0, x2 = 10; static int y1 = 0, y2 = 10; srand(time(0)); x2+= Math::Cos(rand())*20; y2+= Math::Cos(rand())*20; MyGraphics->DrawLine(MyPen, x1, y1, x2, y2 ); x1 = x2; y1 = y2; }
  73. 73. Drawing with angles?
  74. 74. Drawing with angles? public : Drawing::Pen ^MyPen; private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e) { static int x1 = 0, x2 = 10; static int y1 = 0, y2 = 10; srand(time(0)); x2+= Math::Cos(rand())*(rand()%30); y2+= Math::Cos(rand())*(rand()%30); MyGraphics->DrawLine(MyPen, x1, y1, x2, y2 ); x1 = x2; y1 = y2; } };
  75. 75. Drawing with angles?
  76. 76. Drawing with angles?
  77. 77. Drawing with angles? • Precedence? public : Drawing::Pen ^MyPen; private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e) { static int x1 = 0, x2 = 10; static int y1 = 0, y2 = 10; srand(time(0)); x2+= Math::Cos(rand())*rand()%30; y2+= Math::Cos(rand())*rand()%30; MyGraphics->DrawLine(MyPen, x1, y1, x2, y2 ); x1 = x2; y1 = y2; } }; •Also remember that we can use for sincos • Math::PI
  78. 78. Drawing And filling • Brush : – Defines objects used to fill the interiors of graphical shapes such as rectangles, ellipses, pies, polygons, and paths.
  79. 79. Drawing And filling
  80. 80. Drawing And filling • What will happen now? private: System::Void button1_Click_6(System::Object^ sender, System::EventArgs^ e) { Drawing::Rectangle MyRect(50,30,60,100); Drawing::Brush ^MyBrush; MyGraphics->FillEllipse(MyBrush, MyRect ); } Compiler error
  81. 81. Drawing And filling private: System::Void button1_Click_6(System::Object^ sender, System::EventArgs^ e) { Drawing::Rectangle MyRect(50,30,60,100); Drawing::Brush ^MyBrush; MyBrush = gcnew Drawing::Brush; MyGraphics->FillEllipse(MyBrush, MyRect ); } Compiler error
  82. 82. Drawing And filling private: System::Void button1_Click_6(System::Object^ sender, System::EventArgs^ e) { Drawing::Rectangle MyRect(50,30,60,100); Drawing::Brush ^MyBrush; MyBrush = gcnew Drawing::SolidBrush(Color ::Yellow); MyGraphics->FillEllipse(MyBrush, MyRect ); }
  83. 83. Drawing And filling
  84. 84. Drawing And filling private: System::Void button1_Click_6(System::Object^ sender, System::EventArgs^ e) { Drawing::Rectangle MyRect(50,30,60,100); Drawing::Brush ^MyBrush; MyBrush = gcnew Drawing::SolidBrush(Color ::Brown); MyGraphics->FillEllipse(MyBrush, MyRect ); }
  85. 85. Drawing And filling
  86. 86. Drawing And filling
  87. 87. Drawing And filling private: System::Void button1_Click_6(System::Object^ sender, System::EventArgs^ e) { MyGraphics = panel1->CreateGraphics(); Drawing::Rectangle MyRect(50,30,60,100); Drawing::Brush ^MyBrush; MyBrush = gcnew Drawing::SolidBrush(Color ::Chocolate); Drawing::Region ^MyRegion; MyRegion = gcnew Drawing::Region(MyRect); MyGraphics->FillRegion(MyBrush, MyRegion ); }
  88. 88. Drawing And filling
  89. 89. Drawing And filling
  90. 90. Drawing And filling
  91. 91. Drawing And filling • Drawing on panel!
  92. 92. Event Handling
  93. 93. Event Handling • Let’s have the following form
  94. 94. Event Handling private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { MessageBox::Show(sender->ToString()); }
  95. 95. Event Handling private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { MessageBox::Show(e->ToString()); }
  96. 96. Event Handling • Now, let’s see this with pictureBox and paint event – Run the project with the following code private: System::Void pictureBox1_Paint(System::Object^ sender, System::Windows::Forms::PaintEventArgs^ e) { MessageBox::Show(e->ToString()); }
  97. 97. After pressing ok for the first time Another window re-open!
  98. 98. After pressing ok for the second time
  99. 99. Now, resizing!
  100. 100. Resize again, Nothing happens!, WHY?
  101. 101. Enlarge again
  102. 102. Enlarge again and again and this continues.. But till when?
  103. 103. Use the console to trackdebug values! Do not use messagebox for tracking! (see it in utility slide)
  104. 104. More on GDI
  105. 105. Graphics.DrawBezier Method • Draws a Bézier spline defined by four Point structures. Name Description 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.
  106. 106. Graphics.DrawBezier Method private: System::Void pictureBox1_Paint(System::Object^ sender, System::Windows::Forms::PaintEventArgs^ e) { Pen ^ MyPen = gcnew Pen(Color::Red); e->Graphics->DrawBezier(MyPen,20,20,50,50,90,90,70,70); }
  107. 107. Graphics.DrawBezier Method private: System::Void pictureBox1_Paint(System::Object^ sender, System::Windows::Forms::PaintEventArgs^ e) { Pen ^ MyPen = gcnew Pen(Color::Red); e->Graphics->DrawBezier(MyPen,20,20,50,90,90,90,70,70); }
  108. 108. Region Class • Describes the interior of a graphics shape composed of rectangles and paths. This class cannot be inherited. • Exclude Union Xor Intersect • public: void Exclude( Rectangle rect )
  109. 109. Path • Represents a series of connected lines and curves
  110. 110. LinearGradientBrush Name LinearGradientBrush(Point, Point, Color, Color) LinearGradientBrush(PointF, PointF, Color, Color) LinearGradientBrush(Rectangle, Color, Color, LinearGradientMode) LinearGradientBrush(Rectangle, Color, Color, Single) LinearGradientBrush(RectangleF, Color, Color, LinearGradientMode) LinearGradientBrush(RectangleF, Color, Color, Single) LinearGradientBrush(Rectangle, Color, Color, Single, Boolean) LinearGradientBrush(RectangleF, Color, Color, Single, Boolean) Description Initializes a new instance of the LinearGradientBrush class with the specified points and colors. Initializes a new instance of the LinearGradientBrush class with the specified points and colors. Creates a new instance of the LinearGradientBrush class based on a rectangle, starting and ending colors, and orientation. Creates a new instance of the LinearGradientBrush class based on a rectangle, starting and ending colors, and an orientation angle. Creates a new instance of the LinearGradientBrush based on a rectangle, starting and ending colors, and an orientation mode. Creates a new instance of the LinearGradientBrush class based on a rectangle, starting and ending colors, and an orientation angle. Creates a new instance of the LinearGradientBrush class based on a rectangle, starting and ending colors, and an orientation angle. Creates a new instance of the LinearGradientBrush class based on a rectangle, starting and ending colors, and an orientation angle.
  111. 111. Graphics.DrawBezier Method • How To draw this?
  112. 112. More Graphics -_-
  113. 113. AddMetafileComment BeginContainer Adds a comment to the current Metafile object. Overloaded. Saves a graphics container with the current state of this Graphics object and opens and uses a new graphics container. Clears the entire drawing surface and fills it with the specified background color. ClearSupported by the.NET Compact Framework. CreateObjRef(inherited Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. from MarshalByRefObject) DisposeSupported by the.NET Releases all resources used by this Graphics object. Compact Framework. DrawArc DrawBezier Overloaded. Draws an arc representing a portion of an ellipse specified by a pair of coordinates, a width, and a height. Overloaded. Draws a Bzier spline defined by four Pointstructures. DrawBeziers Overloaded. Draws a series of Bzier splines from an array of Point structures. DrawClosedCurve Overloaded. Draws a closed cardinal spline defined by an array of Point structures. DrawCurve Overloaded. Draws a cardinal spline through a specified array of Point structures. DrawEllipseSupported by Overloaded. Draws an ellipse defined by a bounding rectangle specified by a pair of the.NET Compact Framework. coordinates, a height, and a width. DrawIconSupported by Overloaded. Draws the image represented by the specified Icon object at the specified the.NET Compact Framework. coordinates.
  114. 114. DrawIconUnstretched Draws the image represented by the specified Icon object without scaling the image. DrawImageSupported by the.NET Compact Framework. Overloaded. Draws the specified Image object at the specified location and with the original size. DrawImageUnscaled Overloaded. Draws the specified image using its original physical size at the location specified by a coordinate pair. DrawLineSupported by the.NET Compact Framework. Overloaded. Draws a line connecting the two points specified by coordinate pairs. DrawLines Overloaded. Draws a series of line segments that connect an array of Point structures. DrawPath DrawPie Draws a GraphicsPath object. Overloaded. Draws a pie shape defined by an ellipse specified by a coordinate pair, a width, and a height and two radial lines. DrawPolygonSupported by the.NET Overloaded. Draws a polygon defined by an array ofPoint structures. Compact Framework. DrawRectangleSupported by the.NET Overloaded. Draws a rectangle specified by a coordinate pair, a width, and a Compact Framework. height. DrawRectangles Overloaded. Draws a series of rectangles specified byRectangle structures. DrawStringSupported by the.NET Compact Framework. Overloaded. Draws the specified text string at the specified location with the specified Brush and Fontobjects.
  115. 115. EndContainer EnumerateMetafile Equals(inherited from Object)Supported by the.NET Compact Framework. ExcludeClip Closes the current graphics container and restores the state of this Graphics object to the state saved by a call to the BeginContainer method. Overloaded. Sends the records in the specified Metafileobject, one at a time, to a callback method for display at a specified point. Overloaded. Determines whether two Object instances are equal. Overloaded. Updates the clip region of this Graphicsobject to exclude the area specified by a Rectanglestructure. FillClosedCurve Overloaded. Fills the interior a closed cardinal spline curve defined by an array of Point structures. FillEllipseSupported by the.NET Compact Overloaded. Fills the interior of an ellipse defined by a bounding rectangle Framework. specified by a pair of coordinates, a width, and a height. FillPath Fills the interior of a GraphicsPath object. FillPie Overloaded. Fills the interior of a pie section defined by an ellipse specified by a pair of coordinates, a width, and a height and two radial lines. FillPolygonSupported by the.NET Overloaded. Fills the interior of a polygon defined by an array of points Compact Framework. specified by Point structures. FillRectangleSupported by the.NET Overloaded. Fills the interior of a rectangle specified by a pair of coordinates, Compact Framework. a width, and a height. FillRectangles Overloaded. Fills the interiors of a series of rectangles specified by Rectangle structures. FillRegionSupported by the.NET Compact Fills the interior of a Region object. Framework.
  116. 116. private: void AddShadow( PaintEventArgs^ e ) { // Create two SizeF objects. SizeF shadowSize = listBox1->Size; SizeF addSize = SizeF(10.5F,20.8F); Adding Shadow! // Add them together and save the result in shadowSize. shadowSize = shadowSize + addSize; // Get the location of the ListBox and convert it to a PointF. PointF shadowLocation = listBox1->Location; // Add two points to get a new location. shadowLocation = shadowLocation + System::Drawing::Size( 5, 5 ); // Create a rectangleF. RectangleF rectFToFill = RectangleF(shadowLocation,shadowSize); // Create a custom brush using a semi-transparent color, and // then fill in the rectangle. Color customColor = Color::FromArgb( 50, Color::Gray ); SolidBrush^ shadowBrush = gcnew SolidBrush( customColor ); array<RectangleF>^ temp0 = {rectFToFill}; e->Graphics->FillRectangles( shadowBrush, temp0 ); // Dispose of the brush. delete shadowBrush; }
  117. 117. Drawing And filling - RegionData • Example : – The following example is designed for use with Windows Forms, and it requires PaintEventArgse, which is a parameter of the Paint event handler. The code performs the following actions: • • • • Creates a rectangle and draw its to the screen in black. Creates a region using the rectangle. Gets the RegionData. Draws the region data(an array of bytes) to the screen, by using the DisplayRegionData helper function.
  118. 118. public: void GetRegionDataExample( PaintEventArgs^ e ) { // Create a rectangle and draw it to the screen in black. Rectangle regionRect = Rectangle(20,20,100,100); e->Graphics->DrawRectangle( Pens::Black, regionRect ); RegionData // Create a region using the first rectangle. System::Drawing::Region^ myRegion = gcnew System::Drawing::Region( regionRect ); // Get the RegionData for this region. RegionData^ myRegionData = myRegion->GetRegionData(); int myRegionDataLength = myRegionData->Data->Length; DisplayRegionData( e, myRegionDataLength, myRegionData ); } void DisplayRegionData( PaintEventArgs^ e, int len, RegionData^ dat ) { // Display the result. int i; float x = 20,y = 140; System::Drawing::Font^ myFont = gcnew System::Drawing::Font( "Arial",8 ); SolidBrush^ myBrush = gcnew SolidBrush( Color::Black ); e->Graphics->DrawString( "myRegionData = ", myFont, myBrush, PointF(x,y) ); y = 160; for( i = 0; i < len; i++ ) { if( x > 300 ) { y += 20; x = 20; } e->Graphics->DrawString( dat->Data[ i ].ToString(), myFont, myBrush, PointF(x,y) ); x += 30; } }
  119. 119. That’s it for today!
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×