C++ Windows Forms L09 - GDI P2
-
Upload
mohammad-shaker -
Category
Technology
-
view
151 -
download
7
description
Transcript of C++ Windows Forms L09 - GDI P2
![Page 1: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/1.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/2.jpg)
![Page 3: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/3.jpg)
GDI- Part 1 Quick Revision -
![Page 4: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/4.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/5.jpg)
Drawing::Graphic
• Encapsulates a GDI* + drawing surface.• This class cannot be inherited.
___________________• GDI* : Graphical Device Interface
![Page 6: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/6.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/7.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/8.jpg)
System::Drawing
![Page 9: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/9.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/10.jpg)
Drawing::Point
![Page 11: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/11.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/12.jpg)
Drawing::Point
![Page 13: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/13.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/14.jpg)
![Page 15: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/15.jpg)
![Page 16: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/16.jpg)
![Page 17: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/17.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/18.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/19.jpg)
![Page 20: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/20.jpg)
System::Drawing
![Page 21: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/21.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/22.jpg)
![Page 23: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/23.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/24.jpg)
![Page 25: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/25.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/26.jpg)
System::Drawing
• After clicking the button
![Page 27: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/27.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/28.jpg)
![Page 29: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/29.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/30.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/31.jpg)
System::Drawing
• What’s missing? Now we’ll find out!
![Page 32: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/32.jpg)
![Page 33: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/33.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/34.jpg)
System::Drawing
![Page 35: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/35.jpg)
System::Drawing
• Now, when scrolling the line should be appeared, right?
![Page 36: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/36.jpg)
System::Drawing
• Now moving the scroll forward
Nothing!
![Page 37: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/37.jpg)
System::Drawing
• Now moving the scroll backward
Nothing!
![Page 38: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/38.jpg)
Control.Paint() Event
• Control.Paint Event– Occurs when the control is redrawn
![Page 39: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/39.jpg)
Control.Paint() Event
![Page 40: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/40.jpg)
Control.Paint() Event
private: System::Void pictureBox1_Paint(System::Object^
sender, System::Windows::Forms::PaintEventArgs^ e)
{
}
![Page 41: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/41.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/42.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/43.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/44.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/45.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/46.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/47.jpg)
System::Drawing
• Before clicking anything
![Page 48: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/48.jpg)
System::Drawing
• After clicking button6 “filling the linkedList with values”
![Page 49: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/49.jpg)
System::Drawing
• After starting to scroll which means start painting “repainting” the pictureBox
![Page 50: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/50.jpg)
System::Drawing
• What should happen now after continuing to scroll?
![Page 51: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/51.jpg)
System::Drawing
• Scroll Forward
![Page 52: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/52.jpg)
System::Drawing
• Now, when re-scrolling back it’s there!
![Page 53: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/53.jpg)
Clear Method
• Clears the entire drawing surface and fills it with the specified background color.
![Page 54: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/54.jpg)
![Page 55: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/55.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/56.jpg)
Clear Method
![Page 57: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/57.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/58.jpg)
Refresh Method
![Page 59: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/59.jpg)
Refresh Method
• Now, when pressing “TRY” <<Refresh method caller>> button
![Page 60: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/60.jpg)
Refresh Method
• When scrolling again!
![Page 61: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/61.jpg)
Refresh Method
• Now, when pressing TRY again
![Page 62: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/62.jpg)
Refresh Method
• When scrolling again!
![Page 63: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/63.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/64.jpg)
![Page 65: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/65.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/66.jpg)
![Page 67: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/67.jpg)
Graphics::Draw
• You can draw whatever you want!
![Page 68: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/68.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/69.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/70.jpg)
Drawing with timer
![Page 71: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/71.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/72.jpg)
Drawing with timer
![Page 73: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/73.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/74.jpg)
Drawing with timer
![Page 75: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/75.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/76.jpg)
Drawing with sleep
![Page 77: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/77.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/78.jpg)
Drawing with sleep –Refresh methods
![Page 79: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/79.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/80.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/81.jpg)
Drawing with sleep and timer!
![Page 82: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/82.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/83.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/84.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/85.jpg)
Drawing with angles?
![Page 86: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/86.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/87.jpg)
Drawing with angles?
![Page 88: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/88.jpg)
Drawing with angles?
![Page 89: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/89.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/90.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/91.jpg)
![Page 92: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/92.jpg)
Drawing And filling
![Page 93: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/93.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/94.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/95.jpg)
![Page 96: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/96.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/97.jpg)
Drawing And filling
![Page 98: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/98.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/99.jpg)
Drawing And filling
![Page 100: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/100.jpg)
Drawing And filling
![Page 101: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/101.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/102.jpg)
Drawing And filling
![Page 103: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/103.jpg)
Drawing And filling
![Page 104: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/104.jpg)
Drawing And filling
![Page 105: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/105.jpg)
Drawing And filling
• Drawing on panel!
![Page 106: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/106.jpg)
Event Handling
![Page 107: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/107.jpg)
Event Handling
• Let’s have the following form
![Page 108: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/108.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/109.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/110.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/111.jpg)
![Page 112: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/112.jpg)
Another window re-open!
After pressing ok for the first time
![Page 113: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/113.jpg)
After pressing ok for the second time
![Page 114: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/114.jpg)
Now, resizing!
![Page 115: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/115.jpg)
Resize again, Nothing happens!, WHY?
![Page 116: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/116.jpg)
Enlarge again
![Page 117: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/117.jpg)
Enlarge again and again and this continues.. But till when?
![Page 118: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/118.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/119.jpg)
More on GDI
![Page 120: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/120.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/121.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/122.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/123.jpg)
• 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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/124.jpg)
Path
• Represents a series of connected lines and curves
![Page 125: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/125.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/126.jpg)
Graphics.DrawBezier Method
• How To draw this?
![Page 127: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/127.jpg)
More Graphics -_-
![Page 128: C++ Windows Forms L09 - GDI P2](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/128.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/129.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/130.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/131.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/132.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/133.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081801/549cb1f3b47959c4318b47ae/html5/thumbnails/134.jpg)
That’s it for today!