Today - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

Today

Description:

Zip2 contains. demoWnd.h. Declaration of class derived from CFrameWnd. demoWnd.cpp ... Whenever you re-paint the window, re-draw the image as part of the re ... – PowerPoint PPT presentation

Number of Views:124
Avg rating:3.0/5.0
Slides: 30
Provided by: janett
Category:
Tags: re | rezip2 | today | zip2

less

Transcript and Presenter's Notes

Title: Today


1
Lecture 19
  • Today
  • Information to and from Dialog Boxes
  • Refreshing the Screen (OnPaint)
  • Go ahead and get started
  • Fire up Dev Studio when you get logged on
  • Download the files for the first class exercise
  • Announcements
  • Project 4 due Nov 6
  • If you did not receive an e-mail from me, you are
    working alone see discussion group for a list of
    names for groups
  • More resources
  • http//msdn.microsoft.com/library
  • a help file on the website from last Weekss
    lecture
  • http//cs.ua.edu/325/tictactoehelp.pdf
  • Good idea to add OnPaint routine to TicTacToe.

2
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

3
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
  • Lets look at our login example

4
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
5
LoginDlg.h
  • class CLoginDlg public CDialog
  • public
  • CLoginDlg(CString)
  • afx_msg void OnOK()
  • CString getName(void)
  • void DoDataExchange(CDataExchange pDX)
  • private
  • int m_iPassword
  • CString m_strName
  • DECLARE_MESSAGE_MAP()
  • Constructor
  • Initialize user ID
  • OnOK
  • Call UpdateData
  • getName
  • Returns the last user ID entered if password was
    correct
  • DoDataExchange
  • New method we must add

6
LoginDlg.cpp
  • afx_msg void CLoginDlgOnOK()
  • //makes a call to DoDataExchange
  • UpdateData()
  • //in this case our pass word is 50 for everyone
  • if (m_iPassword 50) EndDialog(IDOK)
  • else EndDialog(IDNO)
  • CString CLoginDlggetName(void)return
    m_strName

7
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?

8
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 )

9
LoginWin.h
  • class CLoginWin public CFrameWnd
  • public
  • CLoginWin()
  • afx_msg void OnLButtonDown(UINT, CPoint p)
  • afx_msg void OnLogin()
  • afx_msg void OnQuit()
  • afx_msg void OnOk()
  • private
  • BOOL m_bGo
  • CString m_iID
  • DECLARE_MESSAGE_MAP()

10
LoginWin.cpp
  • afx_msg void CLoginWinOnLogin()
  • //instantiate the Dialog
  • CLoginDlg loginBox(m_iID)
  • //display dialog box
  • int ans loginBox.DoModal()
  • // if DoModal returns IDOK, then the user
    entered a valid password
  • if (ans IDOK)
  • m_iIDloginBox.getName()
  • m_bGo TRUE
  • else
  • m_bGo FALSE

11
LoginWin.cpp
  • afx_msg void CLoginWinOnLButtonDown(UINT,
    CPoint p)
  • if (m_bGo)
  • MessageBox ("You get to see this cool message
    box!!!")
  • else
  • MessageBox("Shame on you for trying to see the
    really cool message box without permission")

12
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
13
Class Exercises
  • You can find this program at Zip1
  • 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

14
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)

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
  • Zip2 contains
  • 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 purple 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?

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
  • 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)
  • 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()
  • 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

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-ShowWindow (m_nCmdShow)
  • m_pMainWnd-UpdateWindow()
  • 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 Zip3
  • 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