Home |
Obtaining a Pointer to PegPresentationManager Determining the Position of an Object Overriding the Message() Method Using PegTimer |
Using PegTimerFor this example we will create a derived PegDecoratedWindow class. In this derived window class we will override the Message() function to provide custom functionality. The custom operation is to start a periodic PegTimer and wait for timer expiration messages to arrive. The window will change colors each time the timer expires. class MyWindow : public PegDecoratedWindow { public: MyWindow(const PegRect &Rect); PEGINT Message(const PegMessage &Mesg); Private: PEGINT miColor; }; void PegAppInitialize(PegPresentationManager * pPresent) { PegRect WinRect; WinRect.Set(0, 0, 100, 100); MyWindow *pWin = new MyWindow(WinRect); pPresent->Center(pWin); pPresent->Add(pWin); } MyWindow::MyWindow(const PegRect &Rect): PegDecoratedWindow(Rect, FF_THIN) { Add(new PegTitle("A colorful Window!")); RemoveStatus(PSF_SIZEABLE); miColor = 0; } PEGINT MyWindow::Message(const PegMessage &Mesg) { switch(Mesg.wType) { case PM_SHOW: PegDecoratedWindow::Message(Mesg); SetTimer(1, ONE_SECOND * 2, ONE_SECOND / 2); break; case PM_TIMER: SetColor(PCI_NORMAL, miColor); Invalidate(); Draw(); miColor++; miColor &= 0x0f; break; case PM_HIDE: KillTimer(1); PegDecoratedWindow::Message(Mesg); break; default: return PegDecoratedWindow::Message(Mesg); } return 0; } In the above Message() function, the derived window class catches the system messages PM_SHOW and PM_HIDE, and the PM_TIMER messages generated by the timer. The PM_SHOW message is received when the window is first displayed. This is a convenient place to start the timer. In this case we set the timer to wait 2 seconds before the first timeout, and to expire every 500 ms (ONE_SECOND / 2) thereafter. This timer ID value is simply set to '1'. If you are using many timers, you will probably want to enumerate the timer ID values, but in this case we are only using one timer and so we simply hard-coded the timer ID value. Note that the PM_SHOW message handler also passes the message on down to the base PegDecoratedWindow class. It is important to pass system messages on down to the base class in case the base class is also catching the message. The PM_HIDE message is received when the window is removed. This is a convenient place to stop the timer. You must remember to kill timers that you have started before your windows are deleted, or PM_TIMER messages will be sent to invalid destinations and your software will most likely crash. Since a window is always removed before it is deleted, the PM_HIDE system message handler is an excellent place to kill any active timers. The PM_TIMER message handler is where we change the window color and re-display the window. A member variable has been defined named miColor, and we will use this variable to keep track of which color to display next. This example assumes we are running in 16-color mode, and therefore prevents the color index from passing 15 (0x0f). Note that after changing the window color by using the SetColor() function, we have to tell the window to re-draw. The window does not automatically redraw since you may make several changes to the window and you do not want each change to cause a window re-draw operation.
|