Today - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

Today

Description:

Zip2. Run the program. Does a line of 79 characters or more wrap? ... Experiment #2: Re-write this program so it tells how many clicks you can do in 10 secs ... – PowerPoint PPT presentation

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

less

Transcript and Presenter's Notes

Title: Today


1
Lecture 20
  • Today
  • Timers
  • Capturing Text
  • Radio buttons
  • 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

2
Adding timers to our programs
  • Timers allow certain events to happen at specific
    times
  • Move things on a page
  • Provide a finite time for the user to respond
  • Simple example take our program from last time
    that displayed an elephant and move it around the
    page
  • Change our header slightly
  • Change our body (program)
  • Same resource file

3
Header Resource File
  • include
  • class CPictureWnd public CFrameWnd
  • public
  • CPictureWnd()
  • afx_msg void OnPaint()
  • afx_msg void OnTimer()
  • afx_msg void OnDestroy()
  • private
  • CDC m_memDC
  • int x, y
  • DECLARE_MESSAGE_MAP()
  • MY_PICTURE BITMAP "elephant.bmp"
  • Standard class header, now handle
  • Repaints
  • Timer
  • Destroy (at end of program)
  • Standard local variable
  • Resource file doesnt change

4
Program body (part 1)
  • include "pictureWnd.h"
  • CPictureWndCPictureWnd()
  • Create(NULL, "Image Example", WS_OVERLAPPEDWINDOW
    ,
  • CRect(100,100,500,500))
  • x 20 y 20
  • CBitmap bmpPicture
  • bmpPicture.LoadBitmap("MY_PICTURE")
  • CClientDC dc(this)
  • m_memDC.CreateCompatibleDC(dc)
  • m_memDC.SelectObject(bmpPicture)
  • afx_msg void CPictureWndOnDestroy()
    KillTimer(1)
  • Constructor does not change from last time, loads
    image into memory
  • New routine OnDestroy
  • invoked when window exits
  • shuts down timer

5
Program body (part 2)
  • afx_msg void CPictureWndOnPaint() CPaintDC
    dc(this)dc.BitBlt(x,y,120,100, m_memDC, 0,
    0, SRCCOPY)
  • afx_msg void CPictureWndOnTimer()
  • bool z FALSE
  • x10 if (x 200) x 20 z TRUE
  • y10 if (y 200) y 20 z TRUE
  • InvalidateRect(NULL, z)
  • OnPaint
  • draws image at the specified location
  • OnTime occurs when timer event occurs
  • increments x and y
  • Forces call of OnPaint
  • Second parameter indicates whether to erase
    current window or not

6
Program body (part 3)
  • BEGIN_MESSAGE_MAP (CPictureWin,
    CFrameWnd)ON_WM_TIMER( )ON_WM_PAINT(
    )ON_WM_DESTROY( )
  • END_MESSAGE_MAP()
  • class CPictureApp public CWinApp
  • public
  • BOOL InitInstance() m_pMainWnd new
    CPictureWndm_pMainWnd-ShowWindow(m_nCmdShow)m
    _pMainWnd-UpdateWindow()if (
    !m_pMainWnd-SetTimer(1,200,NULL) ) return
    FALSEreturn TRUE
  • pictureApp
  • Standard message map and CWinApp
  • CWinApp now also starts the timer
  • Identifier (ID)
  • Timer goes off every 200 milliseconds
  • Routine to handle timer events, NULL sends to
    current window

7
Class Exercises
  • The program is out on the web at
  • Zip1
  • pictureWnd.h
  • pictureWnd.cpp
  • resource.rc
  • elephant.bmp
  • Modify the code so that your elephant moves in a
    square (clockwise) around your window
  • Modify your code so that it
  • Moves very fast
  • Moves very slow

8
Capturing text typed in window
  • Basic idea is you want to capture keystrokes that
    are made within the window
  • Simple example
  • Capture keystrokes in the window
  • Just display the keys entered
  • Must worry about newlines and backspace keys
  • Basic assumptions for this example
  • Wont ever type more than 32 lines of 80
    characters each

9
Program Header
  • include
  • class CTextInputWin public CFrameWnd
  • public
  • CTextInputWin()afx_msg void OnPaint()afx_msg
    void OnChar (UINT, UINT, UINT)
  • privatechar m_arText3280int m_iRowint
    m_iLengthDECLARE_MESSAGE_MAP( )
  • Class declaration
  • OnChar routine has three arguments
  • Character
  • Repeat factor
  • Flags
  • Private data
  • Array of info typed
  • Current row
  • Current position in row

10
Program Body (part 1)
  • include "main.h"
  • CTextInputWinCTextInputWin()
  • Create(NULL, "Text Input Example",
    WS_OVERLAPPEDWINDOW,
  • CRect(100,100,500,500))
  • m_iRow 0
  • m_iLength 0
  • m_arText00 '\0'
  • afx_msg void CTextInputWinOnPaint()CPaintDC
    dc(this)for (int a0 adc.TextOut(10, 2015a, m_arTexta)
  • Constructor
  • Build window
  • Set up local variables (first row, first column,
    null terminated)
  • OnPaint simply re-writes all the characters in
    our array, one line at a time
  • Each row is offset

11
Program Body (part 1)
  • afx_msg void CTextInputWinOnChar (UINT ch, UINT
    repeat, UINT flags)
  • switch (ch) case '\r' m_arTextm_Rowm_Le
    ngth '\0' m_Row m_arTextm_Row0
    '\0' m_Length 0 breakcase '\b'
    if (m_Length 0) m_Length--
    breakdefault m_arTextm_Rowm_Lengthch
    m_arTextm_Rowm_Length
    '\0'InvalidateRect(NULL,)
  • Process a char
  • Switch on type of character
  • Newline, move to next row in array
  • Backspace, reset length of line
  • Normal char, add it to the line and mark the next
    position as the end
  • Repaint window

12
Program Body (part 3)
  • BEGIN_MESSAGE_MAP (CTextInputWin,
    CFrameWnd)ON_WM_PAINT( )ON_WM_CHAR( )
  • END_MESSAGE_MAP( )
  • class CTextInputApp public CWinApp
  • public
  • BOOL InitInstance() m_pMainWnd new
    CTextInputWinm_pMainWnd- ShowWindow(m_nCmdShow
    )m_pMainWnd-UpdateWindow()return TRUE
  • textApp
  • Message map handles character events and window
    repaints
  • Standard driver

13
Class Exercises
  • Download the example from class
  • Zip2
  • Run the program.
  • Does a line of 79 characters or more wrap?
  • Does backspace erase characters or just logically
    remove them?
  • Can you backspace up multiple lines?
  • How would you fix your program to backspace up
    multiple lines?
  • What happens if you type more than 32 lines or if
    you backspace when there is nothing there

14
3 CFrameWnd methods
  • To find the size of the window
  • GetWindowRect(LPRECT ( coordinates stored as
    usual CRect coordinates))
  • 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(LPRECT)
  • 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
Dialog Boxes
  • Can include both check boxes and radio
    buttons in a dialog box
  • Check boxes have two states
  • Checked
  • Not checked
  • Radio buttons have a number of alternatives
  • Only one alternative may be selected

16
Adding check boxes
  • Processing the button
  • CButton pButton1 (CButton )
    GetDlgItem(IDC_ID1)
  • CButton pButton2 (CButton )
    GetDlgItem(IDC_ID2)
  • if ( pButton1-GetCheck( ) )
  • MessageBox(ID1 Checked", "old")
  • else
  • MessageBox(ID1 Not Checked", "old")
  • if ( pButton2-GetCheck( ) )
  • Add to rc file
  • MyDialog DIALOG 20,20,100,100
  • CAPTION Some Caption
  • LTEXT
  • EDITTEXT
  • DEFPUSHBUTTON
  • AUTOCHECKBOX "New,
  • IDC_ID1, 65, 10, 30, 20
  • AUTOCHECKBOX "Old, IDC_ID2, 65, 40, 30, 20
  • Add identifiers to identifiers.h file

17
Adding Radio Buttons
  • Code in your routines
  • int ans GetCheckedRadioButton (IDM_A, IDM_C)
  • switch (ans)
  • case IDM_A MessageBox ("selected option A",
    "select") break
  • case IDM_B MessageBox ("selected option B",
    "select") break
  • case IDM_C MessageBox ("selected option C",
    "select") break
  • Resource File
  • GROUPBOX "A B C" , IDM_RADIO,100,100,60,60
  • AUTORADIOBUTTON "A", IDM_A, 120, 120, 10, 10
  • AUTORADIOBUTTON "B", IDM_B, 130, 130, 10, 10
  • AUTORADIOBUTTON "C", IDM_C, 140, 140, 10, 10
  • Groupbox (above) outlines the set of radio
    buttons
  • Code (other side) retrieves the checked option
    (within range of IDM_A to IDM_C)

18
Class Exercises
  • Zip3
  • Add two more option buttons (options 3 4)
  • Move the OK button down to the bottom of the
    dialog box, and make it square
  • Add two more radio buttons, line all up
    vertically
  • Make sure you can separate functionality and what
    methods perform what events.
  • Check out and play around with
  • SetWindowPos(NULL, 10, 10, 300, 200, 0)

19
Another example with timers
  • Must complete a task within a specified amount of
    time
  • Could use this to have a timed game.
  • Our example program will simply count down a
    timer to zero
  • We want to see if you can do 25 mouse clicks in
    10 seconds

20
Program header
  • include
  • class CTimeWin public CFrameWnd
  • publicCTimeWin()afx_msg void OnLButtonDown
    (UINT, CPoint)afx_msg void OnPaint()afx_msg
    void OnTimer()afx_msg void OnDestroy()
  • privateint m_nClicksint m_nTimeLeftCFont
    m_bigFontDECLARE_MESSAGE_MAP()
  • Standard header file
  • Handle
  • Left button events
  • OnPaint events
  • OnTimer events
  • OnDestroy events
  • Class members
  • Number of clicks
  • Time left
  • A font

21
Program body (part 1)
  • include
  • include "main.h"
  • CTimeWnd CTimeWnd()
  • Create(NULL, "Timer and Text Example", WS_OVERLA
    PPEDWINDOW,CRect(100,100,500,500))m_nClicks
    25m_nTimeLeft 10m_bigFont.CreateFont
  • (144,0,0,0,FW_BOLD,0,0,0,0,0,0,0,0,"Arial")Mess
    ageBox("Try to click the mouse 25 times in 10
    seconds", "Click Test")
  • afx_msg void CTimeWnd OnDestroy()
    KillTimer(1)
  • Standard constructor
  • Set variables
  • Define a font (size 144, bold, Arial)
  • Pop up a message box letting them know what to do
    before the window opens

22
Program body (part 2)
  • afx_msg void CTimeWnd OnPaint() CPaintDC
    dc(this)CFont pfont dc.SelectObject(m_bigFo
    nt)ostrstream ss rectGetClientRect(rect)int midX
    (rect.right / 2) -36strlen(arText)int midY
    (rect.bottom / 2) - 72dc.SetTextColor(RGB(255,0,
    0))dc.TextOut(midX,midY, arText)
  • Paint routine, writes the current time left (in
    seconds) using our specified font
  • Allocate a font
  • Put text in array
  • Get size of our current window
  • Calculate middle
  • Write the info using our font, centering it on
    the screen

23
Program body (part 3)
  • afx_msg void CTimeWnd OnTimer() if
    (m_nTimeLeft 0) KillTimer(1)
    MessageBox("Too Slow", "Click Test")
    SendMessage(WM_CLOSE)else
    InvalidateRect(NULL) m_nTimeLeft--
  • afx_msg void CTimeWnd OnLButtonDown (UINT a,
    CPoint p) m_nClicks--if (m_nClicks0)
    KillTimer(1)MessageBox("Success", "Click
    Test")
  • Timer routine
  • If reach zero, print message and close
  • Else redraw our window and decrement the counter
  • Button routine
  • Decrement clicks left
  • If at zero, kill timer (done)

24
Program body (part 4)
  • BEGIN_MESSAGE_MAP (CTimeWnd, CFrameWnd)ON_WM_LB
    UTTONDOWN()ON_WM_TIMER()ON_WM_PAINT()ON_WM_DEST
    ROY()
  • END_MESSAGE_MAP()
  • class CTimeApp public CWinApp
  • publicBOOL InitInstance() m_pMainWnd new
    CTimeWndm_pMainWnd-ShowWindow(m_nCmdShow)m_pM
    ainWnd-UpdateWindow()if ( !m_pMainWnd-SetTimer
    (1,1000,NULL) ) return FALSEreturn TRUE
  • timeApp
  • Standard message map
  • Standard application framework
  • Create window
  • Start timer

25
Class Exercises
  • Option One
  • The timer routine is out on the web
  • Zip4
  • main.h
  • main.cpp
  • Experiment 1
  • Comment out CreateFont line in the constructor
    and regenerate it watch how VC prompts you
  • Change the font size, change some other options
  • Look up CreateFont in the help for complete
    info
  • Experiment 2 Re-write this program so it tells
    how many clicks you can do in 10 secs
Write a Comment
User Comments (0)
About PowerShow.com