SlideShare a Scribd company logo
1 of 134
Download to read offline
C++.NET
Windows Forms Course
L09- GDI Part 2

Mohammad Shaker
mohammadshakergtr.wordpress.com
C++.NET Windows Forms Course
@ZGTRShaker
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);
}
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);
}
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
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
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
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!
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.
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!!!
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 ); }
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.
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
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());
}
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

What's hot

C++ Windows Forms L07 - Collections
C++ Windows Forms L07 - CollectionsC++ Windows Forms L07 - Collections
C++ Windows Forms L07 - CollectionsMohammad Shaker
 
Mocks introduction
Mocks introductionMocks introduction
Mocks introductionSperasoft
 
Java programs
Java programsJava programs
Java programsjojeph
 
The Ring programming language version 1.5.2 book - Part 7 of 181
The Ring programming language version 1.5.2 book - Part 7 of 181The Ring programming language version 1.5.2 book - Part 7 of 181
The Ring programming language version 1.5.2 book - Part 7 of 181Mahmoud Samir Fayed
 
Apache Flink Training: DataStream API Part 2 Advanced
Apache Flink Training: DataStream API Part 2 Advanced Apache Flink Training: DataStream API Part 2 Advanced
Apache Flink Training: DataStream API Part 2 Advanced Flink Forward
 
Writing Domain-Specific Languages for BeepBeep
Writing Domain-Specific Languages for BeepBeepWriting Domain-Specific Languages for BeepBeep
Writing Domain-Specific Languages for BeepBeepSylvain Hallé
 
Apache Flink Training: DataSet API Basics
Apache Flink Training: DataSet API BasicsApache Flink Training: DataSet API Basics
Apache Flink Training: DataSet API BasicsFlink Forward
 
Do you Promise?
Do you Promise?Do you Promise?
Do you Promise?jungkees
 
SISTEMA DE FACTURACION (Ejemplo desarrollado)
SISTEMA DE FACTURACION (Ejemplo desarrollado)SISTEMA DE FACTURACION (Ejemplo desarrollado)
SISTEMA DE FACTURACION (Ejemplo desarrollado)Darwin Durand
 
Functional Stream Processing with Scalaz-Stream
Functional Stream Processing with Scalaz-StreamFunctional Stream Processing with Scalaz-Stream
Functional Stream Processing with Scalaz-StreamAdil Akhter
 
Visual Studio.Net - Sql Server
Visual Studio.Net - Sql ServerVisual Studio.Net - Sql Server
Visual Studio.Net - Sql ServerDarwin Durand
 
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...Domenic Denicola
 
Sistema de ventas
Sistema de ventasSistema de ventas
Sistema de ventasDAYANA RETO
 
Pick up the low-hanging concurrency fruit
Pick up the low-hanging concurrency fruitPick up the low-hanging concurrency fruit
Pick up the low-hanging concurrency fruitVaclav Pech
 
Vasia Kalavri – Training: Gelly School
Vasia Kalavri – Training: Gelly School Vasia Kalavri – Training: Gelly School
Vasia Kalavri – Training: Gelly School Flink Forward
 
The Ring programming language version 1.7 book - Part 7 of 196
The Ring programming language version 1.7 book - Part 7 of 196The Ring programming language version 1.7 book - Part 7 of 196
The Ring programming language version 1.7 book - Part 7 of 196Mahmoud Samir Fayed
 

What's hot (20)

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

Similar to C++ Windows Forms L09 - GDI P2

Class program and uml in c++
Class program and uml in c++Class program and uml in c++
Class program and uml in c++Osama Al-Mohaia
 
Csphtp1 16
Csphtp1 16Csphtp1 16
Csphtp1 16HUST
 
Java Graphics
Java GraphicsJava Graphics
Java GraphicsShraddha
 
asmt7~$sc_210_-_assignment_7_fall_15.docasmt7cosc_210_-_as.docx
asmt7~$sc_210_-_assignment_7_fall_15.docasmt7cosc_210_-_as.docxasmt7~$sc_210_-_assignment_7_fall_15.docasmt7cosc_210_-_as.docx
asmt7~$sc_210_-_assignment_7_fall_15.docasmt7cosc_210_-_as.docxfredharris32
 
CS101- Introduction to Computing- Lecture 35
CS101- Introduction to Computing- Lecture 35CS101- Introduction to Computing- Lecture 35
CS101- Introduction to Computing- Lecture 35Bilal Ahmed
 
C# Summer course - Lecture 2
C# Summer course - Lecture 2C# Summer course - Lecture 2
C# Summer course - Lecture 2mohamedsamyali
 
Client-server architecture (clientserver) is a network architecture .pdf
Client-server architecture (clientserver) is a network architecture .pdfClient-server architecture (clientserver) is a network architecture .pdf
Client-server architecture (clientserver) is a network architecture .pdffabmallkochi
 
computer graphics-C/C++-dancingdollcode
computer graphics-C/C++-dancingdollcodecomputer graphics-C/C++-dancingdollcode
computer graphics-C/C++-dancingdollcodeBhavya Chawla
 
The Challenge of Bringing FEZ to PlayStation Platforms
The Challenge of Bringing FEZ to PlayStation PlatformsThe Challenge of Bringing FEZ to PlayStation Platforms
The Challenge of Bringing FEZ to PlayStation PlatformsMiguel Angel Horna
 
The Ring programming language version 1.3 book - Part 83 of 88
The Ring programming language version 1.3 book - Part 83 of 88The Ring programming language version 1.3 book - Part 83 of 88
The Ring programming language version 1.3 book - Part 83 of 88Mahmoud Samir Fayed
 
Creating an Uber Clone - Part IV - Transcript.pdf
Creating an Uber Clone - Part IV - Transcript.pdfCreating an Uber Clone - Part IV - Transcript.pdf
Creating an Uber Clone - Part IV - Transcript.pdfShaiAlmog1
 
Introducing the Microsoft Virtual Earth Silverlight Map Control CTP
Introducing the Microsoft Virtual Earth Silverlight Map Control CTPIntroducing the Microsoft Virtual Earth Silverlight Map Control CTP
Introducing the Microsoft Virtual Earth Silverlight Map Control CTPgoodfriday
 
Adding Love to an API (or How to Expose C++ in Unity)
Adding Love to an API (or How to Expose C++ in Unity)Adding Love to an API (or How to Expose C++ in Unity)
Adding Love to an API (or How to Expose C++ in Unity)Unity Technologies
 
Lec 9 05_sept [compatibility mode]
Lec 9 05_sept [compatibility mode]Lec 9 05_sept [compatibility mode]
Lec 9 05_sept [compatibility mode]Palak Sanghani
 
Csphtp1 06
Csphtp1 06Csphtp1 06
Csphtp1 06HUST
 

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

Intake 37 6
Intake 37 6Intake 37 6
Intake 37 6
 
Intake 38 6
Intake 38 6Intake 38 6
Intake 38 6
 
Class program and uml in c++
Class program and uml in c++Class program and uml in c++
Class program and uml in c++
 
Csphtp1 16
Csphtp1 16Csphtp1 16
Csphtp1 16
 
Java Graphics
Java GraphicsJava Graphics
Java Graphics
 
asmt7~$sc_210_-_assignment_7_fall_15.docasmt7cosc_210_-_as.docx
asmt7~$sc_210_-_assignment_7_fall_15.docasmt7cosc_210_-_as.docxasmt7~$sc_210_-_assignment_7_fall_15.docasmt7cosc_210_-_as.docx
asmt7~$sc_210_-_assignment_7_fall_15.docasmt7cosc_210_-_as.docx
 
CS101- Introduction to Computing- Lecture 35
CS101- Introduction to Computing- Lecture 35CS101- Introduction to Computing- Lecture 35
CS101- Introduction to Computing- Lecture 35
 
C# Summer course - Lecture 2
C# Summer course - Lecture 2C# Summer course - Lecture 2
C# Summer course - Lecture 2
 
Client-server architecture (clientserver) is a network architecture .pdf
Client-server architecture (clientserver) is a network architecture .pdfClient-server architecture (clientserver) is a network architecture .pdf
Client-server architecture (clientserver) is a network architecture .pdf
 
computer graphics-C/C++-dancingdollcode
computer graphics-C/C++-dancingdollcodecomputer graphics-C/C++-dancingdollcode
computer graphics-C/C++-dancingdollcode
 
The Challenge of Bringing FEZ to PlayStation Platforms
The Challenge of Bringing FEZ to PlayStation PlatformsThe Challenge of Bringing FEZ to PlayStation Platforms
The Challenge of Bringing FEZ to PlayStation Platforms
 
The Ring programming language version 1.3 book - Part 83 of 88
The Ring programming language version 1.3 book - Part 83 of 88The Ring programming language version 1.3 book - Part 83 of 88
The Ring programming language version 1.3 book - Part 83 of 88
 
Creating an Uber Clone - Part IV - Transcript.pdf
Creating an Uber Clone - Part IV - Transcript.pdfCreating an Uber Clone - Part IV - Transcript.pdf
Creating an Uber Clone - Part IV - Transcript.pdf
 
Introducing the Microsoft Virtual Earth Silverlight Map Control CTP
Introducing the Microsoft Virtual Earth Silverlight Map Control CTPIntroducing the Microsoft Virtual Earth Silverlight Map Control CTP
Introducing the Microsoft Virtual Earth Silverlight Map Control CTP
 
Adding Love to an API (or How to Expose C++ in Unity)
Adding Love to an API (or How to Expose C++ in Unity)Adding Love to an API (or How to Expose C++ in Unity)
Adding Love to an API (or How to Expose C++ in Unity)
 
31csharp
31csharp31csharp
31csharp
 
31c
31c31c
31c
 
delegates
delegatesdelegates
delegates
 
Lec 9 05_sept [compatibility mode]
Lec 9 05_sept [compatibility mode]Lec 9 05_sept [compatibility mode]
Lec 9 05_sept [compatibility mode]
 
Csphtp1 06
Csphtp1 06Csphtp1 06
Csphtp1 06
 

More from Mohammad Shaker

12 Rules You Should to Know as a Syrian Graduate
12 Rules You Should to Know as a Syrian Graduate12 Rules You Should to Know as a Syrian Graduate
12 Rules You Should to Know as a Syrian GraduateMohammad Shaker
 
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]Mohammad Shaker
 
Interaction Design L06 - Tricks with Psychology
Interaction Design L06 - Tricks with PsychologyInteraction Design L06 - Tricks with Psychology
Interaction Design L06 - Tricks with PsychologyMohammad Shaker
 
Short, Matters, Love - Passioneers Event 2015
Short, Matters, Love -  Passioneers Event 2015Short, Matters, Love -  Passioneers Event 2015
Short, Matters, Love - Passioneers Event 2015Mohammad Shaker
 
Unity L01 - Game Development
Unity L01 - Game DevelopmentUnity L01 - Game Development
Unity L01 - Game DevelopmentMohammad Shaker
 
Android L07 - Touch, Screen and Wearables
Android L07 - Touch, Screen and WearablesAndroid L07 - Touch, Screen and Wearables
Android L07 - Touch, Screen and WearablesMohammad Shaker
 
Interaction Design L03 - Color
Interaction Design L03 - ColorInteraction Design L03 - Color
Interaction Design L03 - ColorMohammad Shaker
 
Interaction Design L05 - Typography
Interaction Design L05 - TypographyInteraction Design L05 - Typography
Interaction Design L05 - TypographyMohammad Shaker
 
Interaction Design L04 - Materialise and Coupling
Interaction Design L04 - Materialise and CouplingInteraction Design L04 - Materialise and Coupling
Interaction Design L04 - Materialise and CouplingMohammad Shaker
 
Android L04 - Notifications and Threading
Android L04 - Notifications and ThreadingAndroid L04 - Notifications and Threading
Android L04 - Notifications and ThreadingMohammad Shaker
 
Android L09 - Windows Phone and iOS
Android L09 - Windows Phone and iOSAndroid L09 - Windows Phone and iOS
Android L09 - Windows Phone and iOSMohammad Shaker
 
Interaction Design L01 - Mobile Constraints
Interaction Design L01 - Mobile ConstraintsInteraction Design L01 - Mobile Constraints
Interaction Design L01 - Mobile ConstraintsMohammad Shaker
 
Interaction Design L02 - Pragnanz and Grids
Interaction Design L02 - Pragnanz and GridsInteraction Design L02 - Pragnanz and Grids
Interaction Design L02 - Pragnanz and GridsMohammad Shaker
 
Android L10 - Stores and Gaming
Android L10 - Stores and GamingAndroid L10 - Stores and Gaming
Android L10 - Stores and GamingMohammad Shaker
 
Android L06 - Cloud / Parse
Android L06 - Cloud / ParseAndroid L06 - Cloud / Parse
Android L06 - Cloud / ParseMohammad Shaker
 
Android L08 - Google Maps and Utilities
Android L08 - Google Maps and UtilitiesAndroid L08 - Google Maps and Utilities
Android L08 - Google Maps and UtilitiesMohammad Shaker
 
Android L03 - Styles and Themes
Android L03 - Styles and Themes Android L03 - Styles and Themes
Android L03 - Styles and Themes Mohammad Shaker
 
Android L02 - Activities and Adapters
Android L02 - Activities and AdaptersAndroid L02 - Activities and Adapters
Android L02 - Activities and AdaptersMohammad Shaker
 

More from Mohammad Shaker (20)

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

Recently uploaded

Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 

Recently uploaded (20)

Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 

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
  • 2.
  • 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); }
  • 14.
  • 15.
  • 16.
  • 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); }
  • 19.
  • 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
  • 22.
  • 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
  • 24.
  • 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
  • 28.
  • 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!
  • 32.
  • 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.
  • 54.
  • 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!!!
  • 64.
  • 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 ); }
  • 66.
  • 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.
  • 91.
  • 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
  • 95.
  • 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()); }
  • 111.
  • 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!