Microsoft Visual C++ .NET Professional Projects - Premier Press
.pdf
332 |
Project 2 |
CREATING AN APPLICATION USING MANAGED C++ |
|||
|
|
|
|
|
|
|
|
Table 11-4 (continued) |
|
||
|
|
|
|
|
|
|
|
Property Name |
Description |
||
|
|
|
|
|
|
|
|
FillPolygon |
Used to fill a polygon. This method can be overloaded. |
||
|
|
FillRectangle |
Used to fill a rectangle. This method can be overloaded. |
||
|
|
FillRectangles |
Used to fill a series of rectangles. This method can be over- |
||
|
|
|
|
|
loaded. |
|
|
FillRegion |
Used to fill a Region object with the specified color. |
||
|
|
Flush |
|
Enforces the execution of pending graphic operations and |
|
|
|
|
|
|
returns immediately without waiting for the operations to |
|
|
|
|
|
complete. This method can be overloaded. |
|
|
FromHdc |
Used to create a new Graphics object from the specified han- |
||
|
|
|
|
|
dle. This method can be overloaded. |
|
|
GetHdc |
Used to get the handle to the device context of the Graphics |
||
|
|
|
|
|
object. |
|
|
IsVisible |
Used to indicate whether the pair of coordinates is contained |
||
|
|
|
|
|
within the visible clip region of the Graphics object. This |
|
|
|
|
|
method can be overloaded. |
|
|
MeasureString |
Used to measure a string. This method can be overloaded. |
||
|
|
MultiplyTransform |
Used to multiply the world transformation of the Graphics |
||
|
|
|
|
|
object and the Matrix object. |
|
|
ReleaseHdc |
Used to release a device context handle. |
||
|
|
ResetTransform |
Used to reset the world transformation matrix of the |
||
|
|
|
|
|
Graphics object. |
|
|
Restore |
Used to restore the state of the Graphics object as the state |
||
|
|
|
|
|
of the GraphicsState object. |
|
|
RotateTransform |
Used to apply rotation to the transformation matrix of the |
||
|
|
|
|
|
Graphics object. This method can be overloaded. |
|
|
Save |
|
Used to save the current state of the Graphics object. |
|
|
|
ScaleTransform |
Applies scaling operation to the transformation matrix of the |
||
|
|
|
|
|
Graphics object. This method can be overloaded. |
|
|
SetClip |
Used to set the clipping region of the Graphics object. This |
||
|
|
|
|
|
method can be overloaded. |
334 Project 2 CREATING AN APPLICATION USING MANAGED C++
Table 11-5 (continued)
Method Name |
Description |
InitializeLifetimeService |
Inherited from the MarshalByRef object and is used to |
|
obtain a lifetime service object. |
ToString |
Used to return a String. |
Finalize |
This is a protected method of the Brush class and is over- |
|
ridden. |
MemberwiseClone |
Creates a shallow copy for the current Object. |
|
|
Next, I discuss the Pen class.
The Pen Class
The Pen class also belongs to the System.Drawing namespace. This class is used to define an object that will be used to draw lines and curves. The Pen class cannot be inherited.
A Pen object can then be used to draw lines of a specified style. You can then use a variety of fill styles to fill the lines. However, the fill style will depend on the brush that is used for filling the line.
The properties of the Pen class are listed in Table 11-6.
Table 11-6 Properties of the Pen Class
Property Name |
Description |
Alignment |
Used to get or set the alignment for the Pen object. |
Brush |
Used to get or set the Brush object. |
Color |
Used to get or set the color of the Pen object. |
CompoundArray |
Used to get or set the array values for the specified pen. |
DashCap |
Used to get or set the cap style to be used at the end of the |
|
dashes when drawing dashed lines. |
DashOffSet |
Used to get or set the distance to the start of a dash |
|
pattern. |
DashPattern |
Used to get or set custom dashes. |
336 Project 2 CREATING AN APPLICATION USING MANAGED C++
Table 11-7 (continued)
Method Name Description
TranslateTransform |
Used to translate the geometric transformation. |
Finalize |
A protected method of the Pen class; can be overridden. |
MemberwiseClone |
Used to create a shallow copy for the current instance of |
|
the Object. |
|
|
I have discussed GDI+, including its services, features, and programming model changes. I also discussed the System.Drawing namespace and the various classes that constitute this namespace. In the next section, I discuss events and delegates in managed extension code.
Events and Delegates
Events form the foundation of an object-oriented programming language. An event is a message generated by an object that defines the occurrence of an action. Events can be system generated or user generated. The object that generates an event is known as the event sender and the object that receives events is known as the event receiver. While communicating with events, the event sender class is not aware of the object or method that will receive the event. It uses a pointer-like mechanism that acts as an interface between the event source and the event receiver. A delegate provides this pointer-like mechanism.
A delegate is a class that is responsible for holding references to methods. A delegate is a class with its own signature. A delegate can hold only those methods that match with the signature of the delegate. Therefore, a delegate is similar to a callback or a type-safe function pointer. I will discuss the event-handling functionality of delegates later in the chapter. The next section takes you through an introduction to events.
Introducing Events
Events are of two types: predefined system events used for handling events related to controls and user-defined events. These predefined events are used for handling events when the user performs an action on the controls. Typically, a user interface consists of screens that contain one or more controls associated with it. Users
338 Project 2 CREATING AN APPLICATION USING MANAGED C++
The EventHandler delegate represents the method to be used to handle events that have no data. The syntax for the EventHandler delegate is as follows:
public_gc_delegate void EventHandler ( Object* sender, EventArgs* e);
NOTE
The event handler declaration must match the declaration of the EventHandler delegate.
In the preceding syntax, sender is the name of the source for the event and e refers to an EventArgs that consists of the event data.
By creating an EventHandler delegate, you identify the method used to handle the event.
If the event does not generate data, you need to use the EventArgs class for the event data object and the EventHandler as the event delegate. By creating an EventHandler delegate, you identify the method that will be handling the event. You need to attach events with event handlers. To do so, you need to add an instance of the delegate to the event. The event handler will be called when the event occurs.
I will now discuss the EventArgs class.
The EventArgs Class
The EventArgs class belongs to the System namespace. This class acts as the base class for all classes that contain event data. However, this class does not contain any event data. It can be used by events that do not pass any information to their event handlers. If the event handler requires information, the application should derive a class from the EventArgs class to store the data.
The methods of the EventArgs class are listed in Table 11-8.
INTRODUCTION TO GDI+, EVENTS, AND DELEGATES |
Chapter 11 |
|
339 |
||||
|
|||||||
|
Table 11-8 Methods of the EventArgs Class |
|
|
|
|
|
|
|
|
|
|
|
|
||
|
|
|
|
|
|
|
|
|
Method |
Description |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
EventArgs |
This is the EventArgs constructor and is used to initialize an |
|
|
|||
|
|
instance of the EventArgs class. |
|
|
|
|
|
|
Empty |
This is the public field for the EventArgs class. The field |
|
|
|||
|
|
represents an event that has no event data. |
|
|
|
|
|
|
Equals |
This is the public method for the EventArgs class. This |
|
|
|||
|
|
method is used to determine whether or not two Object |
|
|
|||
|
|
instances are equal. The method can also be overloaded. |
|
|
|||
|
GetHashCode |
This method is used to serve as a hash function for hashing |
|
|
|||
|
|
algorithms. |
|
|
|
|
|
|
GetType |
This method is used to get the Type for the current instance. |
|
|
|||
|
ToString |
This method is used to return a string that represents the cur- |
|
|
|||
|
|
rent Object. |
|
|
|
|
|
|
Finalize |
This is the protected method of the EventArgs class. |
|
|
|||
|
|
|
|
|
|
|
|
Introducing Delegates
As stated earlier, a delegate is a class that is used to store references to methods. A delegate has a signature. The next section discusses the important classes for delegates.
The Delegate Class
This class belongs to the System namespace and is used as a base class for all other delegate types. However, only the system and compilers are permitted to derive a new type from this class.
NOTE
This class should not be considered as a delegate type. Instead, you can derive delegate types from this class.
