Microsoft Visual C++ .NET Professional Projects - Premier Press
.pdf
342 Project 2 CREATING AN APPLICATION USING MANAGED C++
Event Handling in Managed Code
In managed event handling, you need to set up an event source and an event receiver by using the event_source and the event_receiver attributes. You specify type=managed to inform that events are to be handled in a managed context.
To declare a method as an event, you need to use the _event keyword in an event source class. You need to declare the method but do not define it. The compiler automatically defines the method implicitly when the method is made as an event. Therefore, the compiler would result in a compile error if the method is defined. Another point to keep in mind while declaring events is that the return type of the event handler must match the return type of the delegate, and the return type of the delegate must comply with the Common Language Specification.
To work with event handlers, you need to define them. You define an event handler in an event receiver class. Event handlers are nothing but methods that are used to handle events.
Once you have defined event handlers, the next step is to attach these event handlers to events. In an event receiver class, you can use the _hook keyword to attach event handlers to events. To detach the event handler from events, you can use the _unhook keyword. There is no limit to the number of event handlers that you can attach to an event or the number of events that can be attached to an event handler.
To trigger an event, you need to call the data member or the method, which is declared as an event, in the data source class. The equivalent event handler that is attached to the events will be called.
NOTE
You can raise an event only from the class in which it is declared. Therefore, to raise an event, you need a member function that is accessible from outside the class.
The important keywords for event handling in managed code are described in the following sections.
344 Project 2 CREATING AN APPLICATION USING MANAGED C++
The default value for the Type parameter is native. However, if the class is managed, the default is changed from native to managed.
_hook
The _hook keyword is used to associate or attach an event handler with an event. The _hook keyword can be used in two forms. The syntax for the _hook keyword is as follows:
long_hook (&SourceClass::EventMethod, source, &ReceiverClass::Handlermethod [,
receiver = this]);
long_hook (interface, source);
The parameters in the preceding syntax are described here:
&SourceClass::EventMethod. This is a pointer to the event method, which is being attached to the event handler method.
interface. This is the interface name that is being attached to the receiver. This parameter is applicable only for COM event receivers in which the value for the layout_dependent is true.
source. This points to an instance of the event source.
&ReceiverClass::HandlerMethod. This is a pointer to the event handler method that is hooked to an event.
receiver. This points to an instance of the event receiver class.
The first form of the _hook keyword can be used for COM event handlers where
the layout_dependent parameter for event_receiver is false. The second form
can be used for a COM event receiver where the layout_dependent parameter is
true.
_unhook
The _unhook keyword is used to detach an event handler from an event. There are three forms for the _unhook keyword. The syntax for the _unhook keyword is as follows:
long_unhook (&SourceClass::EventMethod, source, &ReceiverClass::HandlerMethod [,
receiver = this]);
long_unhook (interface, source)
long_unhook (source)
INTRODUCTION TO GDI+, EVENTS, AND DELEGATES |
Chapter 11 |
|
345 |
|
|
||||
|
|
|
|
|
The first form of the _unhook keyword can be used in most of the situations. However, the second form can be used only for a COM event receiver, and the third form can be used to unhook all delegates.
In this section, I have discussed the important keywords used for event handling in managed code. In the next section, I discuss the Unified Event Model.
The Unified Event Model
Many times, it may be required by components to pass back information related to events to their clients. Many programming languages take care of this through function callbacks. Although C supports function callbacks, C++ lacks callbacks for object methods. One of the solutions to this problem is an event model provided by COM. However, this event model is difficult to maintain and implement.
The Unified Event Model proves to be a better solution in this regard. It allows events to be used in an effective manner such that the component is no longer dependent on its client, and also ensures the component’s reusability.
This model supports both single-threaded and multithreaded usage. However, it does not support simultaneous multithread access to the data.
You can derive classes from event source and receiver classes in this model. The model also supports extended sourcing and receiving in the derived class.
Summary
In this chapter, you learned about GDI+, its various classes, and the System.Drawing namespace. You also learned about events and delegates and handling events in managed code. You will learn to use these events, delegates, and classes in the forthcoming project.
This page intentionally left blank
Chapter 12
Creating a
Managed
Extensions
Application
Chapter 11 introduced you to the basic concepts of GDI+, events, and delegates. It is time now to implement those concepts.
Project Case Study
Red Sky IT Systems is a reputed training institute in New York that trains students and IT professionals in the latest information technologies. It is a 10-year-old institute and has many centers across New York. It has an experienced faculty. The institute has a number of divisions, such as the Front Office, the Sales and Marketing division, the Software Development division, and the Services division. Each division has a Division Head to monitor the proceedings of the division.
Red Sky IT Systems offers various programs and courses. Students can select from a range of courses and admission is based on an entrance test. The biggest unique selling point of Red Sky IT Systems is that students also get to develop real-life applications. The students are taught not just from books but also get a lot of exposure through real-life projects at the end of the course. The responsibility of developing these projects for the various courses lies with the Software Development division of the institute.
As part of the VC++.NET course, the institute requires a project to be built using Managed C++ code. The responsibility for developing the application is with the following team of the Software Development division:
Susan Ward, project manager
David Wong, developer
Mary Peterson, developer
The team has decided to create a dummy paint application by using Managed Extensions code and has named the application Easy Tool. Using the application, users can type, change font, and paint. The team also decided to add the functionality of a Calculator, which can be used to illustrate the concepts of delegates.
The team also decided on the following development life cycle (DLC) for the project:
CREATING A MANAGED EXTENSIONS APPLICATION Chapter 12 349
1.Requirements analysis phase
2.Design phase
3.Coding phase
4.Testing phase
5.Execution phase
The following sections discuss each of the stages of the DLC in detail.
The Requirements Analysis Phase
In this phase, the team decides on the requirements for the project. It is also one of the most crucial phases in a project development, as all the rest of the phases depend upon this phase. The requirements of the application as decided by the team are as follows:
The application should include text functionality, such as enabling users to change the font name and font size, apply bold or italics, and so on.
The application should allow for paint functionality using pens and brushes.
The application should allow for basic calculations, such as addition, subtraction, and multiplication.
The application should provide menus from which users may choose all the preceding options.
The Design Phase
This phase of the DLC is used for designing the screens for the application. The screens for the application are decided based on the requirements of the project in the requirements analysis phase.
Figure 12-1 shows a sketch of the screen for the Easy Tool application based on the requirements of the project.
The screen will contain a number of menu options, such as File, Edit, Pen, View, Draw, Window, and Help. Clicking on a menu item will display the corresponding drop-down list to the user.
The drop-down list for the File menu option is displayed in Figure 12-2.
