c++ windows forms l09 - gdi p2

Post on 25-Dec-2014

151 Views

Category:

Technology

7 Downloads

Preview:

Click to see full reader

DESCRIPTION

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

TRANSCRIPT

Mohammad Shakermohammadshakergtr.wordpress.com

C++.NET Windows Forms Course@ZGTRShaker

C++.NET Windows Forms Course

L09- GDI Part 2

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::Penprivate: 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::Penprivate: 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::Drawingprivate: 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::DrawStringpublic: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 sin\cos • 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 fillingprivate: 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 fillingprivate: 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 fillingprivate: 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

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

private: System::Void pictureBox1_Paint(System::Object^ sender,

System::Windows::Forms::PaintEventArgs^ e)

{

MessageBox::Show(e->ToString());

}

Event Handling

• Now, let’s see this with pictureBox and paint event– Run the project with the following code

Another window re-open!

After pressing ok for the first time

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 track\debug values!Do not use messagebox for tracking!

(see it in utility slide)

More on GDI

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

• Draws a Bézier spline defined by four Point structures.

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);

}

Graphics.DrawBezier Method

• public: void Exclude( Rectangle rect )

Region Class

• Describes the interior of a graphics shape composed of rectangles and paths. This class cannot be inherited.

• Exclude \ Union \ Xor \ Intersect

Path

• Represents a series of connected lines and curves

Name Description

LinearGradientBrush(Point, Point, Color, Color) Initializes a new instance of the LinearGradientBrush class with the specified points and colors.

LinearGradientBrush(PointF, PointF, Color, Color) Initializes a new instance of the LinearGradientBrush class with the specified points and colors.

LinearGradientBrush(Rectangle, Color, Color, LinearGradientMode)

Creates a new instance of the LinearGradientBrush class based on a rectangle, starting and ending colors, and orientation.

LinearGradientBrush(Rectangle, Color, Color, Single) Creates a new instance of the LinearGradientBrush class based on a rectangle, starting and ending colors, and an orientation angle.

LinearGradientBrush(RectangleF, Color, Color, LinearGradientMode)

Creates a new instance of the LinearGradientBrush based on a rectangle, starting and ending colors, and an orientation mode.

LinearGradientBrush(RectangleF, Color, Color, Single) Creates a new instance of the LinearGradientBrush class based on a rectangle, starting and ending colors, and an orientation angle.

LinearGradientBrush(Rectangle, Color, Color, Single, Boolean)

Creates a new instance of the LinearGradientBrush class based on a rectangle, starting and ending colors, and an orientation angle.

LinearGradientBrush(RectangleF, Color, Color, Single, Boolean)

Creates a new instance of the LinearGradientBrush class based on a rectangle, starting and ending colors, and an orientation angle.

LinearGradientBrush

Graphics.DrawBezier Method

• How To draw this?

More Graphics -_-

AddMetafileComment Adds a comment to the current Metafile object.BeginContainer Overloaded. Saves a graphics container with the current state of this Graphics object

and opens and uses a new graphics container.ClearSupported by the.NET Compact Framework.

Clears the entire drawing surface and fills it with the specified background color.

CreateObjRef(inherited from MarshalByRefObject)

Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

DisposeSupported by the.NET Compact Framework.

Releases all resources used by this Graphics object.

DrawArc Overloaded. Draws an arc representing a portion of an ellipse specified by a pair of coordinates, a width, and a height.

DrawBezier 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 the.NET Compact Framework.

Overloaded. Draws an ellipse defined by a bounding rectangle specified by a pair of coordinates, a height, and a width.

DrawIconSupported by the.NET Compact Framework.

Overloaded. Draws the image represented by the specified Icon object at the specified 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 Draws a GraphicsPath object.DrawPie 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 Compact Framework.

Overloaded. Draws a polygon defined by an array ofPoint structures.

DrawRectangleSupported by the.NET Compact Framework.

Overloaded. Draws a rectangle specified by a coordinate pair, a width, and a 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 Closes the current graphics container and restores the state of this Graphics object to the state saved by a call to the BeginContainer method.

EnumerateMetafile Overloaded. Sends the records in the specified Metafileobject, one at a time, to a callback method for display at a specified point.

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

Overloaded. Determines whether two Object instances are equal.

ExcludeClip 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 Framework.

Overloaded. Fills the interior of an ellipse defined by a bounding rectangle 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 Compact Framework.

Overloaded. Fills the interior of a polygon defined by an array of points specified by Point structures.

FillRectangleSupported by the.NET Compact Framework.

Overloaded. Fills the interior of a rectangle specified by a pair of coordinates, a width, and a height.

FillRectangles Overloaded. Fills the interiors of a series of rectangles specified by Rectangle structures.

FillRegionSupported by the.NET Compact Framework.

Fills the interior of a Region object.

Adding Shadow!private:void AddShadow( PaintEventArgs^ e ){// Create two SizeF objects.SizeF shadowSize = listBox1->Size;SizeF addSize = SizeF(10.5F,20.8F);

// 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.

RegionDatapublic:

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 );

// 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!

top related