SlideShare a Scribd company logo
C++.NET
Windows Forms Course
L09- GDI Part 2

Mohammad Shaker
mohammadshakergtr.wordpress.com
C++.NET Windows Forms Course
@ZGTRShaker
C++ Windows Forms L09 - GDI P2
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* : Graphical Device Interface
Drawing::Graphics

private: System::Void button1_Click_5(System::Object^
sender, System::EventArgs^ e)
{
Drawing::Graphics ^MyGraphics;
MyGraphics = pictureBox1->CreateGraphics();
}
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);
}
System::Drawing
Drawing::Point
• What does this even mean? -_private: System::Void button1_Click(System::Object^
sender, System::EventArgs^ e)
{
button1->Location = 30,120;
}
Drawing::Point
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;
}
Drawing::Point
System::Drawing::Pen
private: System::Void button1_Click_5(System::Object^ sender,
System::EventArgs^ e)
{
System::Drawing::Pen ^ MyPen = gcnew Pen(Color::Red);
}
C++ Windows Forms L09 - GDI P2
C++ Windows Forms L09 - GDI P2
C++ Windows Forms L09 - GDI P2
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);
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);
}
C++ Windows Forms L09 - GDI P2
System::Drawing
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
C++ Windows Forms L09 - GDI P2
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
C++ Windows Forms L09 - GDI P2
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!
System::Drawing
• After clicking the button
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
C++ Windows Forms L09 - GDI P2
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 );
}
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 );
}
System::Drawing
• What’s missing? Now we’ll find out!
C++ Windows Forms L09 - GDI P2
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 );
}
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::PaintEventArgs^ e)
{

}
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
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);
};
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;
}
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^>;
}
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);
}
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 );
}
}
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.
C++ Windows Forms L09 - GDI P2
Clear Method
• What happens now?
private: System::Void button1_Click_5(System::Object^ sender,
System::EventArgs^ e)
{
MyGraphics->Clear(Color::SkyBlue);
}
Clear Method
Refresh Method
• What will happen now?
private: System::Void button1_Click_5(System::Object^ sender,
System::EventArgs^ e)
{
pictureBox1->Refresh();
}
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!
– 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!!!
C++ Windows Forms L09 - GDI P2
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 ); }
C++ Windows Forms L09 - GDI P2
Graphics::Draw
• You can draw whatever you want!
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 );
}
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
Drawing with timer
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;
}
Drawing with timer
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;
}
Drawing with timer
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;
}
}
Drawing with sleep
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();
}
}
Drawing with sleep –
Refresh methods
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!!!
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);
}
Drawing with sleep and timer!
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();
}
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, 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;
}
Drawing with angles?
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;
}
};
Drawing with angles?
Drawing with angles?
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
Drawing And filling
• Brush :
– Defines objects used to fill the interiors of graphical shapes such as
rectangles, ellipses, pies, polygons, and paths.
C++ Windows Forms L09 - GDI P2
Drawing And filling
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
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
C++ Windows Forms L09 - GDI P2
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 );
}
Drawing And filling
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 );
}
Drawing And filling
Drawing And filling
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 );
}
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->ToString());
}
Event Handling
private: System::Void button1_Click(System::Object^ sender,
System::EventArgs^ e)
{
MessageBox::Show(e->ToString());
}
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());
}
C++ Windows Forms L09 - GDI P2
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, 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.
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);
}
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);
}
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 )
Path
• Represents a series of connected lines and curves
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.
Graphics.DrawBezier Method
• How To draw this?
More Graphics -_-
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.
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.
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.
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;
}
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.
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;
}
}
That’s it for today!

More Related Content

PDF
C++ Windows Forms L11 - Inheritance
PDF
C++ Windows Forms L05 - Controls P4
PDF
C++ Windows Forms L10 - Instantiate
PDF
C++ Windows Forms L02 - Controls P1
PDF
C++ Windows Forms L03 - Controls P2
PDF
C++ Windows Forms L06 - Utlitity and Strings
PDF
C++ Windows Forms L04 - Controls P3
PDF
C++ Windows Forms L08 - GDI P1
C++ Windows Forms L11 - Inheritance
C++ Windows Forms L05 - Controls P4
C++ Windows Forms L10 - Instantiate
C++ Windows Forms L02 - Controls P1
C++ Windows Forms L03 - Controls P2
C++ Windows Forms L06 - Utlitity and Strings
C++ Windows Forms L04 - Controls P3
C++ Windows Forms L08 - GDI P1

What's hot (20)

PDF
C++ Windows Forms L07 - Collections
PDF
final project for C#
PDF
Mocks introduction
DOCX
Java programs
PDF
The Ring programming language version 1.5.2 book - Part 7 of 181
PPTX
Apache Flink Training: DataStream API Part 2 Advanced
PDF
Writing Domain-Specific Languages for BeepBeep
PPTX
Apache Flink Training: DataSet API Basics
PDF
Do you Promise?
DOCX
Dotnet 18
PDF
SISTEMA DE FACTURACION (Ejemplo desarrollado)
PDF
Functional Stream Processing with Scalaz-Stream
PPTX
Angular2 rxjs
PDF
Visual Studio.Net - Sql Server
PPTX
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
PDF
Sistema de ventas
PPT
Hill ch2ed3
ODP
Pick up the low-hanging concurrency fruit
PDF
Vasia Kalavri – Training: Gelly School
PDF
The Ring programming language version 1.7 book - Part 7 of 196
C++ Windows Forms L07 - Collections
final project for C#
Mocks introduction
Java programs
The Ring programming language version 1.5.2 book - Part 7 of 181
Apache Flink Training: DataStream API Part 2 Advanced
Writing Domain-Specific Languages for BeepBeep
Apache Flink Training: DataSet API Basics
Do you Promise?
Dotnet 18
SISTEMA DE FACTURACION (Ejemplo desarrollado)
Functional Stream Processing with Scalaz-Stream
Angular2 rxjs
Visual Studio.Net - Sql Server
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Sistema de ventas
Hill ch2ed3
Pick up the low-hanging concurrency fruit
Vasia Kalavri – Training: Gelly School
The Ring programming language version 1.7 book - Part 7 of 196
Ad

Similar to C++ Windows Forms L09 - GDI P2 (20)

PDF
openFrameworks – 関数・クラス、オブジェクト指向プログラミング導入 - 多摩美メディアアートII
DOCX
Computer graphics
PDF
Delphi L06 GDI Drawing
KEY
Sbaw090623
PDF
Cg lab cse-v (1) (1)
PDF
bfd23fd7-0d89-45c0-8b82-c991b30ed375.pdf
PDF
Composite Pattern
PDF
borland-style-graphics-for-dev-c.pdf
PPTX
C game programming - SDL
PPT
13 graph classes_2
PDF
Computer Graphics Lab
PPT
VB Lines and Shapes
PDF
Intake 38 6
PDF
C# Summer course - Lecture 2
PDF
Programming using opengl in visual c++
PPTX
MFC Colors app
PPT
13 Graph Classes
PDF
Computer Graphics Concepts
PPTX
openFrameworks – 関数・クラス、オブジェクト指向プログラミング導入 - 多摩美メディアアートII
Computer graphics
Delphi L06 GDI Drawing
Sbaw090623
Cg lab cse-v (1) (1)
bfd23fd7-0d89-45c0-8b82-c991b30ed375.pdf
Composite Pattern
borland-style-graphics-for-dev-c.pdf
C game programming - SDL
13 graph classes_2
Computer Graphics Lab
VB Lines and Shapes
Intake 38 6
C# Summer course - Lecture 2
Programming using opengl in visual c++
MFC Colors app
13 Graph Classes
Computer Graphics Concepts
Ad

More from Mohammad Shaker (20)

PDF
12 Rules You Should to Know as a Syrian Graduate
PDF
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
PDF
Interaction Design L06 - Tricks with Psychology
PDF
Short, Matters, Love - Passioneers Event 2015
PDF
Unity L01 - Game Development
PDF
Android L07 - Touch, Screen and Wearables
PDF
Interaction Design L03 - Color
PDF
Interaction Design L05 - Typography
PDF
Interaction Design L04 - Materialise and Coupling
PDF
Android L05 - Storage
PDF
Android L04 - Notifications and Threading
PDF
Android L09 - Windows Phone and iOS
PDF
Interaction Design L01 - Mobile Constraints
PDF
Interaction Design L02 - Pragnanz and Grids
PDF
Android L10 - Stores and Gaming
PDF
Android L06 - Cloud / Parse
PDF
Android L08 - Google Maps and Utilities
PDF
Android L03 - Styles and Themes
PDF
Android L02 - Activities and Adapters
PDF
Android L01 - Warm Up
12 Rules You Should to Know as a Syrian Graduate
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
Interaction Design L06 - Tricks with Psychology
Short, Matters, Love - Passioneers Event 2015
Unity L01 - Game Development
Android L07 - Touch, Screen and Wearables
Interaction Design L03 - Color
Interaction Design L05 - Typography
Interaction Design L04 - Materialise and Coupling
Android L05 - Storage
Android L04 - Notifications and Threading
Android L09 - Windows Phone and iOS
Interaction Design L01 - Mobile Constraints
Interaction Design L02 - Pragnanz and Grids
Android L10 - Stores and Gaming
Android L06 - Cloud / Parse
Android L08 - Google Maps and Utilities
Android L03 - Styles and Themes
Android L02 - Activities and Adapters
Android L01 - Warm Up

Recently uploaded (20)

PPTX
Cloud computing and distributed systems.
PDF
Electronic commerce courselecture one. Pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPT
Teaching material agriculture food technology
PDF
Machine learning based COVID-19 study performance prediction
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Spectroscopy.pptx food analysis technology
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
A Presentation on Artificial Intelligence
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Cloud computing and distributed systems.
Electronic commerce courselecture one. Pdf
Spectral efficient network and resource selection model in 5G networks
Review of recent advances in non-invasive hemoglobin estimation
Chapter 3 Spatial Domain Image Processing.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
MIND Revenue Release Quarter 2 2025 Press Release
The Rise and Fall of 3GPP – Time for a Sabbatical?
Teaching material agriculture food technology
Machine learning based COVID-19 study performance prediction
The AUB Centre for AI in Media Proposal.docx
Dropbox Q2 2025 Financial Results & Investor Presentation
Digital-Transformation-Roadmap-for-Companies.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Spectroscopy.pptx food analysis technology
Encapsulation_ Review paper, used for researhc scholars
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
A Presentation on Artificial Intelligence
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton

C++ Windows Forms L09 - GDI P2

  • 1. C++.NET Windows Forms Course L09- GDI Part 2 Mohammad Shaker mohammadshakergtr.wordpress.com C++.NET Windows Forms Course @ZGTRShaker
  • 3. GDI - Part 1 Quick Revision -
  • 4. What do u need to draw sth? • Pen(stroke width, color, etc) • Paper • Brush to filling your drawing
  • 5. Drawing::Graphic • Encapsulates a GDI* + drawing surface. • This class cannot be inherited. ___________________ • GDI* : Graphical Device Interface
  • 6. Drawing::Graphics private: System::Void button1_Click_5(System::Object^ sender, System::EventArgs^ e) { Drawing::Graphics ^MyGraphics; MyGraphics = pictureBox1->CreateGraphics(); }
  • 7. 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); }
  • 9. Drawing::Point • What does this even mean? -_private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { button1->Location = 30,120; }
  • 11. 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; }
  • 13. System::Drawing::Pen private: System::Void button1_Click_5(System::Object^ sender, System::EventArgs^ e) { System::Drawing::Pen ^ MyPen = gcnew Pen(Color::Red); }
  • 17. 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);
  • 18. 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); }
  • 21. 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
  • 23. 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
  • 25. 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!
  • 27. 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
  • 29. 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 ); }
  • 30. 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 ); }
  • 31. System::Drawing • What’s missing? Now we’ll find out!
  • 33. 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 ); }
  • 35. System::Drawing • Now, when scrolling the line should be appeared, right?
  • 36. System::Drawing • Now moving the scroll forward Nothing!
  • 37. System::Drawing • Now moving the scroll backward Nothing!
  • 38. Control.Paint() Event • Control.Paint Event – Occurs when the control is redrawn
  • 40. Control.Paint() Event private: System::Void pictureBox1_Paint(System::Object^ sender, System::Windows::Forms::PaintEventArgs^ e) { }
  • 41. 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
  • 42. 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); };
  • 43. 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; }
  • 44. 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^>; }
  • 45. 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); }
  • 46. 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 ); } }
  • 48. System::Drawing • After clicking button6 “filling the linkedList with values”
  • 49. System::Drawing • After starting to scroll which means start painting “repainting” the pictureBox
  • 50. System::Drawing • What should happen now after continuing to scroll?
  • 52. System::Drawing • Now, when re-scrolling back it’s there!
  • 53. Clear Method • Clears the entire drawing surface and fills it with the specified background color.
  • 55. Clear Method • What happens now? private: System::Void button1_Click_5(System::Object^ sender, System::EventArgs^ e) { MyGraphics->Clear(Color::SkyBlue); }
  • 57. Refresh Method • What will happen now? private: System::Void button1_Click_5(System::Object^ sender, System::EventArgs^ e) { pictureBox1->Refresh(); }
  • 59. Refresh Method • Now, when pressing “TRY” <<Refresh method caller>> button
  • 60. Refresh Method • When scrolling again!
  • 61. Refresh Method • Now, when pressing TRY again
  • 62. Refresh Method • When scrolling again!
  • 63. 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!!!
  • 65. 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 ); }
  • 67. Graphics::Draw • You can draw whatever you want!
  • 68. 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 ); }
  • 69. 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
  • 71. 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; }
  • 73. 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; }
  • 75. 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; } }
  • 77. 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(); } }
  • 78. Drawing with sleep – Refresh methods
  • 79. 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!!!
  • 80. 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); }
  • 81. Drawing with sleep and timer!
  • 82. 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(); }
  • 83. Drawing with sleep and timer! Now, every two seconds a line will be drawn and cleared for another one to be shown
  • 84. 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; }
  • 86. 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; } };
  • 89. 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
  • 90. Drawing And filling • Brush : – Defines objects used to fill the interiors of graphical shapes such as rectangles, ellipses, pies, polygons, and paths.
  • 93. 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
  • 94. 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
  • 96. 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 ); }
  • 98. 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 ); }
  • 101. 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 ); }
  • 105. Drawing And filling • Drawing on panel!
  • 107. Event Handling • Let’s have the following form
  • 108. Event Handling private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { MessageBox::Show(sender->ToString()); }
  • 109. Event Handling private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { MessageBox::Show(e->ToString()); }
  • 110. 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()); }
  • 112. After pressing ok for the first time Another window re-open!
  • 113. After pressing ok for the second time
  • 115. Resize again, Nothing happens!, WHY?
  • 117. Enlarge again and again and this continues.. But till when?
  • 118. Use the console to trackdebug values! Do not use messagebox for tracking! (see it in utility slide)
  • 120. 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.
  • 121. 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); }
  • 122. 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); }
  • 123. 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 )
  • 124. Path • Represents a series of connected lines and curves
  • 125. 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.
  • 128. 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.
  • 129. 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.
  • 130. 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.
  • 131. 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; }
  • 132. 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.
  • 133. 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; } }
  • 134. That’s it for today!