SlideShare una empresa de Scribd logo
1 de 25
Descargar para leer sin conexión
Visual C# .Net using
framework 4.5
Eng. Mahmoud Ouf
Lecture 06
mmouf@2018
Generics
With the release of .NET 2.0, the C# programming language has been
enhanced to support a new feature of the CTS termed generics. Simply,
generics provide a way for programmers to define “placeholders” (formally
termed type parameters) for method arguments and type definitions, which
are specified at the time of invoking the generic method or creating the
generic type
Generic methods: enable you to specify, with a single method declaration, a
set of related methods.
Generic classes: enable you to specify, with a single class declaration, a set
of related classes.
mmouf@2018
Generics
class Point<M>
{
M x;
M y;
public Point(M a, M b)
{
x = a;
y = b;
}
public override string ToString()
{
return ("X = " + x.ToString() + " , Y = " + y.ToString());
}
}
mmouf@2018
Generics
Here, we have made a class Calculate contains a static method used to swap
any 2 data from the same data type, it is also Generics
class Calculate<T>
{
public static void Swap(ref T a, ref T b)
{
T temp;
temp = a;
a = b;
b = temp;
}
}
mmouf@2018
Generics
class Program
{
static void Main(string[] args)
{
int x = 3;
int y = 4;
Calculate<int>.Swap(ref x, ref y);
Console.WriteLine("X = {0}", x);
Console.WriteLine("Y = {0}", y);
Point<float> pt1 = new Point<float>(5.6f, 7.9f);
Point<float> pt2 = new Point<float>(20.5f, 50.7f);
Calculate<Point<float>>.Swap(ref pt1, ref pt2);
Console.WriteLine("PT1 = {0}", pt1.ToString());
Console.WriteLine("PT2 = {0}", pt2.ToString());
}
} mmouf@2018
Partial class
It is possible to split the definition of a class, struct or interface over two or
more source files.
Each source file contains a section of the class definition, and all parts are
combined when the application is compiled.
We use partial class for:
When working on large projects, spreading a class over separate files
allows multiple programmers to work on it simultaneously.
When working automatic generated source, code can be added to the class
without having to recreate the source file. Visual Studio uses this approach
when creating Windows Forms
Use the partial keyword modifier to split class definition.
mmouf@2018
Partial class
public partial class Employee
{
public void Test1()
{
}
}
public partial class Employee
{
public void Test2()
{
}
}
mmouf@2018
Partial class
Notes:
All class part must be defined within the same namesapace.
All parts must use partial keyword.
All parts must be available at compile time to form the final type.
All parts must have the same accessibility (public, private, …).
If any part is declared abstract, then the entire class is abstract.
If any part is declared sealed, then the entire class is sealed.
If any part declare a base type, then the entire type inherits that class.
The partial keyword appears immediately before the keyword class, struct
or interface.
mmouf@2018
Windows Programming
GUI (Graphical User Interface)
mmouf@2018
Windows Forms
Microsoft Windows is a graphical interface.
Users interact with applications through windows, which are graphical
representation of application data and options.
.NET framework provides a wide array of graphical controls and methods
that allow you to create fully visual applications
You might want to render your own graphic content in your application or
create a custom appearance for a control, you must learn to use the Graphic
Device Interface (GDI).
.Net provides a lot of useful classes grouped in 2 major namespaces :
1. System.Windows.Forms
2. System.Drawing
mmouf@2018
Core GDI+
All windows forms applications inherit from System.Windows.Forms.Form
All graphical function provided by Microsoft.Net framework are available in
the System.Drawing namespace (which can be found in the
System.Drawing.dll)
System.Drawing
System.Drawing.Drawing2D
System.Drawing.Imaging
System.Drawing.Printing
System.Drawing.Text
mmouf@2018
Core GDI+
System.Drawing
It is the core GDI+ namespace, which defines numerous types for basic
rendering as well as the almighty Graphics type.
System.Drawing.Drawing2D
It contains classes and enumerations that provide advanced 2-D and vector
graphics functionality.
System.Drawing.Imaging
Define types that allow you to directly manipulate graphical images
System.Drawing.Printing
It define types that allow you to render images to the printed page, interact
with the printer itself and format the appearance of a given print job.
System.Drawing.Text
This namespace allows you to manipulate collections of fonts
mmouf@2018
The Graphics object
The Graphics object is the principal object used in rendering graphics
It represents the drawing surface of a visual element, such as form, a control
or an image object.
A form has an associated Graphics object that can be used to draw inside the
form, and the same for the control and the image.
You can’t directly instantiate one with the call of constructor.
Instead, you must create a Graphics object directly from the visual element.
You can use CreateGraphics method that allow you to get a reference to the
Graphics object associated with that control
Ex: System.Drawing.Graphics myGraphics;
myGraphics = myForm.CreateGraphics();
mmouf@2018
Coordinates and Shapes
Many of drawing methods defined by the Graphics object require you to
specify the coordinate in which you wish to render a given item.
System.Drawing namespace contains a variety of structure that can be used
to describe the location or the region within this coordinate system.
By default, the origin of the coordinate system for each control is the Upper
Left corner, which has coordinates of (0,0)
Point :
Represents an ordered pair of integer x- and y-coordinates that defines a
point in a two-dimensional plane
public Point(int dw);
public Point(Size sz);
public Point(int x, int y);
mmouf@2018
Coordinates and Shapes
PointF :
Represents an ordered pair of floating point x- and y-coordinates that defines
a point in a two-dimensional plane.
public PointF(float dw);
public PointF(SizeF sz);
public PointF(float x, float y);
Size :
Stores an ordered pair of integers, typically the width and height of a
rectangle.
public Size(Point pt);
public Size(int width, int height);
mmouf@2018
Coordinates and Shapes
SizeF :
Stores an ordered pair of float, typically the width and height of a rectangle.
public SizeF(PointF pt);
public SizeF(float width, float height);
Rectangle :
Stores a set of four integers that represent the location and size of a rectangle.
public Rectangle(Point location, Size size);
public Rectangle(int x, int y, int width, int height);
RectangleF :
Stores a set of four floating-point numbers that represent the location and
size of a rectangle.
public RectangleF(PointF location, SizeF size);
public RectangleF(float x, float y, float width, float height);
mmouf@2018
Drawing a string
To display a string, use the method DrawString() which is a member of
Graphics class. There are 6 overload for DrawString() method
Overload #1
public void DrawString(string s, Font font, Brush brush, PointF point);
s : the string to be displayed
s = “Welcome to C#
font : the font to be used in writing the string
font = new Font(“Times New Roman”, 20);
brush : represent the color used to write
brush = new SolidBrush(Color.Black);
point : the upper left corner of where to start writing
point = new PointF(15, 15);
mmouf@2018
Drawing a string
Overload #2
public void DrawString(string s, Font font, Brush brush, float x, float y);
s : the string to be displayed
s = “Welcome to C#
font : the font to be used in writing the string
font = new Font(“Times New Roman”, 20);
brush : represent the color used to write
brush = new SolidBrush(Color.Black);
(x, y) : the upper left corner of where to start writing
x = 15;
y = 15;
mmouf@2018
Drawing a string
Overload #3
public void DrawString(string s, Font font, Brush brush, RectangleF
layoutRectangle);
s : the string to be displayed
s = “Welcome to C#
font : the font to be used in writing the string
font = new Font(“Times New Roman”, 20);
brush : represent the color used to write
brush = new SolidBrush(Color.Black);
layoutRectangle : a rectangle where to write the string. If the string is
longer than the rectangle width, a part will not appear
layoutRectangle = new RectangleF(10, 10, 60, 30);
mmouf@2018
Drawing a string
Where to call the DrawString() method?
We need the DrawString() to be called automatically when the form has to be
redrawn.
So, we need to override the OnPaint Event handler method, as follow:
Protected override void OnPaint(PaintEventArgs e)
{
Graphics MyGraphics = this.CreateGraphics();
MyGraphics.DrawString(…);
}
mmouf@2018
Drawing a line
To draw a line, use the method DrawLine() which is a member of Graphics
class.
The DrawLine method have 4 overloading:
public void DrawLine(Pen pen, Point pt1, Point pt2);
public void DrawLine(Pen pen, int x1, int y1, int x2, int y2);
pen: is used to determine the line color and style
Pen pen = new Pen(Color.Red)
OR Pen pen = new Pen(Color.Red, 5);
The Pen also has some properties as “DashStyle” which is an
enumeration contains the line style
Solid = 0, Dash = 1, Dot = 2, DashDot = 3, DashDotDot = 4
p1, (x1, y1): specify the starting point
p2, (x2, y2): specify the ending point
mmouf@2018
Drawing a rectangle
There are 2 types of rectangle:
1) Just Frame:
To draw a rectangle use the method DrawRectangle() which is a member of
Graphics class.
To draw a rectangle we must know the Pen and the UL corner, the width, and
the height
The DrawRectangle method have 3 overloading:
public void DrawRectangle(Pen pen, Rectangle rect);
public void DrawRectangle(Pen pen, int x, int y, int width, int height);
public void DrawRectangle(Pen pen, float x, float y, float width,
float height);
mmouf@2018
Drawing a rectangle
2) Filled Rectangle:
To draw a filled rectangle, use the method FillRectangle() which is a member
of Graphics class.
To draw a filled rectangle, we must know the brush (color, style) and the
rectangle to be drawn
public void FillRectangle(Brush brush, Rectangle rect);
public void FillRectangle(Brush brush, RectangleF rect);
public void FillRectangle(Brush brush, int x, int y, int width,
int height);
public void FillRectangle(Brush brush, float x, float y, float width,
float height);
mmouf@2018
Drawing a rectangle
2) Filled Rectangle:
The Brush is used to determine the color and the filled style. This is an
abstract base class and cannot be instantiated. To create a brush object, use
classes derived from Brush such as SolidBrush, HatchBrush
Working with SolidBrush
public SolidBrush(Color color);
It has only one property which is the color of the rectangle and the filled
area.
Ex : Brush mBrush = new SolidBrush(Color.Yellow);
mmouf@2018
Drawing a rectangle
2) Filled Rectangle:
Working with HatchBrush
public HatchBrush(HatchStyle hatchstyle, Color foreColor);
//default background BLACK
public HatchBrush(HatchStyle hatchstyle, Color foreColor,
Color backColor);
The HachStyle is an enumeration that define the pattern used to fill the
Shape”Rectangle”
Ex of style : BackwardDiagonal //
ForwardDiagonal 
Vertical ||
mmouf@2018

Más contenido relacionado

La actualidad más candente (20)

Intake 38 7
Intake 38 7Intake 38 7
Intake 38 7
 
Intake 37 4
Intake 37 4Intake 37 4
Intake 37 4
 
Intake 38 5
Intake 38 5Intake 38 5
Intake 38 5
 
Intake 38_1
Intake 38_1Intake 38_1
Intake 38_1
 
Intake 37 5
Intake 37 5Intake 37 5
Intake 37 5
 
Intake 37 2
Intake 37 2Intake 37 2
Intake 37 2
 
Intake 37 1
Intake 37 1Intake 37 1
Intake 37 1
 
Templates
TemplatesTemplates
Templates
 
C sharp chap5
C sharp chap5C sharp chap5
C sharp chap5
 
C sharp chap6
C sharp chap6C sharp chap6
C sharp chap6
 
Java execise
Java execiseJava execise
Java execise
 
C sharp chap4
C sharp chap4C sharp chap4
C sharp chap4
 
Generic Programming
Generic ProgrammingGeneric Programming
Generic Programming
 
Templates presentation
Templates presentationTemplates presentation
Templates presentation
 
C programming & data structure [arrays & pointers]
C programming & data structure   [arrays & pointers]C programming & data structure   [arrays & pointers]
C programming & data structure [arrays & pointers]
 
Csphtp1 06
Csphtp1 06Csphtp1 06
Csphtp1 06
 
Generic Programming seminar
Generic Programming seminarGeneric Programming seminar
Generic Programming seminar
 
C# Generics
C# GenericsC# Generics
C# Generics
 
C++ Templates 2
C++ Templates 2C++ Templates 2
C++ Templates 2
 
Intake 37 linq1
Intake 37 linq1Intake 37 linq1
Intake 37 linq1
 

Similar a Intake 38 6

13 Graph Classes
13 Graph Classes13 Graph Classes
13 Graph Classes
poffdeluxe
 
Csphtp1 16
Csphtp1 16Csphtp1 16
Csphtp1 16
HUST
 
We are restricted from importing cv2 numpy stats and other.pdf
We are restricted from importing cv2 numpy stats and other.pdfWe are restricted from importing cv2 numpy stats and other.pdf
We are restricted from importing cv2 numpy stats and other.pdf
DARSHANACHARYA13
 

Similar a Intake 38 6 (20)

Write a new version of the area calculation program that makes use of.docx
 Write a new version of the area calculation program that makes use of.docx Write a new version of the area calculation program that makes use of.docx
Write a new version of the area calculation program that makes use of.docx
 
Java Graphics
Java GraphicsJava Graphics
Java Graphics
 
C# Summer course - Lecture 2
C# Summer course - Lecture 2C# Summer course - Lecture 2
C# Summer course - Lecture 2
 
C++ Windows Forms L09 - GDI P2
C++ Windows Forms L09 - GDI P2C++ Windows Forms L09 - GDI P2
C++ Windows Forms L09 - GDI P2
 
13 Graph Classes
13 Graph Classes13 Graph Classes
13 Graph Classes
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 
U5 JAVA.pptx
U5 JAVA.pptxU5 JAVA.pptx
U5 JAVA.pptx
 
Csphtp1 16
Csphtp1 16Csphtp1 16
Csphtp1 16
 
CSharpCheatSheetV1.pdf
CSharpCheatSheetV1.pdfCSharpCheatSheetV1.pdf
CSharpCheatSheetV1.pdf
 
Class program and uml in c++
Class program and uml in c++Class program and uml in c++
Class program and uml in c++
 
JFree chart
JFree chartJFree chart
JFree chart
 
Unit1 C
Unit1 CUnit1 C
Unit1 C
 
Unit1 C
Unit1 CUnit1 C
Unit1 C
 
C++
C++C++
C++
 
X++ 1.pptx
X++ 1.pptxX++ 1.pptx
X++ 1.pptx
 
C++ Windows Forms L08 - GDI P1
C++ Windows Forms L08 - GDI P1 C++ Windows Forms L08 - GDI P1
C++ Windows Forms L08 - GDI P1
 
AS TASKS #8
AS TASKS #8AS TASKS #8
AS TASKS #8
 
C Programming Unit-1
C Programming Unit-1C Programming Unit-1
C Programming Unit-1
 
We are restricted from importing cv2 numpy stats and other.pdf
We are restricted from importing cv2 numpy stats and other.pdfWe are restricted from importing cv2 numpy stats and other.pdf
We are restricted from importing cv2 numpy stats and other.pdf
 
Objects and Graphics
Objects and GraphicsObjects and Graphics
Objects and Graphics
 

Más de Mahmoud Ouf (17)

Relation between classes in arabic
Relation between classes in arabicRelation between classes in arabic
Relation between classes in arabic
 
Intake 38 data access 4
Intake 38 data access 4Intake 38 data access 4
Intake 38 data access 4
 
Intake 38 data access 1
Intake 38 data access 1Intake 38 data access 1
Intake 38 data access 1
 
Intake 38 11
Intake 38 11Intake 38 11
Intake 38 11
 
Intake 38 10
Intake 38 10Intake 38 10
Intake 38 10
 
Intake 38 9
Intake 38 9Intake 38 9
Intake 38 9
 
Intake 38 8
Intake 38 8Intake 38 8
Intake 38 8
 
Intake 37 DM
Intake 37 DMIntake 37 DM
Intake 37 DM
 
Intake 37 ef2
Intake 37 ef2Intake 37 ef2
Intake 37 ef2
 
Intake 37 ef1
Intake 37 ef1Intake 37 ef1
Intake 37 ef1
 
Intake 37 linq3
Intake 37 linq3Intake 37 linq3
Intake 37 linq3
 
Intake 37 linq2
Intake 37 linq2Intake 37 linq2
Intake 37 linq2
 
Intake 37 12
Intake 37 12Intake 37 12
Intake 37 12
 
Intake 37 11
Intake 37 11Intake 37 11
Intake 37 11
 
Intake 37 10
Intake 37 10Intake 37 10
Intake 37 10
 
Intake 37 9
Intake 37 9Intake 37 9
Intake 37 9
 
Intake 37 8
Intake 37 8Intake 37 8
Intake 37 8
 

Último

Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 

Último (20)

Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 

Intake 38 6

  • 1. Visual C# .Net using framework 4.5 Eng. Mahmoud Ouf Lecture 06 mmouf@2018
  • 2. Generics With the release of .NET 2.0, the C# programming language has been enhanced to support a new feature of the CTS termed generics. Simply, generics provide a way for programmers to define “placeholders” (formally termed type parameters) for method arguments and type definitions, which are specified at the time of invoking the generic method or creating the generic type Generic methods: enable you to specify, with a single method declaration, a set of related methods. Generic classes: enable you to specify, with a single class declaration, a set of related classes. mmouf@2018
  • 3. Generics class Point<M> { M x; M y; public Point(M a, M b) { x = a; y = b; } public override string ToString() { return ("X = " + x.ToString() + " , Y = " + y.ToString()); } } mmouf@2018
  • 4. Generics Here, we have made a class Calculate contains a static method used to swap any 2 data from the same data type, it is also Generics class Calculate<T> { public static void Swap(ref T a, ref T b) { T temp; temp = a; a = b; b = temp; } } mmouf@2018
  • 5. Generics class Program { static void Main(string[] args) { int x = 3; int y = 4; Calculate<int>.Swap(ref x, ref y); Console.WriteLine("X = {0}", x); Console.WriteLine("Y = {0}", y); Point<float> pt1 = new Point<float>(5.6f, 7.9f); Point<float> pt2 = new Point<float>(20.5f, 50.7f); Calculate<Point<float>>.Swap(ref pt1, ref pt2); Console.WriteLine("PT1 = {0}", pt1.ToString()); Console.WriteLine("PT2 = {0}", pt2.ToString()); } } mmouf@2018
  • 6. Partial class It is possible to split the definition of a class, struct or interface over two or more source files. Each source file contains a section of the class definition, and all parts are combined when the application is compiled. We use partial class for: When working on large projects, spreading a class over separate files allows multiple programmers to work on it simultaneously. When working automatic generated source, code can be added to the class without having to recreate the source file. Visual Studio uses this approach when creating Windows Forms Use the partial keyword modifier to split class definition. mmouf@2018
  • 7. Partial class public partial class Employee { public void Test1() { } } public partial class Employee { public void Test2() { } } mmouf@2018
  • 8. Partial class Notes: All class part must be defined within the same namesapace. All parts must use partial keyword. All parts must be available at compile time to form the final type. All parts must have the same accessibility (public, private, …). If any part is declared abstract, then the entire class is abstract. If any part is declared sealed, then the entire class is sealed. If any part declare a base type, then the entire type inherits that class. The partial keyword appears immediately before the keyword class, struct or interface. mmouf@2018
  • 9. Windows Programming GUI (Graphical User Interface) mmouf@2018
  • 10. Windows Forms Microsoft Windows is a graphical interface. Users interact with applications through windows, which are graphical representation of application data and options. .NET framework provides a wide array of graphical controls and methods that allow you to create fully visual applications You might want to render your own graphic content in your application or create a custom appearance for a control, you must learn to use the Graphic Device Interface (GDI). .Net provides a lot of useful classes grouped in 2 major namespaces : 1. System.Windows.Forms 2. System.Drawing mmouf@2018
  • 11. Core GDI+ All windows forms applications inherit from System.Windows.Forms.Form All graphical function provided by Microsoft.Net framework are available in the System.Drawing namespace (which can be found in the System.Drawing.dll) System.Drawing System.Drawing.Drawing2D System.Drawing.Imaging System.Drawing.Printing System.Drawing.Text mmouf@2018
  • 12. Core GDI+ System.Drawing It is the core GDI+ namespace, which defines numerous types for basic rendering as well as the almighty Graphics type. System.Drawing.Drawing2D It contains classes and enumerations that provide advanced 2-D and vector graphics functionality. System.Drawing.Imaging Define types that allow you to directly manipulate graphical images System.Drawing.Printing It define types that allow you to render images to the printed page, interact with the printer itself and format the appearance of a given print job. System.Drawing.Text This namespace allows you to manipulate collections of fonts mmouf@2018
  • 13. The Graphics object The Graphics object is the principal object used in rendering graphics It represents the drawing surface of a visual element, such as form, a control or an image object. A form has an associated Graphics object that can be used to draw inside the form, and the same for the control and the image. You can’t directly instantiate one with the call of constructor. Instead, you must create a Graphics object directly from the visual element. You can use CreateGraphics method that allow you to get a reference to the Graphics object associated with that control Ex: System.Drawing.Graphics myGraphics; myGraphics = myForm.CreateGraphics(); mmouf@2018
  • 14. Coordinates and Shapes Many of drawing methods defined by the Graphics object require you to specify the coordinate in which you wish to render a given item. System.Drawing namespace contains a variety of structure that can be used to describe the location or the region within this coordinate system. By default, the origin of the coordinate system for each control is the Upper Left corner, which has coordinates of (0,0) Point : Represents an ordered pair of integer x- and y-coordinates that defines a point in a two-dimensional plane public Point(int dw); public Point(Size sz); public Point(int x, int y); mmouf@2018
  • 15. Coordinates and Shapes PointF : Represents an ordered pair of floating point x- and y-coordinates that defines a point in a two-dimensional plane. public PointF(float dw); public PointF(SizeF sz); public PointF(float x, float y); Size : Stores an ordered pair of integers, typically the width and height of a rectangle. public Size(Point pt); public Size(int width, int height); mmouf@2018
  • 16. Coordinates and Shapes SizeF : Stores an ordered pair of float, typically the width and height of a rectangle. public SizeF(PointF pt); public SizeF(float width, float height); Rectangle : Stores a set of four integers that represent the location and size of a rectangle. public Rectangle(Point location, Size size); public Rectangle(int x, int y, int width, int height); RectangleF : Stores a set of four floating-point numbers that represent the location and size of a rectangle. public RectangleF(PointF location, SizeF size); public RectangleF(float x, float y, float width, float height); mmouf@2018
  • 17. Drawing a string To display a string, use the method DrawString() which is a member of Graphics class. There are 6 overload for DrawString() method Overload #1 public void DrawString(string s, Font font, Brush brush, PointF point); s : the string to be displayed s = “Welcome to C# font : the font to be used in writing the string font = new Font(“Times New Roman”, 20); brush : represent the color used to write brush = new SolidBrush(Color.Black); point : the upper left corner of where to start writing point = new PointF(15, 15); mmouf@2018
  • 18. Drawing a string Overload #2 public void DrawString(string s, Font font, Brush brush, float x, float y); s : the string to be displayed s = “Welcome to C# font : the font to be used in writing the string font = new Font(“Times New Roman”, 20); brush : represent the color used to write brush = new SolidBrush(Color.Black); (x, y) : the upper left corner of where to start writing x = 15; y = 15; mmouf@2018
  • 19. Drawing a string Overload #3 public void DrawString(string s, Font font, Brush brush, RectangleF layoutRectangle); s : the string to be displayed s = “Welcome to C# font : the font to be used in writing the string font = new Font(“Times New Roman”, 20); brush : represent the color used to write brush = new SolidBrush(Color.Black); layoutRectangle : a rectangle where to write the string. If the string is longer than the rectangle width, a part will not appear layoutRectangle = new RectangleF(10, 10, 60, 30); mmouf@2018
  • 20. Drawing a string Where to call the DrawString() method? We need the DrawString() to be called automatically when the form has to be redrawn. So, we need to override the OnPaint Event handler method, as follow: Protected override void OnPaint(PaintEventArgs e) { Graphics MyGraphics = this.CreateGraphics(); MyGraphics.DrawString(…); } mmouf@2018
  • 21. Drawing a line To draw a line, use the method DrawLine() which is a member of Graphics class. The DrawLine method have 4 overloading: public void DrawLine(Pen pen, Point pt1, Point pt2); public void DrawLine(Pen pen, int x1, int y1, int x2, int y2); pen: is used to determine the line color and style Pen pen = new Pen(Color.Red) OR Pen pen = new Pen(Color.Red, 5); The Pen also has some properties as “DashStyle” which is an enumeration contains the line style Solid = 0, Dash = 1, Dot = 2, DashDot = 3, DashDotDot = 4 p1, (x1, y1): specify the starting point p2, (x2, y2): specify the ending point mmouf@2018
  • 22. Drawing a rectangle There are 2 types of rectangle: 1) Just Frame: To draw a rectangle use the method DrawRectangle() which is a member of Graphics class. To draw a rectangle we must know the Pen and the UL corner, the width, and the height The DrawRectangle method have 3 overloading: public void DrawRectangle(Pen pen, Rectangle rect); public void DrawRectangle(Pen pen, int x, int y, int width, int height); public void DrawRectangle(Pen pen, float x, float y, float width, float height); mmouf@2018
  • 23. Drawing a rectangle 2) Filled Rectangle: To draw a filled rectangle, use the method FillRectangle() which is a member of Graphics class. To draw a filled rectangle, we must know the brush (color, style) and the rectangle to be drawn public void FillRectangle(Brush brush, Rectangle rect); public void FillRectangle(Brush brush, RectangleF rect); public void FillRectangle(Brush brush, int x, int y, int width, int height); public void FillRectangle(Brush brush, float x, float y, float width, float height); mmouf@2018
  • 24. Drawing a rectangle 2) Filled Rectangle: The Brush is used to determine the color and the filled style. This is an abstract base class and cannot be instantiated. To create a brush object, use classes derived from Brush such as SolidBrush, HatchBrush Working with SolidBrush public SolidBrush(Color color); It has only one property which is the color of the rectangle and the filled area. Ex : Brush mBrush = new SolidBrush(Color.Yellow); mmouf@2018
  • 25. Drawing a rectangle 2) Filled Rectangle: Working with HatchBrush public HatchBrush(HatchStyle hatchstyle, Color foreColor); //default background BLACK public HatchBrush(HatchStyle hatchstyle, Color foreColor, Color backColor); The HachStyle is an enumeration that define the pattern used to fill the Shape”Rectangle” Ex of style : BackwardDiagonal // ForwardDiagonal Vertical || mmouf@2018