2D Graphics with GDI+

basiliskcanoeΛογισμικό & κατασκευή λογ/κού

2 Νοε 2013 (πριν από 4 χρόνια και 6 μέρες)

95 εμφανίσεις

Software Engineering

1

The .NET Framework and the CLI


2D Graphics with GDI+



Visual Studio.NET 2008

Software Engineering

2

The .NET Platform


Based on the Common Language
Infrastructure (CLI)


ECMA (European Computer Manufacturers’
Association) and ISO standard


CLI exists only for Windows but is being
developed for other platforms (Linux, Mac …)


Contains a collection of classes called
Framework Class Library (FCL)


FCL is shared by all .NET languages


C++, C#, J#, COBOL, Fortran, Eiffel, …

Software Engineering

3

Common Language Runtime (CLR)


Executes
managed code


Compilation is performed in two phases:

1.
Program is compiled into the Microsoft
Common Intermediate Language (CIL)


CIL is composed of instructions for the CLR

2.
CLR links FCL function calls and compiles
the program to machine code


.NET Web Services are applications that
expose (make available) functionality to
clients through the Internet

Software Engineering

4

Common Language Infrastructure (CLI)

Software Engineering

5

C++/CLI


When we program in C++ using the .NET
framework, we program in an
extension

of
C++ called
C++/CLI


C++/CLI is a binding between the ISO
standard C++ language and CLI


C++/CLI retains all the features of C++


C++/CLI can use the .NET FCL


C++/CLI can use all CTS types

Software Engineering

6

C++ and the CTS types


Classes can be defined in C++/CLI in four ways:



class
MyClass

{ … };



//native C++ class


ref class
MyClass

{ … };



//abstract base class


value class
MyClass

{ … };


//concrete class


interface class
MyClass

{ … };


//interface class



A new
safe pointer

type called
handle

is introduced



MyClass

^
myObject

=
gcnew

MyClass
( );

// ^ is for handle


myObject
-
>
myMethod
( “Hello” );


// old
-
> syntax


When
myObject

goes out of scope its memory is
automagically

released


no need to worry about destructors and such.

Software Engineering

7

C++/CLI Handles


In C++/CLI a handle is created using the

gcnew

keyword


With native pointers
new

is still used


gc

stands for “garbage collected”


Literal strings like
”Hello”

are resolved to the

System::String ^

type (ISO C++ would resolve to
const char*
)

Software Engineering

8

2D Graphics in .NET


FCL contains a 2D graphics API called
Graphical Device Interface Plus
or
GDI+


GDI+ is part of the
System::Drawing

namespace and some other namespaces


Such as
System::Drawing::Drawing2D


and
System::Drawing::Drawing3D

and more..


There is a newer graphics API called
Direct2D


but it is criticized as to complex and having other issues


Software Engineering

9

Graphics Coordinate System

x
-
axis

( 0, 0 )

y
-
axis

(x, y)

Coordinates are measured in pixels

Software Engineering

10

Graphics Context and Objects


All Windows Form objects inherit a

virtual void Paint( PaintEventArgs * paintEvent )



event handler that contains all drawing code


In the Paint event the
Graphics
,
Pen
, and
Brush

objects (and others) are extracted to perform
drawing


Text is drawn by
DrawString( )

calls

Software Engineering

11

2
-
D Drawing Functions


DrawLine( Pen *p, int x1, int x2, int y1, int y2 )


DrawRectangle( Pen * p, int x, int y, int w, int h )


FillRectangle( Brush *b, int x, int y, int w, int h )


DrawEllipse( Pen *p, int x, int y, int w, int h )


FillEllipse( Brush *b, int x, int y, int w, int h )


DrawArc( Pen *p, int x, int y, int w, int h, int, stA, int swA )


DrawPie( Pen *p, int x, int y, int w, int h, int, stA, int swA )


FillPie( Brush *b, int x, int y, int w, int h, int, stA, int swA )


DrawLines( Pen *p, Point [ ] )


DrawPolygon( Pen *p, Point [ ] )


FillPolygon( Brush *b, Point [ ] )

Software Engineering

12

Mouse Event Handling


GUI controls are derived from the class


System::Windows::Forms::Control


All can handle mouse and keyboard events


Mouse event information is passed by a
MouseEventArgs

object


The Mouse Events are:


MouseEnter, MouseLeave, MouseDown,
MouseHover, MouseMove, MouseUp


The MouseEventArgs Properties are:


Button, Clicks, x, y

Software Engineering

13

Keyboard Event Handling


Keyboard event information is passed by a


KeyEventArgs

object


The Keyboard Events are:


KeyUp, KeyDown, KeyPress


The KeyEventArgs Properties are:


KeyChar, Handled, Alt, Control, Shift, KeyCode,
KeyData, KeyValue, Modifiers

Software Engineering

14

The
ArrayList

Container


.NET equivalent of a dynamic array (vector)


Has Capacity and Count properties


Main methods:



Add


Clear


Contains


IndexOf


Insert


array<type>

still can be used


Remove


RemoveAt


RemoveRange


Sort


TrimToSize

Software Engineering

15

Explicit Type Conversions in .NET


Standard C++ supports two explicit cast
operations:


static_cast < new_type > ( old_type )


dynamic_cast < new_type > ( old_type )


const_cast < new_type > ( old_type )


reinterpret_cast < new_type > ( old_type )


The operation
static_cast

performs conversion at
compile time


The operation
dynamic_cast

converts at run
-
time


.NET
boxing

converts a value type to a managed
type using the
__box( )

operation (byte by byte)



Software Engineering

16

Spatial Visual App