c++ windows forms l09 - gdi p2

134
Mohammad Shaker mohammadshakergtr.wordpress.com C++.NET Windows Forms Course @ZGTRShaker C++.NET Windows Forms Course L09- GDI Part 2

Upload: mohammad-shaker

Post on 25-Dec-2014

150 views

Category:

Technology


7 download

DESCRIPTION

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

TRANSCRIPT

Page 1: C++ Windows Forms L09 - GDI P2

Mohammad Shakermohammadshakergtr.wordpress.com

C++.NET Windows Forms Course@ZGTRShaker

C++.NET Windows Forms Course

L09- GDI Part 2

Page 2: C++ Windows Forms L09 - GDI P2
Page 3: C++ Windows Forms L09 - GDI P2

GDI- Part 1 Quick Revision -

Page 4: C++ Windows Forms L09 - GDI P2

What do u need to draw sth?

• Pen(stroke width, color, etc)• Paper• Brush to filling your drawing

Page 5: C++ Windows Forms L09 - GDI P2

Drawing::Graphic

• Encapsulates a GDI* + drawing surface.• This class cannot be inherited.

___________________• GDI* : Graphical Device Interface

Page 6: C++ Windows Forms L09 - GDI P2

Drawing::Graphics

private: System::Void button1_Click_5(System::Object^

sender, System::EventArgs^ e)

{

Drawing::Graphics ^MyGraphics;

MyGraphics = pictureBox1->CreateGraphics();

}

Page 7: C++ Windows Forms L09 - GDI P2

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

}

Page 8: C++ Windows Forms L09 - GDI P2

System::Drawing

Page 9: C++ Windows Forms L09 - GDI P2

Drawing::Point

• What does this even mean? -_-private: System::Void button1_Click(System::Object^

sender, System::EventArgs^ e)

{

button1->Location = 30,120;

}

Page 10: C++ Windows Forms L09 - GDI P2

Drawing::Point

Page 11: C++ Windows Forms L09 - GDI P2

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;

}

Page 12: C++ Windows Forms L09 - GDI P2

Drawing::Point

Page 13: C++ Windows Forms L09 - GDI P2

System::Drawing::Penprivate: System::Void button1_Click_5(System::Object^ sender,

System::EventArgs^ e)

{

System::Drawing::Pen ^ MyPen = gcnew Pen(Color::Red);

}

Page 14: C++ Windows Forms L09 - GDI P2
Page 15: C++ Windows Forms L09 - GDI P2
Page 16: C++ Windows Forms L09 - GDI P2
Page 17: C++ Windows Forms L09 - GDI P2

Graphics.DrawLine Method

• public: void DrawLine(Pen*, Point, Point);• public: void DrawLine(Pen*, PointF, PointF);• public: void DrawLine(Pen*, int, int, int, int);• public: void DrawLine(Pen*, float, float, float, float);

Page 18: C++ Windows Forms L09 - GDI P2

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

}

Page 19: C++ Windows Forms L09 - GDI P2
Page 20: C++ Windows Forms L09 - GDI P2

System::Drawing

Page 21: C++ Windows Forms L09 - GDI P2

System::Drawing

• What will happen now?

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

System::EventArgs^ e)

{

System::Drawing::Pen ^ MyPen = gcnew Pen(Color::Red);

Drawing::Graphics::DrawLine( MyPen, 2, 3, 4,5 );

}

Compiler error

Page 22: C++ Windows Forms L09 - GDI P2
Page 23: C++ Windows Forms L09 - GDI P2

System::Drawing

• What will happen now?

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

System::EventArgs^ e)

{

System::Drawing::Pen ^ MyPen = gcnew Pen(Color::Red);

Drawing::Graphics ^MyGraphics;

MyGraphics->DrawLine( MyPen, 2, 3, 4,5 );

}

No compiler error but Runtime error when clicking button1

Page 24: C++ Windows Forms L09 - GDI P2
Page 25: C++ Windows Forms L09 - GDI P2

System::Drawing

• What will happen now?

private: System::Void button1_Click_5(System::Object^ sender, System::EventArgs^

e)

{

System::Drawing::Pen ^ MyPen = gcnew Pen(Color::Red);

Drawing::Graphics ^MyGraphics;

MyGraphics = pictureBox1->CreateGraphics();

MyGraphics->DrawLine( MyPen, 2, 3, 50,105 );

}

A line will be drawn!

Page 26: C++ Windows Forms L09 - GDI P2

System::Drawing

• After clicking the button

Page 27: C++ Windows Forms L09 - GDI P2

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

Page 28: C++ Windows Forms L09 - GDI P2
Page 29: C++ Windows Forms L09 - GDI P2

System::Drawing

• But remember we can just do this private: System::Void button1_Click_5(System::Object^ sender, System::EventArgs^

e)

{

System::Drawing::Pen ^ MyPen = gcnew Pen(Color::Red);

Drawing::Graphics ^MyGraphics;

MyGraphics = pictureBox1->CreateGraphics();

MyGraphics->DrawLine( MyPen, 2, 3, 50,105 );

}

Page 30: C++ Windows Forms L09 - GDI P2

System::Drawing

• What will happen now? private: System::Void button1_Click_5(System::Object^ sender,

System::EventArgs^ e)

{

System::Drawing::Pen ^ MyPen = gcnew Pen(Color::Red);

Drawing::Graphics ^MyGraphics;

MyGraphics = pictureBox1->CreateGraphics();

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

}

Page 31: C++ Windows Forms L09 - GDI P2

System::Drawing

• What’s missing? Now we’ll find out!

Page 32: C++ Windows Forms L09 - GDI P2
Page 33: C++ Windows Forms L09 - GDI P2

System::Drawing

• Now, let’s have the following private: System::Void button1_Click_5(System::Object^ sender,

System::EventArgs^ e)

{

System::Drawing::Pen ^ MyPen = gcnew Pen(Color::Red);

Drawing::Graphics ^MyGraphics;

MyGraphics = pictureBox1->CreateGraphics();

Drawing::Point P1, P2;

P1.X = 50; P1.Y = 60;

P2.X = 150; P2.Y = 160;

MyGraphics->DrawLine( MyPen, P1, P2 );

P1.X = 50; P1.Y = 60;

P2.X = 510; P2.Y = 30;

MyGraphics->DrawLine( MyPen, P1, P2 );

}

Page 34: C++ Windows Forms L09 - GDI P2

System::Drawing

Page 35: C++ Windows Forms L09 - GDI P2

System::Drawing

• Now, when scrolling the line should be appeared, right?

Page 36: C++ Windows Forms L09 - GDI P2

System::Drawing

• Now moving the scroll forward

Nothing!

Page 37: C++ Windows Forms L09 - GDI P2

System::Drawing

• Now moving the scroll backward

Nothing!

Page 38: C++ Windows Forms L09 - GDI P2

Control.Paint() Event

• Control.Paint Event– Occurs when the control is redrawn

Page 39: C++ Windows Forms L09 - GDI P2

Control.Paint() Event

Page 40: C++ Windows Forms L09 - GDI P2

Control.Paint() Event

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

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

{

}

Page 41: C++ Windows Forms L09 - GDI P2

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

Page 42: C++ Windows Forms L09 - GDI P2

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

};

Page 43: C++ Windows Forms L09 - GDI 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;

}

Page 44: C++ Windows Forms L09 - GDI 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^>;

}

Page 45: C++ Windows Forms L09 - GDI P2

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

}

Page 46: C++ Windows Forms L09 - GDI P2

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

}

}

Page 47: C++ Windows Forms L09 - GDI P2

System::Drawing

• Before clicking anything

Page 48: C++ Windows Forms L09 - GDI P2

System::Drawing

• After clicking button6 “filling the linkedList with values”

Page 49: C++ Windows Forms L09 - GDI P2

System::Drawing

• After starting to scroll which means start painting “repainting” the pictureBox

Page 50: C++ Windows Forms L09 - GDI P2

System::Drawing

• What should happen now after continuing to scroll?

Page 51: C++ Windows Forms L09 - GDI P2

System::Drawing

• Scroll Forward

Page 52: C++ Windows Forms L09 - GDI P2

System::Drawing

• Now, when re-scrolling back it’s there!

Page 53: C++ Windows Forms L09 - GDI P2

Clear Method

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

Page 54: C++ Windows Forms L09 - GDI P2
Page 55: C++ Windows Forms L09 - GDI P2

Clear Method

• What happens now? private: System::Void button1_Click_5(System::Object^ sender,

System::EventArgs^ e)

{

MyGraphics->Clear(Color::SkyBlue);

}

Page 56: C++ Windows Forms L09 - GDI P2

Clear Method

Page 57: C++ Windows Forms L09 - GDI P2

Refresh Method

• What will happen now?

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

System::EventArgs^ e)

{

pictureBox1->Refresh();

}

Page 58: C++ Windows Forms L09 - GDI P2

Refresh Method

Page 59: C++ Windows Forms L09 - GDI P2

Refresh Method

• Now, when pressing “TRY” <<Refresh method caller>> button

Page 60: C++ Windows Forms L09 - GDI P2

Refresh Method

• When scrolling again!

Page 61: C++ Windows Forms L09 - GDI P2

Refresh Method

• Now, when pressing TRY again

Page 62: C++ Windows Forms L09 - GDI P2

Refresh Method

• When scrolling again!

Page 63: C++ Windows Forms L09 - GDI P2

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

Page 64: C++ Windows Forms L09 - GDI P2
Page 65: C++ Windows Forms L09 - GDI P2

private:Rectangle RcDraw;void Form1_MouseDown( Object^ /*sender*/, System::Windows::Forms::MouseEventArgs^ e ){ // Determine the initial rectangle coordinates.RcDraw.X = e->X; RcDraw.Y = e->Y; }

void Form1_MouseUp( Object^ /*sender*/, System::Windows::Forms::MouseEventArgs^ e ){// Determine the width and height of the rectangle.if( e->X < RcDraw.X ){ RcDraw.Width = RcDraw.X - e->X; RcDraw.X = e->X; }else{ RcDraw.Width = e->X - RcDraw.X; }

if( e->Y < RcDraw.Y ){ RcDraw.Height = RcDraw.Y - e->Y; RcDraw.Y = e->Y; }else{ RcDraw.Height = e->Y - RcDraw.Y; }

// Force a repaint of the region occupied by the rectangle.this->Invalidate( RcDraw );}

void Form1_Paint( Object^ /*sender*/, System::Windows::Forms::PaintEventArgs^ e ){ // Draw the rectangle.float PenWidth = 5;e->Graphics->DrawRectangle( gcnew Pen( Color::Blue,PenWidth ), RcDraw ); }

Page 66: C++ Windows Forms L09 - GDI P2
Page 67: C++ Windows Forms L09 - GDI P2

Graphics::Draw

• You can draw whatever you want!

Page 68: C++ Windows Forms L09 - GDI P2

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

Page 69: C++ Windows Forms L09 - GDI P2

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

Page 70: C++ Windows Forms L09 - GDI P2

Drawing with timer

Page 71: C++ Windows Forms L09 - GDI P2

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;

}

Page 72: C++ Windows Forms L09 - GDI P2

Drawing with timer

Page 73: C++ Windows Forms L09 - GDI P2

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;

}

Page 74: C++ Windows Forms L09 - GDI P2

Drawing with timer

Page 75: C++ Windows Forms L09 - GDI P2

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;

}

}

Page 76: C++ Windows Forms L09 - GDI P2

Drawing with sleep

Page 77: C++ Windows Forms L09 - GDI P2

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

}

}

Page 78: C++ Windows Forms L09 - GDI P2

Drawing with sleep –Refresh methods

Page 79: C++ Windows Forms L09 - GDI P2

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

Page 80: C++ Windows Forms L09 - GDI P2

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

}

Page 81: C++ Windows Forms L09 - GDI P2

Drawing with sleep and timer!

Page 82: C++ Windows Forms L09 - GDI P2

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

}

Page 83: C++ Windows Forms L09 - GDI P2

Drawing with sleep and timer!

Now, every two seconds a line will be drawn and cleared for another one to be shown

Page 84: C++ Windows Forms L09 - GDI P2

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;

}

Page 85: C++ Windows Forms L09 - GDI P2

Drawing with angles?

Page 86: C++ Windows Forms L09 - GDI P2

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;

}

};

Page 87: C++ Windows Forms L09 - GDI P2

Drawing with angles?

Page 88: C++ Windows Forms L09 - GDI P2

Drawing with angles?

Page 89: C++ Windows Forms L09 - GDI P2

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

Page 90: C++ Windows Forms L09 - GDI P2

Drawing And filling

• Brush :– Defines objects used to fill the interiors of graphical shapes such as

rectangles, ellipses, pies, polygons, and paths.

Page 91: C++ Windows Forms L09 - GDI P2
Page 92: C++ Windows Forms L09 - GDI P2

Drawing And filling

Page 93: C++ Windows Forms L09 - GDI P2

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

Page 94: C++ Windows Forms L09 - GDI P2

Drawing And filling

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

System::EventArgs^ e)

{

Drawing::Rectangle MyRect(50,30,60,100);

Drawing::Brush ^MyBrush;

MyBrush = gcnew Drawing::Brush;

MyGraphics->FillEllipse(MyBrush, MyRect );

}

Compiler error

Page 95: C++ Windows Forms L09 - GDI P2
Page 96: C++ Windows Forms L09 - GDI P2

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

}

Page 97: C++ Windows Forms L09 - GDI P2

Drawing And filling

Page 98: C++ Windows Forms L09 - GDI P2

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

}

Page 99: C++ Windows Forms L09 - GDI P2

Drawing And filling

Page 100: C++ Windows Forms L09 - GDI P2

Drawing And filling

Page 101: C++ Windows Forms L09 - GDI P2

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

}

Page 102: C++ Windows Forms L09 - GDI P2

Drawing And filling

Page 103: C++ Windows Forms L09 - GDI P2

Drawing And filling

Page 104: C++ Windows Forms L09 - GDI P2

Drawing And filling

Page 105: C++ Windows Forms L09 - GDI P2

Drawing And filling

• Drawing on panel!

Page 106: C++ Windows Forms L09 - GDI P2

Event Handling

Page 107: C++ Windows Forms L09 - GDI P2

Event Handling

• Let’s have the following form

Page 108: C++ Windows Forms L09 - GDI P2

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

System::EventArgs^ e)

{

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

}

Event Handling

Page 109: C++ Windows Forms L09 - GDI P2

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

System::EventArgs^ e)

{

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

}

Event Handling

Page 110: C++ Windows Forms L09 - GDI P2

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

Page 111: C++ Windows Forms L09 - GDI P2
Page 112: C++ Windows Forms L09 - GDI P2

Another window re-open!

After pressing ok for the first time

Page 113: C++ Windows Forms L09 - GDI P2

After pressing ok for the second time

Page 114: C++ Windows Forms L09 - GDI P2

Now, resizing!

Page 115: C++ Windows Forms L09 - GDI P2

Resize again, Nothing happens!, WHY?

Page 116: C++ Windows Forms L09 - GDI P2

Enlarge again

Page 117: C++ Windows Forms L09 - GDI P2

Enlarge again and again and this continues.. But till when?

Page 118: C++ Windows Forms L09 - GDI P2

Use the console to track\debug values!Do not use messagebox for tracking!

(see it in utility slide)

Page 119: C++ Windows Forms L09 - GDI P2

More on GDI

Page 120: C++ Windows Forms L09 - GDI P2

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.

Page 121: C++ Windows Forms L09 - GDI P2

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

Page 122: C++ Windows Forms L09 - GDI P2

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

Page 123: C++ Windows Forms L09 - GDI P2

• 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

Page 124: C++ Windows Forms L09 - GDI P2

Path

• Represents a series of connected lines and curves

Page 125: C++ Windows Forms L09 - GDI P2

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

Page 126: C++ Windows Forms L09 - GDI P2

Graphics.DrawBezier Method

• How To draw this?

Page 127: C++ Windows Forms L09 - GDI P2

More Graphics -_-

Page 128: C++ Windows Forms L09 - GDI P2

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.

Page 129: C++ Windows Forms L09 - GDI P2

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.

Page 130: C++ Windows Forms L09 - GDI P2

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.

Page 131: C++ Windows Forms L09 - GDI P2

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

Page 132: C++ Windows Forms L09 - GDI P2

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.

Page 133: C++ Windows Forms L09 - GDI P2

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;

}

}

Page 134: C++ Windows Forms L09 - GDI P2

That’s it for today!