CS 325 - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

CS 325

Description:

Advising for Summer and Fall pre-registration begins next week ... required two players can play Othello add more functionality as you wish ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 30
Provided by: dyes
Category:
Tags: othello

less

Transcript and Presenter's Notes

Title: CS 325


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

2
Announcements
  • Advising for Summer and Fall pre-registration
    begins next week
  • We are offering CS 325 (Software Development and
    Systems) and CS 357 (Data Structures) in both
    Summer and Fall
  •   
  • Enrollments for both 325 and 357 requires at
    least 10 students.  Otherwise these courses may
    be cancelled and there will be fewer available
    courses this summer.  The decision may be made
    after pre-registration, so students who wait
    until late registration wont be counted.

3
Lecture 25
  • Today
  • Dialogs DoDataExchange
  • Paint()
  • BitMaps

4
Projects 5 6 - 7
  • P5 WinOthello Due in Next Monday.
  • Loose assignment very basic functionality
    required two players can play Othello add
    more functionality as you wish
  • P6 WinOthelloGold Before Spring Break?
  • Ten improvements over the basic interface (not
    your basic interface over a conceptual basic
    interface P5 and P6 may be the same)
  • P7 WinFinalProject After Spring Break.
  • Show it to me during study week

5
Two Major Parameter Systems
  • X1, Y1, X2, Y2
  • (Left, Top, Right, Bottom)
  • X1, Y1, W, H
  • (Left, Top, Width, Height)
  • CRect(l, t, r, b) gt x1,y1,x2,y2
  • CRect(x,y, xW,yH)
  • CRect(x,y, x100,y100)
  • CRect(CPoint CPoint)
  • CRect(CPoint(x,y) CPoint(xW,yH))
  • CRect(CPoint(x,y) CPoint(x100,y100))
  • CRect(CPoint size)
  • CRect(const CRect)
  • CRect(LPCRECT) gt CRect(CRect)

6
3 CFrameWnd Methods
  • To find the size of the window
  • GetWindowRect(CRect)
  • CRect what
  • GetWindowRect(what)
  • //after this call, what will contain the
    coordinates for the entire Window, these
    coordinates will be in relation to the edge of
    the entire screen
  • To find the size of the Client window (the white
    space)
  • GetClientRect(CRect)
  • CRect what
  • GetClientRect(what) // what is same as above
  • To resize the window
  • SetWindowPos(pointer, new-left, new-top, width,
    height, flags)
  • SetWindowPos(NULL, 10, 10, 300, 200, 0)
  • SetWindowPos(CWND, x, y, cx, cy, FLAGS)

7
Dialogs
  • Three (at least) ways to create
  • Stand alone application
  • Refer to our 1st example
  • Embedded within application as temp object
  • Declared locally to a method
  • Refer to our class examples
  • Embedded within application as attribute of the
    frame
  • MapObjects
  • Always DoModal()
  • Can return a value
  • Communicating with the dialog via DoDataExchange
  • Called automatically when either of these
    happens
  • DoModal()
  • OnOK()
  • UpdateData(bool)

8
DoDataExchange
  • Purpose
  • Allows us to automatically obtain information
    from Dialog controls without having to write the
    code ourselves
  • Also allows us to validate data obtained from
    controls
  • When is it called?
  • The DoDataExchange function is automatically
    called by a function UpdateData(),
  • UpdateData() is called by
  • DoModal() to transfer information from your
    data members to dialog controls as you enter into
    dialog (overriden method)
  • OnOK() to transfer information as you go out of
    dialog (method we created that will cause our
    return of DoModal)
  • UpdateData() can also be called by you.
  • DoDataExchange should not be called directly.
  • However, if you want it to be called, you must
    override this method

9
DoDataExchange
  • Override the DoDataExchange function
  • Called base class function
  • Map controls(Text Boxes) to member variables
    within the dialog class
  • Takes a CDataExchange object that will contain
    information about the direction and validation of
    the data

10
DoDataExchange
Dialog Object
Actual Dialog
UpdateData() automatically by DoModal()
m_strName
UpdateData() explicitly by OnOK()
Dialog constructor
getName()
IDM_ID
m_strID
Main Window Object
11
LoginDlg.cpp
  • void CLoginDlgDoDataExchange(CDataExchange
    pDX)
  • //call to the base class DoDataExchange
  • CDialogDoDataExchange(pDX)
  • //methods to exchange data to and from
    CLoginDlg controls
  • //and CLoginDlg data members
  • DDX_Text(pDX, IDC_NUM, m_iPassword)
  • DDX_Text(pDX, IDC_ID, m_strName)
  • Questions you should ask
  • How does this take place?
  • What information does the variable pDX hold?
  • How do I know does the ID know which control it
    is paired with?
  • What on earth is a control?

12
Types you can transfer
  • void AFXAPI DDX_Text( CDataExchange pDX, int
    nIDC, BYTE value )
  • void AFXAPI DDX_Text( CDataExchange pDX, int
    nIDC, short value )
  • void AFXAPI DDX_Text( CDataExchange pDX, int
    nIDC, int value )
  • void AFXAPI DDX_Text( CDataExchange pDX, int
    nIDC, UINT value )
  • void AFXAPI DDX_Text( CDataExchange pDX, int
    nIDC, long value )
  • void AFXAPI DDX_Text( CDataExchange pDX, int
    nIDC, DWORD value )
  • void AFXAPI DDX_Text( CDataExchange pDX, int
    nIDC, CString value )
  • void AFXAPI DDX_Text( CDataExchange pDX, int
    nIDC, float value )
  • void AFXAPI DDX_Text( CDataExchange pDX, int
    nIDC, double value )
  • void AFXAPI DDX_Text( CDataExchange pDX, int
    nIDC, COleCurrency value )
  • void AFXAPI DDX_Text( CDataExchange pDX, int
    nIDC, COleDateTime value )

13
DoDataExchange
Dialog Object
Actual Dialog
UpdateData() automatically by DoModal()
m_strName
UpdateData() explicitly by OnOK()
Dialog constructor
getName()
IDM_ID
m_strID
Main Window Object
14
Class Exercises
  • Create a text file (using a text editor) that
    contains ids and their respective numerical
    passwords. Change this program to look up the id
    and password in a text file to check for valid
    login.
  • Try to add another text box to your resource file
    and try getting information from your window
    class to your dialog class then to your dialog
    text box.
  • Using MS Dev Studio help files, look up
  • DoDataExchange
  • UpdateData
  • Find Map Objects Zip Run it
  • Load the setting dialog to see the controls
  • Examine the resource understand them
  • See DoDataExchange
  • Also look at OnInitDialog() What's this do?

15
Repainting the window
  • We have seen the need to refresh the window
    client area in several of our applications
  • Toggling between roll and tide
  • Minimizing and maximizing screen
  • How do we know will our system know when it needs
    to be redrawn?
  • Windows will notify our window when it should
    redraw itself (part of overall screen management)
  • If we want to capture this event, we have to
    override the appropriate method and specify that
    we are handling the repaint event in our message
    map.

16
Repainting our window (cont)
  • Have message sent to our window
  • We assume this will be sent when necessary
  • We can also force this even to be sent to
    ourselves by the method InvalidateRect(BOOL)
  • Overriding appropriate method
  • OnPaint () is the name of the method that needs
    to be overridden. It will have to redraw the
    screen in its current state
  • MessageMap
  • Must have the macro for this event listed in our
    CFrameWnd message map
  • ON_WM_PAINT()

17
Repaint example
  • Consider our simple program that simply printed
    out roll or tide in different colors.
  • Zip3 MenuDemo
  • demoWnd.h
  • Declaration of class derived from CFrameWnd
  • demoWnd.cpp
  • Definition of class derived from CFrameWnd
  • Message Map for class derived from CFrameWnd
  • Declaration, definition, and instantiation of
    class derived from CWinApp
  • menus.h
  • Necessary identifiers are defined
  • menus.rc
  • Description of our menu resource
  • Changes to make to this routine
  • Indicate we want to catch ON_WM_PAINT
  • Write a routine to repaint our window

18
Repainting the window
  • Changes to demoWnd.h
  • afx_msg void OnPaint()
  • Changes to message map part of demoWnd.cpp
  • BEGIN_MESSAGE_MAP
  • (CDemoWnd, CFrameWnd)
  • ON_COMMAND
  • (IDM_EXIT, OnExit)
  • ON_COMMAND
  • (IDM_TOGGLE, OnToggle)
  • ON_WM_PAINT( )
  • END_MESSAGE_MAP( )
  • The method OnPaint
  • afx_msg void
  • CDemoWndOnPaint()
  • CClientDC dc(this)
  • string strText
  • if (m_iToggle) strTextm_strTwo
  • dc.SetTextColor( RGB(0,0,255))
  • else strTextm_strOne
  • dc.SetTextColor( RGB(255,0,0))
  • dc.TextOut(50,50, strText.c_str())

19
Class Exercises
  • Add repaint capability to our menu program
  • Add the new method to the demoWnd.h file
  • Change the MESSAGE_MAP and add the OnPaint
    function to demoWnd.cpp
  • Run the program and observe the results
  • This is almost good what things do you notice
    that do not seem quite right?

20
2 problems with the current fix
  • First problem
  • What?
  • Why?
  • Second problem
  • What?
  • Why?

21
Problem One
  • The method OnPaint
  • afx_msg void
  • CMenuWndOnPaint( )
  • CClientDC dc(this)
  • string sText
  • if (m_iToggle) sTextm_sOne
  • dc.SetTextColor( RGB(255,0,0))
  • else sTextm_sTwo
  • dc.SetTextColor( RGB(0,0,255))
  • dc.TextOut(50,50, sText.c_str( ))
  • The method OnPaint went into a hard loop
  • Continuous updating of the window
  • Problem was CClientDC
  • Inside OnPaint Should be CPaintDC
  • Both are derived from CDC (Device Context Base
    Class)
  • Replace colored line with CPaintDC dc(this)

22
Why CPaintDC and not CClientDC?
  • OnPaint routine assumes that the developer
  • 1. Defines location to be repainted (indicates
    ready to paint, invoking the routine
    BeginPaint)
  • 2. Repaints what is necessary
  • 3. Indicates that painting has finished (call to
    EndPaint)
  • If we paint in OnPaint via CClientDC
  • System does not automatically do (1) (3) from
    above, not doing (3) gets us in trouble as the
    system never realizes were done with the refresh
    (since no call to EndPaint)
  • If we paint in OnPaint via CPaintDC
  • System automagically calls BeginPaint when we
    define our CPaintDC object and calls EndPaint
    when routine exits.

23
Problem Two
  • We arent refreshing screen between toggles
  • We only refresh when the WM_PAINT message is sent
    to our application.
  • How can we send this message to ourselves to
    force OnPaint to be called?
  • Invalidate(EraseTrue)
  • InvalidateRect(CRect, EraseTrue)

24
Displaying graphics
  • Can display bitmap (.bmp) files in window
  • Basic concept
  • Link a bitmap image to the program via the
    programs resource file
  • Load the bitmap image into a memory image
  • Whenever you re-paint the window, re-draw the
    image as part of the re-paint process

25
App that just displays an image
  • Header file
  • include ltafxwin.hgt
  • class CPictureWnd public CFrameWnd
  • public
  • CPictureWnd()
  • afx_msg void OnPaint()
  • private
  • CDC m_memDC
  • DECLARE_MESSAGE_MAP( )
  • Basic class declaration
  • Will put our image out on the window, and then
    repaint it whenever necessary
  • Data member
  • m_memDC contains a copy of the image in memory

26
Application (part one)
  • Constructor for the CPictureWin class
  • Create the window
  • Load in the picture into a bitmap image via
    LoadBitmap
  • This bmpPicture may be used for multiple BitMaps
  • Create compatible image in memory so that you can
    transfer the image in memory to the screen
  • Store information about the bitmap into the CDC
  • Release the current Bitmap in bmpPicture so you
    can create another one if you wish
  • CPictureWndCPictureWnd() Create(NULL, "Image
    Demo", WS_OVERLAPPEDWINDOW, CRect(100,100,500,50
    0))
  • CBitmap bmpPicture
  • bmpPicture.LoadBitmap ("MY_PICTURE")CClientDC
    dc(this)m_memDC.CreateCompatibleDC (dc)
  • m_memDC.SelectObject (bmpPicture)
  • bmpPicture.DeleteObject()

27
Application (part two)
  • afx_msg void CPictureWndOnPaint() CPaintDC
    dc(this)dc.BitBlt(50,50,120,100, m_memDC,
    0, 0, SRCCOPY)dc.BitBlt(200,200,120,100,
    m_memDC, 0, 0, SRCCOPY)
  • BEGIN_MESSAGE_MAP (CPictureWnd, CFrameWnd)
  • ON_WM_PAINT( )
  • END_MESSAGE_MAP( )
  • OnPaint routines redraws window, which includes
    the graphic image
  • Draw two copies of the image, one starting at
    (50,50) and one starting at (200,200).
  • Both draw what is in m_memDC
  • SRCCOPY is a bit-by-bit copy option

28
Application (part three)
  • class CPictureApp public CWinApp
  • public
  • BOOL InitInstance()
  • m_pMainWnd new CPictureWnd
  • m_pMainWnd-gtShowWindow (m_nCmdShow)
  • m_pMainWnd-gtUpdateWindow()
  • return TRUE
  • pictureApp
  • MY_PICT1 BITMAP "elephant.bmp"
  • Standard driver portion, loads an instance of
    CPictureWin as the application
  • Resource file shown on the left in red, only one
    line that identifies the bitmap file

29
Class Exercises
  • Download Zip4
  • Try out these 4 things
  • Modify the code so that you can get as many
    elephants as possible in the window without any
    of them overlapping
  • Involves multiple BitBlt statements in OnPaint
  • Make a hollow square of elephants.
  • Look for images on the internet to use in your
    application.
  • Add multiple images think about using a CDC
    array in order to store information about how the
    images should be displayed on the screen.
  • For multiple bitmaps, you only need multiple
    CDCs you may use one CBitmap instance to load
    the bitmaps as long as you call the
    CBitmapDeleteObject() method
Write a Comment
User Comments (0)
About PowerShow.com