CS 325 - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

CS 325

Description:

It would be nice to have a brain so we can play the game ... Often want to pop up a little window to let the user know something. Information. Question ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 26
Provided by: dyes
Category:
Tags: brain | pop

less

Transcript and Presenter's Notes

Title: CS 325


1
CS 325
  • Unix Programming Environment
  • and
  • Windows Programming
  • with Microsoft Foundation Classes (MFC)

2
Lecture 21
  • Today
  • TicTacToe
  • Face vs Brain (requires state)
  • Reading
  • Visual C manuals are online
  • http//msdn.microsoft.com/library
  • The MFC book Read the whole thing. Only about
    100 pages. Great tips and examples to avoid
    pitfalls. And some stuff you may not see in
    class.
  • Assignments
  • Get familiar with Visual C
  • You will be using it for your remaining projects

3
Projects 5 6 - 7
  • P5 WinOthello Due in 1-2 weeks.
  • P6 WinOthelloGold Due in 3-4 weeks.
  • P7 WinFinalProject Due by last day of class.

4
Sample TicTacToe program
  • Want to be able to write text on the screen
  • Already seen this (Menu example)
  • New capabilities
  • Want to be able to draw lines on the screen
  • Want to be able to draw shapes on the screen
  • Example
  • Extremely simplistic version of tic-tac-toe
  • Menu with two options (new and quit)
  • Draw a simple board, click to indicate moves
  • Use red and blue blocks instead of Xs and Os

5
Class Exercises
  • On the web you will find four files
  • TTTWin.h
  • TTTWin.cpp
  • TTTmsgs.h
  • TTTMenu.rc
  • Download these files and run the program
  • Modify the program so that it centers the square
    in the appropriate space (instead of just putting
    the square centered around where you clicked)
  • Modify the program so the square goes to the edge
    of the lines

6
Problem with TicTacToe
  • We arent really playing TicTacToe
  • It would be nice to have a brain so we can play
    the game
  • We need to be able to separate the brain from the
    face so that we dont get the details of the Tic
    Tac Toe game mixed up with the GUI of the Tic Tac
    Toe game

7
Brain vs. Face
  • Brain Basic rules of TicTacToe
  • Something to represent the game (2-d array)
  • Making a valid play on that game
  • Determine if the game is over
  • Face Interface between the end-user and the
    machine
  • Something to use the brain in order to decide
    what to display

8
Brain Vs. Face in Code
  • Brain
  • TTTBrain.h (TicTacToe class declaration)
  • TTTBrain.cpp (TicTacToe class definition
  • Face
  • menus.h (event identifiers)
  • menus.rc (menu description
  • TTTWin.h (CFrameWnds child declaration)
  • TTTWin.cpp(CFrameWnds child definition,
    CFrameWnds child Message Map, CWinApps child
    declaration, definition, and instantiation)
  • This is also where we instantiate and use the
    TicTacToe class to play the game

9
Class Exercise
  • Add these files to your project
  • (TTTBrain.h)
  • (TTTBrain.cpp)
  • Make your program play TicTacToe correctly
  • Integrate brain with the face
  • Add a method "refresh" which repaints the game in
    the current state. Add refresh to the menu
  • OnPaint()
  • Same as refresh but instead of CClientDC use
    CPaintDC and you never call OnPaint().

10
Console vs Event-driven
  • Console
  • Main program
  • Follow sequence/selection/iteration/function
    commands in order as directed
  • Program has more control over the user
  • Event-driven
  • No typical main program
  • We decide on events to handle
  • User has more control via choosing events

11
Event-driven monitoring state
  • sketchy pseudocode for searching for event
  • while(1)
  • if(msgmessagefound())
  • if(application_handles_message(msg))
  • sendmessage(msg) to appropriate application
    and to appropriate class (see MessageMap)

12
Events (messages)
  • Each message has a unique id associated with it
  • Windows-defined
  • IDs 0 1023
  • You-defined
  • Can name between 1024 65535
  • Naming Convention
  • IDs
  • WM_MESSAGE Windows Message
  • IDM_MESSAGE Menu Id
  • Handlers (methods)
  • OnMessage

13
Event/Message Handlers
  • Each unique message ID needs to be associated
    with message handler (method)
  • MFC-defined
  • Has already been associated
  • Use pre-defined macro ON_WM_MESSAGE()in message
    to signify it is to be handled in the class
    specified in the BEGIN_MESSAGE_MAP params
  • You-defined
  • You must make the association
  • Use macro ON_COMMAND(ID, OnMessage)

14
Macros / Message Map
  • The macros we use have already been defined.
    Before compilation time, there is a text
    replacement where we use the macro identifier.
  • We have been using the Message Map macros(where
    we associate a message ID to its handler)
  • BEGIN_MESSAGE_MAP(owner-class name, base-class
    name)
  • //mappings
  • ON_WM_MESSAGE() // for pre-defined
  • ON_COMMAND(ID, OnMessage) //for you-defined
  • END_MESSAGE_MAP()
  • For more than you want to know about message maps
  • http//msdn.microsoft.com/library/default.asp?URL
    /library/devprods/vs6/visualc/vcmfc/_mfcnotes_tn00
    6.htm

15
Message Boxes
  • Often want to pop up a little window to let the
    user know something
  • Information
  • Question
  • Notification
  • MessageBox class simplifies this process
  • MessageBox has a title
  • MessageBox displays information
  • MessageBox might have
  • Icons and/or selections for the user to pick

16
MessageBox example
  • Four basic types of message boxes exist
  • Our example will generate one of four message
    boxes depending on which mouse button you click
    and where you clicked it
  • Will see a more complete description of message
    boxes shortly
  • include ltafxwin.hgt
  • class CMessageWin public CFrameWnd
  • public
  • CMessageWin()
  • CMessageWin()
  • afx_msg void OnLButtonDown
  • (UINT uFlags, CPoint point)
  • afx_msg void OnRButtonDown
  • (UINT uFlags, CPoint point)
  • private
  • DECLARE_MESSAGE_MAP( )

17
Body of the program (1)
  • include ltafxwin.hgt
  • include MessageWin.h"
  • CMessageWinCMessageWin()
  • Create(NULL, "MessageBox Example", WS_OVERLAPPED
    WINDOW, CRect(100,100,500,500))
  • CMessageWinCMessageWin()
  • Standard include files
  • Constructor
  • Destructor

18
Body of the program (2)
  • afx_msg void CMessageWinOnLButtonDown (UINT
    uFlags, CPoint point) // handle left button
    click
  • if (point.x lt 200)MessageBox("x coordinate is
    small", "Stop Message Box", MB_ICONSTOP)
  • elseMessageBox("x coordinate is
    large", "Question Message Box",
    MB_ICONQUESTION)
  • afx_msg void CMessageWinOnRButtonDown (UINT
    uFlags, CPoint point) // handle right button
    click
  • if (point.y lt 200) MessageBox("y coordinate is
    small", "Exclamation Message Box",
    MB_ICONEXCLAMATION)
  • elseMessageBox("y coordinate is large", "Info
    Message Box", MB_ICONINFORMATION)

19
Body of the program (3)
  • BEGIN_MESSAGE_MAP (CMesssageWin, CFrameWnd)
  • ON_WM_LBUTTONDOWN( )
  • ON_WM_RBUTTONDOWN( )
  • END_MESSAGE_MAP( )
  • class CMessageApp public CWinApp
  • public
  • BOOL InitInstance() m_pMainWnd new
    CMessageWinm_pMainWnd-gtShowWindow (m_nCmdShow)
    m_pMainWnd-gtUpdateWindow()return TRUE
  • MessageApp
  • Our message map section indicates we are
    interested in capturing two types of events
  • Left button down
  • Right button down
  • Standard main portion

20
Class Exercises
  • Build our message program
  • MessageWin.h
  • MessageWin.cpp
  • Create a new VC project and insert these files
    into this project. Run the program. Make sure
    you can get all four types of message boxes
    displayed.

21
Messagebox with real info
  • Message boxes represent a simple way to provide
    information to the developer
  • Can display variable values
  • Can identify your current location in program
  • When using to develop/debug programs
  • Can use message boxes to indicate where you are
    in the program (Info, entering routine xxx)
  • Can display variable and values if you dont want
    to use debugger

22
Class Exercises
  • Another MessageBox example
  • Print coordinates of location you clicked on
  • Modify your code from the previous example
    (remember to add include ltstrstreamgt using
    namespace std)
  • if (point.x lt 200)
  • ostrstream s
  • s ltlt "" ltlt point.x ltlt "," ltlt point.y ltlt ""
  • MessageBox(s.str(), Coordinates",
    MB_ICONQUESTION)
  • //notice the difference from the way we used
    ostrstream last time
  • What is wrong with the output?

23
Problem with previous example
  • The string you generate (in carText) does not
    have a NULL (\0) character at the end of it.
    The MessageBox routine assumes that the string
    given is NULL-terminated.
  • Add a terminating NULL character to the end of
    the string
  • if (point.x lt 200)
  • ostrstream s
  • s ltlt "" ltlt point.x ltlt "," ltlt point.y ltlt " ltlt
    \0
  • MessageBox(carText, Coordinates",
    MB_ICONQUESTION)

24
One more comment
  • Lots of different versions of the MessageBox
    construct exist
  • Can prompt for Abort/Retry/Ignore (or other
    choices), and also define the default operation
  • Can identify what option the user selected
  • Dont need an icon on the MessageBox
  • For complete details, check out
  • In the help files under the Search tab, look for
    MessageBox, may also want to follow link to
    AfxMessageBox for more details on return types
    what is the difference between AfxMessageBox and
    MessageBox?

25
Class Exercises
  • Read the help files for MessageBox and
    AfxMessageBox
  • Allow your program to display four different
    MessageBox icons
  • Using the manual page as a guideline, generate a
    message box that includes a push button (yes,
    no, cancel, ok, etc.) Make sure your program can
    detect which button the user selected.
  • Hint You dont need to add any handlers just
    look at possible return values
Write a Comment
User Comments (0)
About PowerShow.com