Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# and the NET Platform, Second Edition - Andrew Troelsen.pdf
Скачиваний:
67
Добавлен:
24.05.2014
Размер:
22.43 Mб
Скачать

Summary C# and the .NET Platform, Second Edition

by Andrew Troelsen ISBN:1590590554

This chapter introduced the fine art of building a user interface with the types contained in the

Apress © 2003 (1200 pages)

System.Windows.Forms namespace. It began by examining the basic steps you must take to build a

This comprehensive text starts with a brief overview of the

custom Form. ThisC#entailedlanguageaanddiscussionthen quicklyof themovesApplicationto keyobject,technicandl andits various members. As you have seen, the Form typearchitecturgains almajorityissues forof.itsNETfunctionalitydevelopers.from a rather long chain of base types.

During the course of this chapter, you learned how to build topmost menus (and pop-up menus) and how

Tableto respondof Contentsto a number of menu events. You also came to understand how to further enhance your Form C#objectsand theusing.NETtoolbarsPlatform,andSecondstatusEditionbars. Finally, this chapter illustrated how to construct MDI applications

using Windows Forms.

Introduction

Part One - Introducing C# and the .NET Platform

Chapter 1 - The Philosophy of .NET

Chapter 2 - Building C# Applications

Part Two - The C# Programming Language

Chapter 3 - C# Language Fundamentals

Chapter 4 - Object-Oriented Programming with C#

Chapter 5 - Exceptions and Object Lifetime

Chapter 6 - Interfaces and Collections

Chapter 7 - Callback Interfaces, Delegates, and Events

Chapter 8 - Advanced C# Type Construction Techniques

Part Three - Programming with .NET Assemblies

Chapter 9 - Understanding .NET Assemblies

Chapter 10 - Processes, AppDomains, Contexts, and Threads

Chapter 11 - Type Reflection, Late Binding, and Attribute-Based Programming

Part Four - Leveraging the .NET Libraries

Chapter 12 - Object Serialization and the .NET Remoting Layer

Chapter 13 - Building a Better Window (Introducing Windows Forms)

Chapter 14 - A Better Painting Framework (GDI+)

Chapter 15 - Programming with Windows Forms Controls

Chapter 16 - The System.IO Namespace

Chapter 17 - Data Access with ADO.NET

Part Five - Web Applications and XML Web Services

Chapter 18 - ASP.NET Web Pages and Web Controls

Chapter 19 - ASP.NET Web Applications

Chapter 20 - XML Web Services

Index

List of Figures

List of Tables

Part Four - Leveraging the .NET Libraries
Part Two - The C# Programming Language

Chapter C#14:andAtheBetter.NET Platform,PaintingSecond EditionFramework (GDI+)

by Andrew Troelsen

ISBN:1590590554

Apress © 2003 (1200 pages)

 

Overview This comprehensive text starts with a brief overview of the C# language and then quickly moves to key technical and

Theprevious chapterarchitecturalintroducedissuesyouforto .theNETfined velopersart of building. a traditional main window using various types contained within the System.Windows.Forms namespace. Now that you can assemble a Form to represent the shell of your GUI-based applications, the next task is to understand the details of rendering

Tablegraphicalof Contentsdata (including stylized text and image data) onto the Form's client area.

C# and the .NET Platform, Second Edition

We begin by taking a high-level view of the numerous drawing-related namespaces, and examine the

Introduction

process of responding to (and initiating) paint sessions. You will also discover various ways of obtaining

Part One - Introducing C# and the .NET Platform

(and configuring) a Graphics object. Once you understand the general layout of the GDI+ landscape, the

Chapter 1 - The Philosophy of .NET

remainder of this chapter covers how to manipulate colors, fonts, geometric shapes, and graphical

Chapter 2 - Building C# Applications

images. This entails understanding related types such as Brush, Pen, Color, Point, and Rectangle (among

others). This chapter also explores a number of GDI+-centric programming techniques such as

Chapter 3 - C# Language Fundamentals

nonrectangular hit testing and GUI drag-and-drop logic.

Chapter 4 - Object-Oriented Programming with C#

Chapter 5 - Exceptions and Object Lifetime

The chapter concludes by exploring the new .NET resource format. While technically not part of GDI+ Chapterproper,6it does- Interfacesinvolveandthe manipulationCollections of graphical data (which in my opinion is "GDI+-enough" to be

Chaptpresentedr 7 -here)Callback. Here,Interfaces,you learnDelegates,how to embedand Eventsyour application's external resources directly into a .NET

Chapterassembly8 to- Advancedship moreC# portableType Constructionbinary imageTechniques. During the process, you explore the System.Resources

PartnamespaceThree - Programmingand learn howithto .performNET Assembliesread/write operations on the underlying *.resx and *.resources files Chapterby hand,9 as- Understandingwell as pull resources.NET Assembliesfrom an assembly at runtime using the

System.Resources.ResourceManager type.

Chapt r 10 - Processes, AppDomains, Contexts, and Threads

Chapter 11 - Type Reflection, Late Binding, and Attribute-Based Programming

Note If you are a Web programmer by trade, you may think that GDI+ is of no use to you. However, as

you will see later in this text during our examination of ASP.NET, GDI+ is not limited to traditional

Chapter 12 - Object Serialization and the .NET Remoting Layer

desktop applications and is extremely relevant for Web applications.

Chapter 13 - Building a Better Window (Introducing Windows Forms) Chapter 14 - A Better Painting Framework (GDI+)

Chapter 15 - Programming with Windows Forms Controls

Chapter 16 - The System.IO Namespace

Chapter 17 - Data Access with ADO.NET

Part Five - Web Applications and XML Web Services

Chapter 18 - ASP.NET Web Pages and Web Controls

Chapter 19 - ASP.NET Web Applications

Chapter 20 - XML Web Services

Index

List of Figures

List of Tables

Survey of theC# andGDI+the .NamespacesET Platform, S cond Edition

by Andrew Troelsen

ISBN:1590590554

The .NET Framework provides a number of namespaces devoted to two-dimensional graphical rendering.

Apress © 2003 (1200 pages)

In addition to the basic functionality you would expect to find in a graphics package (color, font, pen, brush,

This comprehensive text starts with a brief overview of the

and image manipulation),C# languageyouandalsothenfindquicklytypes thatmovenables to keygeometrictechnicaltransformations,and antialiasing, palette blending, and documentarchitectuprintingal ssuessupportfor .NET. Collectivelydevelopersspeaking,. these namespaces make up the .NET facility we call GDI+, which is a vast improvement over the traditional Win32 Graphical Device Interface (GDI) API. Table 14-1 gives a high-level view of each major player.

Table of Contents

C# and the .NET Platform, Second Edition

 

 

 

 

 

 

Table 14-1: The Core GDI+ Namespaces

 

 

 

 

 

 

 

Introduction

.NETMeaningPlatformin Life

 

 

 

 

PartGDI+NamespaceOne - Introducing C# and the

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Chapter 1

- The Philosophy of .NET

 

 

 

 

 

 

 

System.Drawing

This is the key GDI+ namespace, which defines numerous types

 

 

 

Chapter 2

- Building C# Applications

 

 

 

 

 

 

 

 

 

for basic rendering (fonts, pens, basic brushes, etc.) as well as

 

 

Part Two - The C# Programming Language

 

 

 

 

 

 

 

 

 

the almighty Graphics type.

 

 

 

 

Chapter 3

- C# Language Fundamentals

 

 

 

 

 

 

 

 

 

 

 

 

 

 

System.Drawing.Drawing2D

This namespace provides types used for more advanced two-

 

 

 

Chapter 4

- Object-Oriented Programming with C#

 

 

 

 

 

 

 

Chapter 5

 

dimensional graphics functionality (e.g., gradient brushes, pen-

 

 

 

- Exceptions and Object Lifetime

 

 

 

 

 

 

 

 

 

caps, geometric transforms, etc.).

 

 

 

 

 

Chapter 6

- Interfaces and Collections

 

 

 

 

 

 

 

Chapter 7

- Callback Interfaces,

Delegates, and Events

 

 

 

 

 

 

 

System.Drawing.Imaging

This namespace defines types that allow you to directly

 

 

 

Chapter 8

- Advanced C# Type

Construction Techniques

 

 

 

 

 

 

 

 

 

manipulate graphical images (e.g., change the palette, extract

 

 

Part Three - Programming with .NET Ass mblies

 

 

 

 

 

 

 

 

 

image metadata, manipulate metafiles, and so forth).

 

 

 

 

 

 

 

 

 

 

 

 

 

Chapter 9

- Understanding .NET

Assemblies

 

 

 

 

 

 

 

System.Drawing.Printing

This namespace defines types that allow you to render images to

 

 

 

Chapter 10

- Processes, AppDomains,

Contexts, and Threads

 

 

 

 

 

 

Chapter 11

- Type Reflection, Late

the printed page, interact with the printer itself, and format the

 

 

 

Binding, and Attribute-Based Programming

 

 

 

 

 

 

 

overall appearance of a given print job.

 

 

Part Four - Leveraging the .NET

Libraries

 

 

 

 

 

 

 

 

ChapterSystem12.Drawing- Object.TextS rialization andThisthe(rather.NET Resmall)otingnamespaceLay r

allows you to manipulate

 

 

Chapter 13

- Building a Better Windowcollections(Introducingof fonts.WindowsFor example,Forms) as you see in this chapter, the

 

 

Chapter 14

- A Better Painting FrameworkInstalledFontCollection(GDI+)

type allows you to dynamically discover

 

 

 

 

 

Chapter 15

- Programming with

the set of installed fonts on the target machine.

 

 

 

Windows Forms Controls

 

 

 

 

 

 

 

 

 

 

 

 

 

Chapter 16

- The System.IO Namespace

 

 

 

 

 

Chapter 17

- Data Access with ADO.NET

 

 

 

 

Part Five - Web Applic tions and XML W b Services

 

 

 

 

 

 

Configuring a GDI+ Project Workspace

 

 

 

Chapter 18

- ASP.NET Web Pages and Web Controls

 

 

 

 

When you wish to make use of GDI+, you must set a reference to the System.Drawing.dll assembly. This

Chapter 19 - ASP.NET Web Applications

single binary contains definitions of the types for each of the core GDI+ namespaces. Be aware that if you

Chapter 20 - XML Web Services

select a new Windows Application Project Workspace using VS .NET, this reference is set on your behalf

Index

automatically. Other project types, however, may require you to set this assembly reference explicitly. In

List of Figures

any case, once you have set this reference, just make use of the C# "using" keyword and you are ready to

List of Tables render:

// Don't forget to reference System.Drawing.dll! using System.Drawing;

To begin the GDI+ journey, let's examine the functionality defined by the System.Drawing namespace.

Overview ofC#theandSystemthe .NET Platform,.DrawingSecondNamespaceEdition

by Andrew Troelsen

ISBN:1590590554

A vast majority of the types used when programming GDI+ applications are found within the

Apress © 2003 (1200 pages)

System.Drawing namespace. As you would expect, there are classes that represent images, brushes,

This comprehensive text starts with a brief overview of the

pens, and fonts. Furthermore,C# language andSystemthen.Drawingquickly movesdefinesto akeynumbertechnicalof relatedand types such as Color, Point, and Rectangle. Tablearchitectur14-2allistsissuessomefor(but.NETnotdevelopersall) of the. core types.

Table 14-2: Core Types of the System.Drawing Namespace

Table of Contents

 

 

 

C#System.Drawingand the .NET Platform,

 

SecondMeaningEditionin Life

 

IntroductionType

 

 

 

 

 

 

 

 

Part One - Introducing C#

 

and the .NET Platform

 

 

Bitmap

 

Encapsulates a given image file (*.bmp or otherwise) and defines a

 

Chapter 1 - The Philosophy

 

of .NET

 

 

 

 

number of methods to manipulate the underlying graphical data.

 

 

Chapter 2 - Building C#

 

Applications

 

 

 

 

PartBrushTwo - The C# ProgrammingBrushLanguageobjects are used to fill the interiors of graphical shapes such as

 

Chapter 3 - C# Language Fundamentalsrectangles, ellipses, and polygons. These types represent a number of

 

 

Brushes

 

brush variations, with Brush functioning as the abstract base class to the

 

Chapter 4 - Object-Oriented Programming with C#

 

 

SolidBrush

 

remaining types. Additional Brush types are defined in the

 

Chapter 5 - Exceptions and Object Lifetime

 

 

SystemBrushes

 

System.Drawing.Drawing2D namespace.

 

Chapter 6 - Interfaces and Collections

 

 

ChapterTextureBrush7 - Callback Interfaces, Delegates, and Events

 

 

 

 

 

 

 

Chapter 8 - Advanced C#

 

Type Construction Techniques

 

 

Color

 

As you have already seen in the previous chapter, the Color structure

 

Part Three - Programming

 

with .NET Assemblies

 

 

SystemColors

 

defines a number of static fields that can be used to configure the color

 

Chapter 9 - Understanding

 

.NET Assemblies

 

ChapterColorTranslator10 - Processes,

 

of fonts, brushes, and pens. The ColorTranslator type allows you to build

 

 

AppDomains, Contexts, and Threads

 

 

 

 

a new .NET Color type from other color representations (Win32, the

 

Chapter 11 - Type Reflection,

ate Binding, and Attribute-Based Programming

 

 

 

 

OLE_COLOR type, HTML color constants, etc.).

 

 

 

 

 

 

Part Four - Leveraging the

 

.NET Libraries

 

 

Font

 

The Font type encapsulates the characteristics of a given font (i.e., type

 

Chapter 12 - Object Serialization and the .NET Remoting Layer

 

 

 

 

name, bold, italic, point size, and so forth). FontFamily provides an

 

ChapterFontFamily13 - Building a Better Window (Introducing Windows Forms)

 

 

 

 

abstraction for a group of fonts having a similar generic design but with

 

Chapter 14 - A Better Painting Framework (GDI+)

 

 

 

 

certain variations in styles.

 

 

Chapter 15 - Programming

 

with Windows Forms Controls

 

 

 

 

 

ChapterGraphics16 - The System.IO

 

ThisNamespacecore class represents a valid drawing surface, as well as a number

 

 

Chapter 17 - Data Access

 

withof methodsADO.NET to render text, images, and geometric patterns. Consider this

 

Part Five - Web Applications

 

typeandtheXML.NETWebequivalentServices of a Win32 HDC.

 

 

 

 

 

 

 

Chapter 18 - ASP.NET Web

 

Pages and Web Controls

 

 

Icon

 

These classes represent custom icons, as well as the set of standard

 

 

Chapter 19 - ASP.NET Web

 

Applications

 

 

SystemIcons

 

system supplied icons.

 

 

Chapter 20 - XML Web Services

 

 

 

 

 

 

Index

 

Image is an abstract base class that provides functionality for the Bitmap,

 

 

Image

 

 

List of Figures

 

Icon, and Cursor types. ImageAnimator provides a way to iterate over a

 

 

ImageAnimator

 

number of Image-derived types at some specified interval.

 

List of Tables

 

 

 

 

 

 

 

 

Pen

 

Pens are objects used to draw lines and curves. The Pens type defines a

 

 

Pens

 

number of static properties that return a new Pen of a given color.

 

 

 

 

 

 

SystemPens

 

 

 

 

 

 

 

 

 

Point

 

These structures represent an (x,y) coordinate mapping to an underlying

 

 

PointF

 

integer or float (respectively).

 

 

 

 

 

 

 

Rectangle

 

These structures represent a rectangular dimension (again mapping to

 

 

RectangleF

 

an underlying integer or float).

 

 

 

 

 

 

 

Size

 

These structures represent a given height/width (again mapping to an

 

 

SizeF

 

underlying integer or float).

 

 

 

 

 

 

 

StringFormat

 

This type is used to encapsulate various features of textual layout (i.e.,

 

 

 

 

alignment, line spacing, etc.).

 

 

Region

C# and

 

theDescribes.NET Platform,the interiorSecondof a geometricEdition image composed of rectangles and

 

 

 

 

 

paths.

ISBN:1590590554

 

 

 

by Andrew Troelsen

 

 

 

 

 

 

 

 

Apress © 2003 (1200 pages)

Many of these core types make substantial use of a number of related enumerations, most of which are

This comprehensive text starts with a brief overview of the

also defined within the System.Drawing namespace. As you can guess, many of these enumerations are

C# language and then quickly moves to key technical and

used to configurearchitecturalthe look andissufeelsofforbrushes.NET developersand pens. . For example, ponder the types listed in Table 14- 3.

Table of Contents

 

 

 

 

Table 14-3: Enumerations in the System.Drawing Namespace

C# and the .NET Platform, Second

Edition

 

 

IntroductionSystem.Drawing

Meaning in Life

 

 

PartEnumerationOne - Introducing C# and the

.NET Platform

 

 

 

 

 

 

 

 

 

 

Chapter 1

- The Philosophy of .NET

Specifies how to align content on a drawing surface (center, left,

 

 

 

 

ContentAlignment

 

 

 

Chapter 2

- Building C# Applications

right, and so forth)

 

 

Part Two - The C# Programming Language

 

 

 

 

 

FontStyle

Specifies style information applied to text (bold, italic, etc.)

 

 

 

Chapter 3

- C# Language Fundamentals

 

 

 

 

 

 

 

 

 

Chapter 4

- Object-Oriented Programming with C#

 

 

 

 

GraphicsUnit

Specifies the unit of measure for the given item (much like the

 

 

 

Chapter 5

- Exceptions and Object Lifetime

 

 

 

 

 

 

Win32 mapping mode constants)

 

 

 

 

 

 

 

 

 

Chapter 6

- Interfaces and Collections

Specifies friendly names for the known system colors

 

 

 

 

KnownColor

 

 

 

 

Chapter 7

- Callback Interfaces,

Delegates, and Events

 

 

 

 

Chapter 8

- Advanced C# Type Construction Techniques

 

 

 

 

StringAlignment

Specifies the alignment of a text string relative to its layout

 

 

Part Three - Programming with .NET Assembli s

 

 

 

 

 

 

rectangle

 

 

 

 

 

 

 

 

Chapter 9

- Understanding .NET

Assemblies

 

 

 

 

StringFormatFlags

Specifies the display and layout information for text strings (e.g.,

 

 

 

Chapter 10

- Processes, AppDomains,

Contexts, and Threads

 

 

 

 

 

 

NoWrap, LineLimit, and so on)

 

 

 

 

Chapter 11

- Type Reflection, Late

Binding, and Attribute-Based Programming

 

 

 

 

 

 

Part Four - Leveraging the .NET Libraries

 

 

 

 

StringTrimming

Specifies how to trim characters from a string that does not

 

 

Chapter 12

- Object Serialization and thecompletely.NET Remotingfit into aLayerlayout shape

 

 

 

 

 

 

 

 

 

Chapter 13

- Building a Better Window

(Introducing Windows Forms)

 

 

 

 

StringUnit

Specifies the units of measure for a text string

 

 

 

 

Chapter 14

- A Better Painting Framework (GDI+)

 

 

Chapter 15

- Programming with Windows Forms Controls

Chapter 16

- The System.IO Namespace

 

If you currently have a background using graphic toolkits found in other frame-works (such as Java or

Chapter 17

- Data Access with ADO.NET

MFC) you should feel right at home with the functionality provided by the .NET System.Drawing

PartnamespaceFive - Web. NextApplicationsup, let's examineand XML WthebsetServicesof basic utility types that are commonly used in GDI+

Chapterprogramming18 - ASP. .NET Web Pages and Web Controls

Chapter 19 - ASP.NET Web Applications

Chapter 20 - XML Web Services

Index

List of Figures

List of Tables

ExaminingC#theandSystemthe .NET Platform,.DrawingSecondUtilityEditionTypes

by Andrew Troelsen ISBN:1590590554

Many of the drawing methods defined by the System.Drawing.Graphics class require you to specify the

Apress © 2003 (1200 pages)

position or area in which you wish to render a given item. For example, the DrawString() method requires

This comprehensive text starts with a brief overview of the

you to specify theC#locationl nguageto renderand thenthequicklytext stringmovesontothekeyControltechnical-derivedand type. Given that DrawString() has been overloadedarchitecturala number ofissuestimes,forthis.NETpositionaldevelopersparameter. may be specified using an (x,y) coordinate

or the dimensions of a "box" to draw within. Other GDI+ type methods may require you to specify the width and height of a given item, or the internal bounds of a geometric image.

Table of Contents

C#Toandspecifythe .NETsuchPlatform,information,SecondtheEditionSystem.Drawing namespace defines the Point, Rectangle, Region, and

Size types. Obviously, a Point represents some (x,y) coordinate. Rectangle types capture a pair of points

Introduction

representing the upper left and lower right bounds of a rectangular region. Size types are similar to

Part One - Introducing C# and the .NET Platform

Rectangles, however these structures represent a given dimension using a given length and width.

Chapter 1 - The Philosophy of .NET

Regions provide a way to represent and manipulate nonrectangular drawing surfaces.

Chapter 2 - Building C# Applications

Part Two - The C# Programming Language

The member variables used by the Point, Rectangle, and Size types are internally represented as an

Chapter

3

- C# Language Fundamentals

 

integer data type. If you need a finer level of granularity, you are free to make use of the corresponding

Chapter

4

- Object-Oriented Programming with C#

 

PointF, RectangleF, and SizeF types, which (as you might guess) map to an underlying float. Regardless

Chapter

5

- Exceptions and Object Lifetime

 

of the underlying data representation, each type has an identical set of members, including a number of

Chapter

6

- Interfaces and Collections

 

overloaded operators. A quick run-through follows.

Chapter

7

- Callback Interfaces, Delegates, and Events

Chapter

8

- Advanced C# Type Construction Techniques

 

Point(F) Type

 

 

 

 

 

 

 

Part Three - Programming with .NET Assemblies

Chapter

9

- Understanding .NET Assemblies

 

The first utility type you should be aware of is System.Drawing.Point(F). As you recall, you created a

Chapter

10

- Processes, AppDomains, Contexts, and Threads

 

custom Point type earlier in this text, which in many ways was a slimmed-down version of the official GDI+

Chapter

11

- Type Reflection, L te Binding, and Attribute-B sed Programming

 

Point type. A breakdown of each member is shown in Table 14-4.

Part Four - Leveraging the .NET Libraries

 

 

Ch pter

12

- Object Serialization and the .NET Remoting Layer

 

Table

14-4: Key Members of the Point(F) Types

 

 

 

 

 

 

 

 

 

 

 

 

Chapter

13

- Building a Better

 

Window (Introducing Windows Forms)

 

 

 

 

ChapterPoint14 and- A BetterPointFPainting

 

FrameworkMeaning (GDI+)in Life

 

 

 

 

 

ChapterMember15

- Programming with Windows Forms Controls

 

 

 

 

 

 

 

 

 

Chapter

16

- The System.IO

 

Namespace

 

 

 

 

 

+

 

 

 

Allows you to manipulate the underlying (x,y) point using common

 

 

 

 

 

Chapter

17

- Data Access with

 

ADO.NET

 

 

 

 

 

-

 

 

 

overloaded operators.

 

 

 

Part Five - Web Applications

 

and XML Web Services

 

 

 

= =

18

- ASP.NET Web Pages and Web Controls

 

 

 

Chapter

 

 

 

Chapter!=

19

- ASP.NET Web Applications

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Chapter

20

- XML Web Services

 

These properties allow you to get and set the underlying (x,y) values.

 

 

 

 

 

X

 

 

 

 

 

 

Index

 

 

 

 

 

 

 

 

 

ListYof Figures

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

List of Tables

 

This static method convents a PointF type into a Point by rounding off

 

 

 

 

 

Ceiling()

 

 

 

 

 

 

 

 

 

 

 

the underlying floats into integers.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

IsEmpty

 

 

This property returns true if x and y are both set to zero.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Offset()

 

 

This method translates a given Point type by a given amount.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Although this type is most commonly used when working with GDI+ and user interface applications, do be aware that you may make use of any utility type from any application. To illustrate, here is a console application that makes use of the System.Drawing.Point type (be sure to set a reference to System.Drawing.dll).

using System;

using System.Drawing;

namespace UtilTypes

{

public class UtilTester

C# and the .NET Platform, Second Edition

{

by Andrew Troelsen ISBN:1590590554 public static int Main(string[] args)

{ Apress © 2003 (1200 pages)

This compCreateehensive text starts with a brief overview of the

// and offset a point.

C# language and then quickly moves to key technical and

Point pt = new Point(100, 72); architectural issues for .NET developers.

Console.WriteLine(pt);

pt.Offset(20, 20);

Console.WriteLine(pt);

Table of Contents

// Overloaded Point operators.

C# and the .NET Platform, Second Edition

Point pt2 = pt;

Introduction

if(pt == pt2)

Part One - Introducing C# and the .NET Platform

WriteLine("Points are the same");

Chapter 1 - The Philosophy of .NET

else

Chapter 2 - Building C# Applications

WriteLine("Different points");

Part Two - The C# Programming Language

// Change pt2's X value.

Chapter 3 - C# Language Fundamentals

pt2.X = 4000;

Chapter 4 - Object-Oriented Programming with C#

// Now show each X value:

Chapter 5 - Exceptions and Object Lifetime

Console.WriteLine("First point: {0} ", pt);

Chapter 6 - Interfaces and Collections

Console.WriteLine("Second point: {0} ", pt2);

Chapter 7 - Callback Interfaces,return 0;Delegates, and Events

Chapter 8 - Advanced} C# Type Construction Techniques

Part Three} - Programming with .NET Assemblies

Chapter} 9 - Understanding .NET Assemblies

Chapter 10 - Processes, AppDomains, Contexts, and Threads

Chapter 11 - Type Reflection, Late Binding, and Attribute-Based Programming

Part Four - Leveraging the .NET Libraries

ChapterRectangle(F)12 - Object SerializationType and the .NET Remoting Layer

Chapter 13 - Building a Better Window (Introducing Windows Forms)

ChapterRectangles,14 - AlikeB tterPoints,Paintingare usefulFrameworkin any(GDI+)application (GUI-based or otherwise). Some core members to be

aware of are listed in Table 14-5.

Chapter 15 - Programming with Windows Forms Controls

Chapter 16 - The System.IO Namespace

Table 14-5: Key Members of the Rectangle(F) Types

Chapter 17 - Data Access with ADO.NET

 

 

 

 

 

Part Five - Web Applications and XML Web Services

 

 

Rectangle and

 

Meaning in Life

 

Chapter 18 - ASP.NET Web Pages and Web Controls

 

 

RectangleF Member

 

 

 

 

Chapter 19 - ASP.NET Web Applications

 

 

 

 

Chapter= = 20 - XML Web Services Allows you to test if two rectangles have identical values (or not).

 

Index

 

 

 

 

!=

 

 

 

 

 

 

 

 

List of Figures

 

These static methods allow you to expand a rectangle, as well as

 

 

Inflate()

 

 

List of Tables

 

create new rectangles that are a result of an intersection or union

 

 

Intersect()

 

 

 

 

operation.

 

 

Union()

 

 

 

 

 

 

 

 

 

 

 

Top

 

These properties set the dimensions of a new Rectangle type.

 

 

Left

 

 

 

 

Bottom

 

 

 

 

Right

 

 

 

 

 

 

 

 

Height

 

Configures the height and width of a given Rectangle.

 

 

Width

 

 

 

 

 

 

 

 

Contains()

 

This method can be used to determine if a given Point (or Rectangle)

 

 

 

 

is within the bounds of the current Rectangle. Great for hit testing a

 

 

 

 

point within a rectangle.

 

Console.WriteLine("Point is within the rect!");

 

X

C# and the

 

.NETThesePlatform,propertiesSecondreturnEditionthe x or y coordinate of the Rectangle's

 

 

Y

by Andrew

 

upper left corner.

ISBN:1590590554

 

 

 

Troelsen

 

 

 

 

 

 

 

 

Apress © 2003 (1200 pages)

One of the more Thisusefulcomethodsprehensiveof thetextRectanglestarts withtypea brisefContains()overv ew of. Thist e method allows you to determine

C# language and then quickly moves to key technical and

if a given Point or Rectangle is within the current bounds of another Rectangle object. Later in this chapter,

architectural issues for .NET developers.

you see how to make use of this method to reform hit testing of GDI+ images. Until then, here is a simple example:

Table of Contents

public static int Main(string[] args)

C# and the .NET Platform, Second Edition

{

Introduction

...

Part One - Introducing C# and the .NET Platform

// Point is initially outside of rectangle's bounds.

Chapter 1 - The Philosophy of .NET

Rectangle r1 = new Rectangle(0, 0, 100, 100);

Chapter 2 - Building C# Applications

Point pt3 = new Point(101, 101);

Part Two - The C# Programming Language

if(r1.Contains(pt3))

Chapter 3 - C# Language Fundamentals

Chapter 4 - Object-Oriented Programming with C# else

Chapter 5 - Exceptions and Object Lifetime

Console.WriteLine("Point is not within the rect!");

Chapter 6 - Interfaces and Collections

// Now place point in rectangle's area.

Chapter 7 - Callback Interfaces, Delegates, and Events

pt3.X = 50;

Chapter 8 - Advanced C# Type Construction Techniques

pt3.Y = 30;

Part Threeif(r1- Programming.Containswith(pt3)).NET Assemblies

Chapter 9 - UnderstandingConsole.WriteLine("Point.NET Ass mblies is within the rect!");

Chapter else10 - Processes, AppDomains, Contexts, and Threads

Chapter 11 - TypeConsoleRef ction,.WriteLine("PointLate Binding, and Attributeis not-BasedwithinProgrammingthe rect!");

Part Fourreturn- Leveraging0; the .NET Libraries

}

Chapter 12 - Object Serialization and the .NET Remoting Layer Chapter 13 - Building a Better Window (Introducing Windows Forms) Chapter 14 - A Better Painting Framework (GDI+)

ChaptSize(F)r 15 - andProgrammingRegionwith WindowsTypesForms Controls

Chapter 16 - The System.IO Namespace

The Size and SizeF types are quite simple to manipulate, and require little comment. These types each

Chapter 17 - Data Access with ADO.NET

define Height and Width properties and a handful of overloaded operators (Table 14-6).

Part Five - Web Applications and XML Web Services

Chapter 18 - ASP.NET Web Pages and Web Controls

Table 14-6: Key Members of the Size(F) Types

Chapter 19 - ASP.NET Web Applications

Chapter 20 - XML Web Services

Size and SizeF

Index

Member

List of Figures

List+of Tables

-

= =

!=

Height

Width

Meaning in Life

Operators to manipulate Size types.

These properties are used to manipulate the current dimension of a Size type.

The Region Class

Finally we have the Region class. This type represents the interior of a geometric shape. Given this last statement, it should make sense that the constructors of the Region class require you to send an instance of some existing geometric pattern. For example, assume you have created a (100 ×100) pixel rectangle. If you wish to gain access to the rectangle's interior region, you could write the following:

// Get the interior of this rectangle.

C# and the .NET Platform, Second Edition

Rectangle r = new Rectangle(0, 0, 100, 100); ISBN:1590590554 by Andrew Troelsen

Region rgn = new Region(r);

Apress © 2003 (1200 pages)

This comprehensive text starts with a brief overview of the C# language and then quickly moves to key technical and architectural issues for .NET developers.

Once you do have the interior dimensions of a given shape, you may manipulate it using the core members shown in Table 14-7.

Table of Contents

C#Tableand the14-7:.NETMembersPlatfo m,ofSecondthe RegionEditionClass

Introduction

Meaning in Life

Region

Part One - Introducing C# and the .NET Platform

Member

Chapter 1 - The Philosophy of .NET

ChapterComplement()2 - Building C#UpdatesApplicationsthis Region to the portion of the specified graphics object that does

Part Two - The C# Programmingnot intersectLanguagewith this Region.

 

 

 

 

 

 

 

Chapter 3

- C# Language Fundamentals

 

 

Exclude()

 

Updates this Region to the portion of its interior that does not intersect with

 

 

Chapter 4

- Object-Oriented Programming with C#

 

 

 

 

 

the specified graphics object.

 

 

Chapter 5

- Exceptions

 

and Object Lifetime

 

 

 

 

 

ChapterGetBounds()6 - Interfaces

 

Returnsand Collectionsa RectangleF that represents a rectangular region that bounds this

 

 

Chapter 7

- Callback

 

Interfaces,Region. Delegates, and Events

 

 

 

 

 

 

 

 

Chapter 8

- Advanced

 

C# Type Construction Techniques

 

 

Intersect()

 

Overloaded. Updates this Region to the intersection of itself with the specified

 

Part Three - Programming

with .NET Assemblies

 

 

 

 

 

graphics object.

 

 

Chapter 9

- Understanding .NET Assemblies

 

 

 

 

ChapterIsEmpty()10 - Processes,TestsAppDomains,whetherContexts,this RegionandhasThreadsan empty interior on the specified drawing

 

 

Chapter 11

 

 

surface (or sets the current Region empty).

 

 

- Type Reflection, Late Binding, and Attribute-Based Programming

 

 

MakeEmpty()

 

 

 

 

 

 

 

 

Part Four - Leveraging

 

the .NET Libraries

 

 

IsInfinite()

 

Tests whether this Region has an infinite interior on the specified drawing

 

Chapter 12

- Object

 

Serialization and the .NET Remoting Layer

 

 

ChapterMakeInfinite()13 - Building a

surface (or sets the current Region infinite).

 

 

Better Window (Introducing Windows Forms)

 

 

 

 

 

 

 

 

Chapter 14

- A Better

 

Painting Framework (GDI+)

 

 

Transform()

 

Transforms this Region by the specified Matrix.

 

 

Chapter 15

- Programming with Windows Forms Controls

 

 

 

 

Translate()

 

Offsets the coordinates of this Region by the specified amount.

 

 

Chapter 16

- The System.IO Namespace

 

 

 

 

 

 

 

 

Chapter 17

- Data Access with ADO.NET

 

 

Union()

 

 

Updates this Region to the union of itself and the specified graphics object.

 

Part Five - Web Applications and XML Web Services

 

 

 

Xor()

 

 

Updates this Region to the union minus the intersection of itself with the

 

Chapter 18

- ASP.NET Web Pages and Web Controls

 

 

 

 

 

specified graphics object.

 

 

Chapter 19

- ASP.NET

Web Applications

 

 

Chapter 20 - XML Web Services

I'm sure you get the general idea behind these coordinate primitives. You will have a chance to work with

Index

each of them during the course of this chapter (and any time you program against GDI+). Now then, on to

List of Figures

some more interesting material!

List of Tables

SOURCE

The UtilTypes project is included under the Chapter 14 subdirectory.

CODE

Соседние файлы в предмете Программирование