.NET Tip



COM Interoperability...

COM components and .NET components are not compatible since they have different internal architectures. However, there are tools available in the .NET SDK for generating COM proxies for .NET components and .NET proxies for COM components. Using these proxies we can use COM components in .NET projects and vice versa. 

For using COM components in .NET application we can create the wrapper class either by using the 'TlbImp.exe' tool provided in the .NET SDK or by adding the COM reference through the Solution Explorer.

To use a .NET component in a non-.NET project we have to first register the component using the RegAsm tool as well as create a corresponding COM type library file ('.tlb' file). To create a COM type library file we need to use the TlbExp tool. 

Top


Device Drivers Tip



Page Out Operation...

Both virtual and physical memory is divided into units called pages. On the Intel platform a page is of 4 KB. Under low memory conditions the OS writes a page to the disk. This operation is known as page out operation. When the page is latter accessed a fault occurs as a result of which fault handler code gets executed. The Fault handler code reads the page from the disk and recreates a physical page in the memory (page-in operation).

Pages can either be marked as pageable or non - pageable. As the name suggests pageable memory can be paged out (written) to the disk under low memory conditions. On the other hand non-pageable memory is never paged out (written) to the disk even under low memory conditions. Some critical routines in the OS and device driver cannot afford to have a memory fault to occur and expect that the data they access be always present in the physical memory in a non-paged area.

Top


VC++ Tip



What is Message Reflection?

Ans: Windows controls frequently send notification messages to their parent windows. For instance, many controls send a control color notification message (WM_CTLCOLOR) to their parent to allow the parent to supply a brush for painting the background of the control. The parent window, often a dialog box, is responsible for handling these messages. This means that the code for handling the message needs to be in the parent window's class and that it has to be duplicated in every class that needs to handle that message. In such case, every dialog box that wanted controls with custom backgrounds would have to handle the control color notification message. It would be much easier to reuse code if a class for a control could be written that would handle its own background color. MFC facilitates reuse by providing a feature called "message reflection" that allows these notification messages to be handled in either the control window or the parent window, or in both. In the example, where background of a control has to be colored, you can now write a control class that sets its own background color by handling the reflected WM_CTLCOLOR message-all without relying on the parent.

If you have supplied a handler for a specific message in your parent window's class, it will override reflected message handlers for the same message, provided you don't call the base class handler function in the handler that you have supplied. For example, if you handle WM_CTLCOLOR in your dialog box class, your handling will override any reflected message handlers.

If, in your parent window class, you supply a handler for a specific WM_NOTIFY message, your handler will be called only if the child control sending those messages does not have a reflected message handler called through ON_NOTIFY_REFLECT( )

The message-map macro for reflected messages is slightly different than the one used for regular notifications. It has _REFLECT appended to its usual name. For instance, to handle a WM_NOTIFY message in the parent, you use the macro ON_NOTIFY in the message map of parent window's class. To handle the reflected message in the child control, use ON_NOTIFY_REFLECT macro in the child control's message map.

Top


C++ Tip



Can a friend function be used for overloading the assignment operator? 

Ans:  No! A friend function cannot be used for overloading assignment operator. If this were made possible, then a friend function would try to assign an object of one class containing totally different data members to an object of some other class, which too contains different members. This is not expected from an assignment operator. hence, a friend function cannot be used to overload assignment operator.

Top


C Tip



Interrupts...

A PC must handle certain events like pressing of a key, very quickly irrespective of what it is doing at the moment the key was pressed. Hence it generates a special condition, an interrupt, each time such an event occurs. Thus, an interrupt is a signal to the microprocessor that its immediate attention is needed. The moment interrupt draws the microprocessor's attention, the microprocessor's normal work is interrupted and hence the name interrupt. In other words an interrupt is the signal which when generated asks the microprocessor to stop whatever it is doing at the moment (when the signal is issued) and execute some other task.

The signal may be generated through either software or hardware. For example, the int86( ) library function in C generates a software interrupt, whereas on hitting a key from the keyboard, a hardware interrupt is generated.

Top


Article – C# - Custom Controls - II



Continued from last week...

In the last article we had added the Tick event handler for the mytimer control. Let us see what we have done in this handler. Consider the following code of the mytimer_Tick( ) handler.

g.TranslateTransform ( 60, 60 ) ;
for ( int i = 0 ; i<=12; i++ )
{

g.FillRectangle ( b, -2, -45, 4, 5 ) ;
g.RotateTransform ( 30 ) ;

}
g.ResetTransform( ) ;

Here we have drawn the rectangles that represent the hours. We have used the TranslateTransform( ) method of the Graphics class to shift the origin to (60, 60). Using the FillRectangle( ) method we drew 12 rectangles and rotated the graphics world every time by 30°. After drawing the rectangles we used the ResetTransform( ) method to reset the transform world to default settings.

Next we have drawn the seconds, minutes and hour hands. To display the current time we need to calculate the angles of the hands first. For every second, the second hand should be rotated by 6° (360 / 60). For every minute, the minute hand should be rotated by 6° (360 / 60) again and for every hour the hour hand should be rotated by 30° (360 / 12). Moreover, the hour hand should also be rotated by 0.5° for every minute. This is because in every 60 minutes the hour hand rotates by 30°, hence in every minute it would rotate by 0.5°. So while displaying the current time we need to multiply the seconds, minutes and hours values by the angles and rotate the hands by the multiplication results. These calculations need to be done before starting the timer and hence we have written this logic in a method called start( ). We would see this method later. 

To draw the hands we have used appropriate Color, Width and EndCap style of the Pen object. Next we have translated the graphics world by 60, 60 and rotated it by s°. Then we have drawn the hand using the DrawLine( ) method at appropriate co-ordinates. We have again used the ResetTransform( ) method to reset the transform.

For drawing the minute hand we have used the same logic but rotated the hand by m° and made the hand shorter, thicker and of blue color. We have also changed the EndCap style to LineCap.Triangle. After resetting the transform we have drawn the hour hand rotated at an angle of h° and made it more short and thick than the minute hand. We have used the same EndCap style as of the minute hand for the hour hand. 

With every second we have incremented s by 6°. As soon as the angle becomes 360°, we have reset it to 0°. With every second we have also incremented the value of sec by 1. As soon as sec becomes 60 (i.e. after 1 minute) we have done three things-reset sec to 0, incremented angle m by 6° and incremented the value of min by 1. As soon as the angle m becomes 360°, we have reset it to 0°.

With every minute we have incremented the value of angle h by 0.5°. Similarly, after completion of 360°, we have reset the angle to 0°. After min becomes 60 (i.e. after 1 hour) we have reset the value of min to 0 and incremented the value of hr.

All these would get drawn on the bitmap because the graphics context is of the bitmap. Now we need to display the bitmap on the control. To do so we created another bitmap object bm using the Clone( ) method. We created bm with the same size as that of the control and having the same PixelFormat as of mybmp. PixelFormat represents number of bits associated with one pixel of data. Next we have displayed the bitmap on the control by setting the BackGroundImage property of the control to bm. Lastly, we have cleared mybmp with the BackColor of the control making it ready for painting. Let us now see the start( ) method that was mentioned above.

public void start( )
{

now = DateTime.Now ;
sec = now.Second ;
min = now.Minute ;
hr = now.Hour ;
s = sec * 6 ;
m = min * 6 ;
h = ( ( hr % 12f ) * 30f ) + ( min * 0.5f ) ;
mytimer.Start( ) ;

}

The start( ) method would be called by the client when he wants to start the clock. We must start the clock and place the second, minute and hour hands according to the present time. To do this we have first collected the current system time in now of type DateTime. We have added now as a data member of the class. To collect the seconds, minutes and hours values we have also added 3 integers named sec, min and hr as data members of the Clock class. According to the current time we have displayed the hands at different angles. We have used two integer variables s and m to specify the angles for the second and minute hands respectively and we have used a single variable h to specify the angle for the hour hand. We have also added s, m and h as data members of the Clock class.

% (modulus operator) is used to avoid the AM/PM conflict. As soon as we start the timer using the Start( ) method the Tick event is fired every second. And every second we have painted the control with the new bitmap and changed the angles. To stop the timer we have added a method called Halt( ) to the control shown below:

public void Halt( )
{

mytimer.Stop( ) ;

}

Using the Custom Control

To use the control we have created 'Windows Form' client called ClockClient. To be able to add the control to our form, we need to add it first to the Toolbox. For this we need to right click on the Toolbox and select Customize Toolbox. On doing so the Customize Toolbox window would get opened. Here we need to select the .NET Framework Components Tab.

Our control would not get displayed in the list of existing controls; we need to browse for its dll. After selecting the dll we need to click the OK button. This would add our control to the Toolbox as shown in the following figure.

To add the control to our form we need to drag it on the form. After adding it to the form, we have changed its name to myclock and set its Locked property to True to lock its size. To start the ticking, we have called the start( ) method of the control in the constructor of the form as shown below:

public Form1( )
{

InitializeComponent( ) ;
myclock.start( ) ;

}

This starts the timer and the control starts displaying the time.

Top


Joke



A new manager spends a week at his new office with the manager he is replacing. On the last day the departing manager tells him, "I have left three numbered envelopes in the desk drawer. Open an envelope if you encounter a crisis you can't solve."

Three months down the track there is a major drama, everything goes wrong - the usual stuff - and the manager feels very threatened by it all. He remembers the parting words of his predecessor and opens the first envelope. The message inside says "Blame your predecessor!" He does this and gets off the hook.

About half a year later, the company is experiencing a dip in sales, combined with serious product problems. The manager quickly opens the second envelope. The message read, "Reorganize!" This he does, and the company quickly rebounds.

Three months later, at his next crisis, he opens the third envelope. The message inside says "Prepare three envelopes".

Top


Different Strokes



Top