Exercises


Exercise 28.1: Viewing the OnMouseMove calls

Use a block like #ifdef BOGUS at the start and #endif at the end to temporarily comment out the CPopView::onMouseMove code inside the brackets and replace it with these three lines.

 CDC *pDC = GetDC();  pDC->TextOut(point.x, point.y, "X");  ReleaseDC(pDC); 

This way you'll see an X at each position where the mouse sends a an OnMouseMove call. Notice that if you move the mouse fast the X's are further apart. Also notice that if you resize the window after putting the X's in it they go away. Why is that?

The reason the three suggested lines look as they do is because you need to have a CDC object in order to call a 'graphics' function like TextOut . You can get a CDC from a CView using the GetDC function. The only requirement here is that when you get a CDC like this, you need to release it with a ReleaseDC call before leaving the block of code where you got the CDC .

Exercise 28.2: Using system-wide cursor resources

It turns out there are a few system-wide cursor resources you can use. The system-wide Windows cursors are called IDC_ARROW , IDC_CROSS , IDC_EXCLAMATION , and IDC_WAIT . You can get an HCURSOR for one of them by using the CWinApp::LoadStandardCursor method which takes one of these IDC_??? for a system-wide cursor name as argument. Change the Pop Framework code so that it uses the system-wide IDC_EXCLAMATION instead of our IDC_PIN .

Exercise 28.3: The grenade tool

In this problem, add a 'grenade' cursor tool which will remove all nearby critters when you click it. When you left-click with the grenade tool, you destroy all the critters whose center is within some fixed Real _grenadedistance of the cVector point corresponding to the cursor click. Or maybe it will be better to just destroy the first int _grenadekillpower count critters that you find within _grenadedistance , counting down from the closest ones. This way, if you have a really deep stack of critters, like the Game Huge selection, you can have fun using repeated grenade hits to blast your way down. Try and think of a game where the grenade tool would serve a useful purpose.

Exercise 28.4: A Rotate tool

It would be nice in the Dambuilder game to have a rotating tool, that is, a cursor tool so that if you left-click with it, it will rotate a selected wall critter a bit counterclockwise. See if you can design and implement this.

Exercise 28.5: Creating floating popup menus

A lot of programs popup a context menu when you right-click. You can do this as follows . (a) Use the Resource View to add a new menu with an ID like ID_TOOLPOPUP . (b) Type any old character, which will add a top-level menu selection with a popup under it. Go down into the popup and add 'Pin' and 'Hand' selections with, respectively, the ID values ID_VIEW_PINCUSOR and ID_VIEW_DRAGGERCURSOR . (c) Class Wizard will ask which class to associate the new menu with. Associate it with CPopView . (d) Use Class Wizard to add a WM_CONTEXTMENU handler to CPopView . Code the handler like this.

 void CPopView::OnContextMenu(CWnd* pWnd, CPoint point)  {      CMenu menu;      menu.LoadMenu(ID_TOOLPOPUP);      menu.GetSubMenu(0)->          TrackPopupMenu(TPM_LEFTALIGN  TPM_RIGHTBUTTON, point.x,              point.y, this);  } 

To make the popup really fast to use, make the easy keys 1 and 2 be the menu shortcuts on this little menu. That is, use these strings for our menu item names '&1 Pin' and '&2 Hand.'

Exercise 28.6: An accelerator key for autofocus

Open up about four windows and get an idea of what autofocus does. It would be a useful thing to have an accelerator key for Window Autofocus . Use the Ctrl+A combination for this. The idea is that we might want to usually leave autofocus on, and only hit Ctrl+A to turn it off when we want to move our mouse away from a view and up into the menu bar and we don't want to worry about having the view get autofocused away from the view we want to change.

Exercise 28.7: Using the right mouse button

Give CPopView a message handler for WM_RBUTTONDOWN and have it change the cursor type. Or, if (a) you've done a right button popup and don't want to lose it and (b) your mouse has a middle button, use the WM_MBUTTONDOWN.



Software Engineering and Computer Games
Software Engineering and Computer Games
ISBN: B00406LVDU
EAN: N/A
Year: 2002
Pages: 272

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net