Presentation is loading. Please wait.

Presentation is loading. Please wait.

Essential Structures [Chapter-3]

Similar presentations


Presentation on theme: "Essential Structures [Chapter-3]"— Presentation transcript:

1 Essential Structures [Chapter-3]
Course : CSE-791 Advanced Windows Programming Instructor : Dr. James Fawcett Semester : Summer 2002 Presented By : Vivekananthan Murugesan & Minu Puranik

2 Topics Classes and Structures
Point [Two Dimensional Coordinate Points] Size [Two-Dimensional Sizes in terms of Width & Height] Rectangle Color Pens and Brushes Paint Procedures String Measurement

3 Classes & Structures C++
- Structure: All methods & fields are public by default - Class: All methods & fields are private by default C# - Structure: Value Type - Class: Reference Type - In both the types all methods, fields, events & properties are private by default

4 Value Type Vs Reference Type
Question Value Type Reference Type Allocation Stack [Even though created using new – in case of struct] Heap Representation Local Copies Pointers to Memory Allocated Base Type Must derive from System.ValueType Any other type. [Except: System.ValueType & Sealed] Support Inheritance NO [Always Sealed] YES [if NOT sealed] Parameter Passing By Value [copy] By Reference Constructor Support YES. But Default Constructors are reserved. YES. [Of Course!] Existence Deleted when it falls out of the defined Scope Until Garbage Collected Example int, float, bool, any struct Form, any class

5 Value Type Vs Reference Type
CODE EXAMPLE int a = 5 int b = a; a = 10; What is current value of b? b = 5 Form form1 = new Form(); Form form2 = form1; form2.Text = “CSE 791”; What is current value of form1.Text ? “CSE 791”

6 Point Point is a Number Pair (X , Y) - X : horizontal coordinate
- Y : vertical coordinate - Generally represents a Point in a 2D Coordinate System - Thus, X & Y can have positive and negative values +y +x -x (0, 0) -y

7 Point […Continued] Point Constructor Type Example Point()
Point tempPt = new Point(); //[X=0, Y=0] Point(int x, int y) Point tempPt = new Point(5, 10); // [X=5, Y=10] Point(int xypacked) Point tempPt = new Point(0x ); // [X=16, Y=256] Point(Size size) Point tempPt = new Point(new Size(5, 10)); // [X=5, Y=10] Special One [Using Static Field] Point tempPt =Point.Empty; // [X=0, Y=0]

8 Point […Continued] Point Creation Point tempPt; // WRONG!!!
Point tempPt = new Point(); // YES! Point Properties Type Property Accessibility Example int X Get/Set int x = tempPt.X; //[Get] tempPt.X = 5; //[Set] Y int y = tempPt.Y; //[Get] tempPt.Y = 10; //[Set] bool IsEmpty Get bool flag = tempPt.IsEmpty; //[Get]

9 Point Public Interface Methods
Point […Continued] Point Public Interface Methods Method Functionality Type GetType() Returns a Type object which fully describes object currently referenced [In Short: RTTI] int GetHashCode() Returns an integer which identifies a specific object instance string ToString() Returns the string representation of Object Output Format: {X=0, Y=0} Usage: Console.Write(tempPt.ToString()); bool Equals(Point) Compares two Points [Both X & Y are compared separately] Usage: bool isSame = tempPt1.Equals(tempPt2); Usage: Bool isSame = tempPt1 == tempPt2; Void Offset() Adds given offset to the Point Usage: tempPt.Offset(5,10); //tempPt.X+=5; tempPt.Y+=10;

10 Size Size is also a Number Pair (Width, Height)
- Similar to Point, but has Width & Height instead of X & Y - Width & Height can have negative values. [Mainly used for computations purpose]

11 Size […Continued] Size Constructor Type Example Size()
Size tempSz = new Size(); //[Width=0, Height=0] Size(int width, int height) Size tempSz = new Size(5, 10); // [Width=5, Height=10] Size(Point point) Size tempSz = new Size(new Point(5,10));

12 Size Creation Size tempSz; // WRONG!!! Size Properties
Size tempSz = new Size(); // YES! Size Properties Type Property Accessibility Example int Weight Get/Set int w = tempSz.Width; //[Get] tempSz.Width = 5; //[Set] Height int h = tempSz.Height; //[Get] tempSz.Height = 10; //[Set] bool IsEmpty Get bool flag = tempSz.IsEmpty; //[Get]

13 Point & Size Construction Casting Operations
Point tempPt = new Point(new Size(5, 10)); // [X=5, Y=10] Size tempSz = new Size(new Point(5,10)); // [Width=5, Height=10] Casting tempPt = (Point)tempSz; tempSz = (Size)tempPt; Operations tempSz3 = tempSz1 + tempSz2; tempSz3 = tempSz1 – tempSz2; tempSz1 += tempSz2; tempSz1 -= tempSz2; tempPt2 = tempPt1 + tempSz; tempPt2 = tempPt1 – tempSz; tempPt1 += tempSz; tempPt1 -= tempSz;

14 PointF and SizeF Floating Point Representations of Point and Size
PointF: Similar to Point except X & Y are float values SizeF: Similar to Size except Width & Height are float values General Syntax: - tempPtF.X = 5.5; //Error - tempPtF.Y = 6.5; //Error - tempPtF.X = (float)5.5; // OR tempPtF.X = 5.5f; - tempPtF.Y = (float)6.5; // OR tempPtF.Y = 6.5f;

15 PointF and SizeF […Continued]
Constructor Comparison Point PointF Size SizeF () (x , y) (Size) (SizeF) (Point) (PointF) (xyPacked)

16 PointF and SizeF […Continued]
Casting To  From Point PointF Size SizeF YES NO

17 PointF and SizeF […Continued]
Point Static Methods Name of Method Functionality Point Round(PointF ptF) Rounds X & Y to nearest positive integer. Point Truncate(PointF ptF) Strips the fractional part and rounds towards next lowest integer. Point Ceiling(PointF ptF) Rounds towards next highest integer.

18 PointF and SizeF […Continued]
Size Static Methods Name of Method Functionality Size Round(SizeF ptF) Rounds Width & Height to nearest positive integer. Size Truncate(SizeF ptF) Strips the fractional part and rounds towards next lowest integer. Size Ceiling(SizeF ptF) Rounds towards next highest integer. SizeF Instance Methods Name of Method Functionality PointF ToPointF() Converts SizeF to PointF. Size ToSize() Equivalent to Truncate().

19 Rectangle “Rectangle” structure defines a rectangle as a combination of a Point and a Size Point refers to the location of the upper left corner of the rectangle and Size is the Width and Height of this Rectangle. Size should be Non-Negative. [No specific Restriction] Constructors Rectangle(Point pt, Size size) Rectangle(int x, int y, int width, int height)

20 RectangeF Constructors
RectangleF is similar to Rectangle except that the data types associated with RectangleF are float, PointF and SizeF rather than int, Point and size. Constructors RectangleF(PointF ptf, SizeF sizef) RectangleF(float x, float y, float width, float height)

21 Rectangle & RectangleF
Rectangle Static Methods Rectangle Round(RectangleF rectF) Rectangle Truncate(RectangleF rectF) Rectangle Ceiling(RectangleF rectF) Rectangle/RectangleF ToString Method Output Format: {X=0, Y=0, Width=0, Height=0} Note: RectangleF is similar to Rectangle except that the data types associated with RectangleF are float, PointF and SizeF rather than int, Point and size. Hence following slides refer to Rectangle which are also applicable to RectangleF.

22 Rectangle […Continued]
Rectangle Properties Type Property Accessibility Point Location Get/Set Size int X Y Width Height Left Get Int Top Right Bottom Bool IsEmpty

23 Rectangle […Continued]
Comparing Two Rectangles Equality Operator [==] Inequality Operator [!=] Equals Method Creating Rectanlges [Old Way] static Rectangle FromLTRB(int xLeft, int yTop, int xRight, int yBottom

24 Rectangle […Continued]
Offset and Inflate Methods void Offset(int x, int y) void Offset(Point pt) void Inflate(int x, int y) static Rectangle Inflate(Rectangle rect, int x, int y) rect.Offset(x,y) is equivalent to: rect.X+=x; rect.Y+=y rect.Inflate(x,y) is equivalent to: rect.X-=x5; rect.Y-=y; rect.Width +=2*x; rect.Height +=2*y;

25 Rectangle […Continued]
More Rectangle Methods static Rectangle Union(Rectangle rect1, Rectangle rect2) static Rectangle Intersect(Rectangle rect1, Rectangle rect2) void Intersect(Rectangle rect) bool Contains(Point) bool Contains(int x, int y) bool Contains(Rectangle rect) bool IntersectsWith(Rectangle rect)

26 Form Properties/Events
- Form class has 14 Properties. [11 inherited from Control] Form(Control) Properties (selection) Type Property Accessibility Comments Point Location Get/Set Location relative to Screen Size Size of Full Form Rectangle Bounds Equals Rectangle(Location, Size) int Width Equals Size.Width Height Equals Size.Height Left Equals Location.X Top Equals Location.Y Right Get Equals Location.X + Size.Width Bottom Equals Location.Y + Size.Height

27 Form Properties/Events
DON’T DO - Size.Width =2; //Accessing Property of Property Form Properties (selection) Type Property Accessibility Comments Point DesktopLocation Get/Set Location relative to Screen, taking windows taskbar into account Rectangle DesktopBounds Size of Full Form taking windows taskbar into account

28 Form Properties/Events
Form(Control) Properties (selection) - Client Area Excludes: Caption Bar, Border, Menu, ScrollBar Type Property Accessibility Comments Size ClientSize Get/Set Size of ClientArea Rectangle ClientRectangle Get No Additional Information! X=0; Y=0; But useful for passing as parameter to Rectangle parameter. See: FormSize Demo

29 Form Properties/Events
Control Events (Selection) Control Invalidate Methods Event Method Delegate Argument Move OnMove EventHandler EventArgs Resize OnResize void Invalidate() void Invalidate(Rectangle rect) void Invalidate(bool IncludeChildren) void Invalidate(Rectangle rect, bool IncludeChildren) void Invalidate(Region regn) void Invalidate(Region regn, bool IncludeChildrren)

30 Form Properties/Events
Invalidate() - Informs the Windows that client are is no longer valid and to be repainted. - causes a call to OnPaint - OnPaint call doesn’t occur right away - all pending requests will be served first - To Update the client area immediately follow the invalidate call with a call to Control Object’s Update method. void Update();

31 Color Structure Color in Windows Forms is based on ARGB(a;pha-red-green-blue) model alpha channel determines the transparency of the color. It ranges from 0 for transparent to 0xFF for opaque. Construction: Color color = new Color(); //creates a transparent black color //only default constructor is supported

32 Color Structure Static Color Properties
- Provide 140 Colors [HTML Standard] to choose from. - 141st property, named Transparent, represents a transparent color Type Property Accessibility Color AliceBlue Get AntiqueWhite YellowGreen Transparent

33 Color Structure Color.FromArgb Static Methods
- To create create colors based on red, green, blue and alpha components, you can use the following Color.FromArgb static methods. Color Color.FromArgb(int r, int g, int b) Color Color.FromArgb(int a, int r, int g, int b) Color Color.FromArgb(int a, Color color) Color Color.FromArgb(int argbPacked)

34 Pens and Brushes Pen Object is used for drawing lines or curves
Brush Object is used for drawing filled areas and text Pens and Brushes themselves are specified using Color, but other characteristics are often involved as well. Constructing Pen 1. Pen mypen = new Pen(new Color()); 2. Pen mypen = new Pen(Color.Red) 3. Pen mypen = Pens.Red //Pens has 141 static properties More Descriptions about Pen on chapter 17!

35 Pens and Brushes Brush:
- Brush class itself is abstract, which means you can’t create instance of it - Brush is parent class for 5 other classes. SolidBrush, HatchBrush, TextureBrush, LinearGradientBrush and PathGradientBrush. Brush Construction: 1. Brush mybrush = new SolidBrush(new Color()); 2. Brush mybrush = new SolidBrush(Color.Red); 3. Brush mybrush =Brushes.Red //Brushes has 141 static properties More Descriptions about Brushes on chapter 17!

36 System Colors Also called “User-Preference Colors”
Colors set by the users for the Windows Environment using Display Properties Windows maintains 29 colors for the user interface out of which 26 are available in the Windows Forms Framework. One can obtain these colors from SystemColors class, which consists of 26 read-only properties, each of which returns a Color Object. Type Property Accessibility Comments Color ActiveBorder Get Border of Active Window ActiveCaption Caption Bar of Active Window ControlText Text Color of Controls Desktop Windows Desktop Color WindowText Window Text

37 Pens and Brushes Creating Pens and Brushes using SystemColors:
- Pen mypen = new Pen(SystemColor.Desktop); - Brush mybrush = new SolidBrush(SystemColor.Desktop); SystemPens: - has 15 static read only properties that return object of type Pen Type Property Accessibility Pen ActiveCaptionText Get Control WindowText

38 Pens and Brushes SystemBrushes:
- has 21 static read only properties that return object of type Brush Type Property Accessibility Brush ActiveBorder Get ActiveCaption WindowText

39 Pens & Brushes More Ways to Create:
Pen mypen = SystemPens.FromSystemColor(SystemColor.ActiveBorder); Brush mybrush = SystemBrushes.FromSystemColor(SystemColor.MenuText); Known Colors: KnownColor is an Enumeration that encompasses all the color names and all the system colors

40 Form Examples RandomClear Example: RandomClearResizeRedraw Example:
- Clears the Form with different colors selected at random. - Clear is restricted to the region that’s newly invalid. thus entire form is not repainted. RandomClearResizeRedraw Example: - 1 way for complete Clear: override OnResize and Put a invalidate call. - Another way is set the ResizeRedraw property to true.

41 StringFormat Used to Change the default text alignment Properties:
StringAlignment Enumeration: Type Property Accessibility Comments StringAlignment Alignment Get/Set Horizontal alignment Color LineAlignment Vertical Alignment Member Value Description Near Usually left or top Center 1 Always Center Far 2 Usually right or bottom More Descriptions about StringFormat on chapter 9!

42 Form Examples FourCorners Example - Using StringFormat to display Text
HelloCenteredAlignment Example HelloCenteredMeasured Example - Using SizeF sizeText = grfx.MeasueString(str, font); More Descriptions about MeasueString on chapter 9!

43 Form Examples HelloCenteredRectangle Example HuckleberryFinn Example
- Using void DrawString(string str, Font f, Brush b, Rectangle rect, Stringformat strfmt) HuckleberryFinn Example void DrawString(string str, Font f, Brush b, Rectangle rect)

44 Reference Programming Microsoft Windows with C# - Charles Petzold
C# and the .Net Platform - Andrew Troelsen


Download ppt "Essential Structures [Chapter-3]"

Similar presentations


Ads by Google