Title: Graphic Device Interface (GDI)
1Graphic Device Interface(GDI)
2Class Form
- A Form is a representation of any window
displayed in your application. - The Form class can be used to create standard,
tool, borderless, and floating windows. - The events of the Form class allow you to respond
to actions performed on the form. You can use the
Activated event to perform operations such as
updating the data displayed in the controls of
the form when the form is activated. - You can use a form as the starting class in your
application by placing a method called Main() in
the class. In the Main() method add code to
create and show the form.
3Some Event functions of Form
protected override void OnClick( EventArgs e )
protected override void OnKeyDown( KeyEventArgs e)
protected override void OnMouseDown( MouseEventArgs e)
protected override void OnMouseEnter( MouseEventArgs e)
protected override void OnMouseHove( MouseEventArgs e)
protected override void OnMouseMove( MouseEventArgs e)
protected override void OnMouseUp( MouseEventArgs e)
protected override void OnMouseLeave( MouseEventArgs e)
protected override void OnPaint( PaintEventArgs e )
protected override void OnResize(EventArgs e)
Note EventArgs is parent class of all other
EventArgs
4Class Point
- Namespace System.Drawing
- Constructor
- Point p new Point(int x, int y)
-
y
x
5Class Rectangle
- Constructor
- Rectangle r new Rectangle( Point(x, y),
Size(w, h)) - Rectangle r new Rectangle( int x, int y, int
w, int h) -
y
x
w
h
6class Color
- Some static Color objects
- Color.Red Color.Yellow Color.Blue
- Color object can be obtained from RGB formation
- Color c1 Color.FromArgb(200, 128, 48)
- We also can use 0? a ? 255 to control color
- a 255 means opaque, a 0 means
transparent - a 180 means half transparent
- For example
- Color c2 Color.FromArgb(a, c1)
- Color c2 Color.FromArgb(a, 200, 128, 48)
7Class Pen
- Namespace System.Drawing
- Pen object draws a line of specified width and
style. - Constructor
- Pen(Color object, int width)
- Pen p new (Color.Red, 3)
- Some Pen objects can be got from class Pens
- Pens.Red Pens.Yellow Pens.Blue
- They all have width 1
-
8Class Font
- Constructor
- Font( FontFamily family,
- float Size,
- FontStyle style )
- Example
- Font f new Font("Arial",
- 36,
- FontStyle.BoldFontStyle.Italic )
9Class Brush
- Namespace System.Drawing
- Brush object used to fill the interiors of
graphical shapes such as rectangles, ellipses,
pies, polygons, and paths. - This is an abstract class. Use derived classes
such as - SolidBrush(Color object)
- TextureBrush(Image object)
- (In Namespace System.Drawing.Drawing2D)
- LinearGradientBrush(Point1, Point1, Color1,
Color2) - HatchBrush(HatchStyle, Color1, Color2)
10Class Graphics
- Namespace System.Drawing
- The Graphics class provides methods for drawing
objects to the display device. - A Graphics object is associated with a specific
device context, which means every Form has a
built-in Graphics object. - The following method is wrong
- Graphics g new Graphics()
-
11Some Methods of Graphics
public void DrawArc(Pen, Rectangle, float angle1, float angle2)
public void DrawLine(Pen, Point p1, Point p2)
public void DrawLine(Pen, int x1, int y1, int x2, int y2)
public void DrawRectangle(Pen, Rectangle)
public void DrawRectangle(Pen, int x, int y, int w, int h)
public void DrawEllipse(Pen, Rectangle)
public void DrawImage(Image, Point)
public void DrawString(string, Font, Brush, Point)
public void FillEllipse(Brush, Rectangle)
public void FillRectangle(Brush, Rectangle)
12Operations in OnPaint()
- All code inside OnPaint() will be executed when
the - application window is showing to the screen.
protected override void OnPaint(PaintEventArgs
e) Graphics g e.Graphics Pen
blackPen new Pen(Color.Black, 3)
Rectangle rect new Rectangle( 0, 0, 200, 100)
g.DrawEllipse(blackPen, rect)
13 Pen redPen new Pen(Color.Red, 3)
Point p1 new Point(150, 150) Point p2
new Point(500, 300) g.DrawLine(redPen, p1,
p2) g.FillEllipse(new
SolidBrush(Color.FromArgb (180,Color.Green)),
new Rectangle(50,50, 200,200))
g.FillRectangle(new SolidBrush(Color.Red), 20,
20, 50, 50) g.FillRectangle(new
SolidBrush(Color.FromArgb(180, Color.Yellow)),
40, 40, 50, 50) HatchBrush hb new
HatchBrush(HatchStyle.ForwardDiagonal,
Color.Green, Color.FromArgb(100,
Color.Yellow)) g.FillEllipse(hb, 250, 10,
100, 100) Image newImage
Image.FromFile("dog.gif")
g.DrawImage(newImage, new Point( 300, 100))
14 string drawString "UH Sugarland"
Font drawFont new Font("Arial", 36,
FontStyle.BoldFontStyle.Italic )
SolidBrush drawBrush new SolidBrush(Color.Red)
PointF drawPoint new Point(50, 300)
g.DrawString(drawString, drawFont, drawBrush,
drawPoint) LinearGradientBrush lb new
LinearGradientBrush ( new Point(200,200),
new Point(400,300), Color.Yellow ,
Color.Blue) g.FillRectangle(lb, 200, 200,
200, 100)
15Output