9. Drawing::Point
• What does this even mean? -_private: System::Void button1_Click(System::Object^
sender, System::EventArgs^ e)
{
button1->Location = 30,120;
}
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);
};
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 ); }
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);
}
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.
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());
}
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.
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 )
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;
}
}