CS 325 - PowerPoint PPT Presentation

1 / 31
About This Presentation
Title:

CS 325

Description:

Embedded within application as attribute of the frame. MapObjects. Always DoModal ... Same 'main driver' code ... Won't really have to worry about offset this time... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 32
Provided by: dyes
Category:
Tags: attribute

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 31
  • Today
  • Built in Dialogs
  • CFileDialog
  • CColorDialog

3
Projects 6 - 7
  • P6 WinOthelloGold
  • Ten improvements over the basic interface (not
    your basic interface over a conceptual basic
    interface P5 and P6 may be the same)
  • Use a dialog (or multiple dialogs) to give the
    user control over 5 of the improvements
    (background color, piece display, etc)
  • P7 WinFinalProject After Spring Break.
  • Show it to me anytime after break

4
OnPaint
  • Call OnPaint indirectly through one of these
    methods
  • Invalidate(BOOL eraseTRUE)
  • InvalidateRect(CRect paintregion, BOOL erase)
  • Calling Invalidate puts an invalidate message in
    the message queue. OnPaint will not be called
    immediately.
  • Void SomeMethod()
  • code
  • InvalidateRect(region1)
  • code
  • InvalidateRect(region2)
  • code
  • InvalidateRect(regionN)
  • This results in one (1!) call to paint and its
    at some later time. The paint regions are
    magically merged.

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
DoubleBuffering (Why isnt it automatic!)
  • afx_msg void CKalahFrameWndOnPaint()
  • //step 1 set up a buffer
  • CPaintDC paintDC(this) CDC dbufdc
  • dbufdc.CreateCompatibleDC(paintDC)
  • CRect screen GetClientRect(screen)
  • CBitmap bmp
  • bmp.CreateCompatibleBitmap(
  • paintDC, screen.Width(), screen.Height() )
  • dbufdc.SelectObject(bmp)
  • //step2 draw on dbbufdc
  • dbbuddc.TextOut()
  • dbbufdc.FillRect(CRect(x,y,xw, yh), pBrush)
  • //step3 copy buffer to screen
  • paintDC.BitBlt(0, 0, screen.Width(),
    screen.Height(),
  • dbufdc, 0, 0, SRCCOPY )

9
Common Errors What causes?
  • Using wrong menu or dialog name
  • Program will crash on startup
  • Make sure you match the name specified in RC
  • Calling create twice -- Assertion error
  • CBrush b new CBrush()
  • b-gtcreateSolidBrush(RED)
  • b-gtcreateSolidBrush(BLUE)
  • Calling create twice -- Assertion error
  • CBrush b new CBrush(RED)
  • b-gtcreateSolidBrush(BLUE)

10
Existing (useful) components
  • Common Dialog boxes
  • Open file, print file, etc.
  • Should be able to easily use existing common
    dialog boxes within your code
  • CDialog class has a subclass CCommonDialog
  • CCommonDialog contains
  • CFileDialog, CColorDialog, CPrintDialog,
  • Also have CPageSetupDialog, CFontDialog,
    CFindReplaceDialog (will not look at these)
  • These all require include ltafxdlgs.hgt

11
Example using CFileDialog
  • Our sample program
  • Uses the standard windows open file dialog
  • Opens a window, each left mouse click allows the
    user to select a file
  • After selection (dialog box closes), pop up a
    MessageBox that displays the selected file

12
Example Header
  • include ltafxwin.hgt
  • include ltafxdlgs.hgt
  • class CFileOpenWin public CFrameWnd
  • public
  • CFileOpenWin( )
  • afx_msg void OnLButtonDown (UINT, CPoint)
  • private
  • DECLARE_MESSAGE_MAP( )
  • Need one extra include file (afxdlgs.h)
  • Standard header
  • Handle only one event
  • Left mouse click

13
Example body (part 1)
  • include "main.h"
  • CFileOpenWinCFileOpenWin() Create(NULL, "File
    Open Example", WS_OVERLAPPEDWINDOW,CRect(100,100,
    500,500))
  • afx_msg void CFileOpenWinOnLButtonDown (UINT
    flags, CPoint p)
  • CFileDialog myFile(TRUE)
  • myFile.DoModal( )
  • char text32
  • strcpy(text, myFile.GetFileName( ) )
  • MessageBox(text, "file")
  • Nothing different in constructor
  • OnLButtonDown
  • Declare a CFileDialog object (TRUE implies
    open, FALSE implies save as)
  • Invoke the dialog box via DoModal( )
  • Can retrieve the filename selected via objects
    GetFileName method

14
Example body (part 2)
  • BEGIN_MESSAGE_MAP (CFileOpenWin, CFrameWnd)
  • ON_WM_LBUTTONDOWN( )
  • END_MESSAGE_MAP( )
  • class CFileOpenApp public CWinApp
  • public
  • BOOL InitInstance()
  • m_pMainWnd new CFileOpenWin
  • m_pMainWnd-gtShowWindow (m_nCmdShow)
  • m_pMainWnd-gtUpdateWindow()
  • return TRUE
  • fileApp
  • Standard message map (just handles left mouse
    clicks)
  • Standard main routine
  • Nothing unique or different about these routines

15
Comments on CFileDialog
  • Constructor Parameters
  • 1st open or save as
  • 2nd default file extension (NULL unless
    specified)
  • 3rd initial filename that appears in the box
    (NULL unless specified)
  • 4th flags (default NULL)
  • 5th filters (limit displayed files), default
    NULL
  • 6th object parent (NULL)
  • Data retrieval options
  • GetPathName( )
  • GetFileName( )
  • GetFileExt( )
  • GetFileTitle( )
  • All return a CString (array of characters). May
    either convert to string or use old strcpy,
    strcmp, etc. routines

16
Class Exercises
  • Example from previous slides is on the web
  • Zip 9
  • Run the program
  • Modify the program as follows
  • Create a text file that has your name and your
    partners name in it (two lines)
  • Have the program prompt for a file to use
  • Read from that file, display each of the two
    lines in a message box

17
Example using CColorDialog
  • include ltafxwin.hgt
  • include ltafxdlgs.hgt
  • class CColorWin public CFrameWnd
  • public
  • CColorWin()
  • afx_msg void OnLButtonDown (UINT, CPoint)
  • afx_msg void OnPaint()
  • private
  • COLORREF m_color
  • int m_offset
  • DECLARE_MESSAGE_MAP( )
  • Our example will allow the user to select a
    color, and then fill the window with that color
  • Basic class has two methods, handling
    OnLButtonDown and OnPaint
  • Two local members, one controls color and one is
    an offset for the rectangle we draw

18
Example body (part 1)
  • Standard constructor
  • Establishes window
  • Sets initial variable values, color is white and
    offset is zero
  • On left button click
  • Declare dialog box
  • Invoke dialog box
  • Retrieve color selected
  • Invoke OnPaint
  • include "main.h"
  • CColorWinCColorWin()
  • Create(NULL, "Color Example", WS_OVERLAPPEDWINDO
    W, CRect(100,100,510,530))m_color
    RGB(255,255,255)m_offset 0
  • afx_msg void CColorWinOnLButtonDown (UINT
    flags, CPoint p) CColorDialog
    myColormyColor.DoModal()m_color
    myColor.GetColor()Invalidate(FALSE)

19
Example body (part 2)
  • afx_msg void CColorWinOnPaint() CPaintDC
    dc(this)CBrush pBrush new CBrush(m_color)
    dc.FillRect(CRect(m_offset, m_offset,400,400),
    pBrush)m_offset 20if (m_offset 200)
    m_offset 0
  • BEGIN_MESSAGE_MAP (CColorWin, CFrameWnd)ON_WM_
    LBUTTONDOWN( )ON_WM_PAINT( )
  • END_MESSAGE_MAP( )
  • // Same main driver code
  • OnPaint routine draws a new rectangle (slightly
    offset) using the new color
  • Message map handles two events
  • Left button down
  • Repaint events

20
Class Exercises
  • Previous example is out on the web
  • Zip10
  • Fix this program so that
  • The size of the rectangle shrinks in all
    directions (not just the top and left)

21
CColorDialog from last time
  • It does not really repaint the way we would
    like it to repaint the screen
  • Minimize and then Maximize
  • It only remembers the last color, and thus only
    repaints it
  • Need it to retain a history of all the colors
    that were selected
  • We were also bothered by the fact m_offset
    changes in OnPaint
  • Need OnPaint to only refresh
  • Also, rectangles are boring
  • Draw circles

22
Solution to our problem
  • Keep a list of the colors we have utilized
  • Could do this by hand
  • Prefer something easier (vector class)
  • Add a vector of COLORREF objects to class
  • Also modify how we keep track of the number of
    colors we have displayed
  • In OnPaint routine
  • Cycle through vector and print each rectangle
  • Learn how to draw ellipses (circles)
  • Wont really have to worry about offset this
    time well determine offset based upon the
    position in the vector

23
Modified Class Header
  • include ltafxwin.hgt
  • include ltafxdlgs.hgt
  • include ltvectorgtusing namespace std
  • class CColorWin public CFrameWnd
  • publicCColorWin()afx_msg void
    OnLButtonDown (UINT, CPoint)afx_msg void
    OnPaint( )
  • privateint m_countvectorltCOLORREFgt
    m_vectorDECLARE_MESSAGE_MAP( )
  • Another include file
  • For vector class
  • using clause
  • Replace m_offset with m_count
  • No need to keep the individual COLORREF object as
    a data member
  • Declare a vector of COLORREF objects
  • Holds all the colors we have displayed
  • Dont need to worry about sizing vector (done
    automatically)

24
Modified OnLButtonDown
  • afx_msg void CColorWinOnLButtonDown (UINT
    flags, CPoint p)
  • CColorDialog myColor
  • myColor.DoModal( )
  • COLORREF color
  • color myColor.GetColor( )
  • m_vector.push_back(color)
  • InvalidateRect(NULL,FALSE)
  • m_count
  • if (m_count 10) m_count 0
  • Each time you add a color, insert it into the
    vector we are maintaining of colors we have used
  • Increment the number of colored circles we have
    displayed
  • Once we hit ten circles, start over (dont keep
    getting smaller forever)

25
Modified OnPaint routine
  • afx_msg voidCColorWinOnPaint() CPaintDC
    dc(this)CBrush pBrushCPen pPenfor (int
    a0 altm_count a) pBrush new
    CBrush(m_vectora) pPen new
    CPen(PS_SOLID, 1, m_vectora)
    dc.SelectObject(pBrush) dc.SelectObject(pPen)
    dc.Ellipse(a20,a20, 380-a20,380-a20)
    delete pBrush delete pPen
  • Allocate a Brush and Pen
  • Fill with brush color
  • Draw with pen color
  • For each color in vector
  • Assign brush color
  • Assign pen type color
  • Set the current objects
  • Brush color
  • Pen color
  • Draw the ellipse
  • Top-left lower-right bounding box (rectangle)
  • Delete current brush/pen

26
Class Exercises
  • The code from the last example is on the web
  • Zip 11
  • Run this program.
  • Remember the first color you add
  • What happens after you add ten colors and it
    cycles back to the big circle?
  • Why does this happen?
  • How can you fix it?

27
Creating processes in Windows
  • No exact Windows equivalent of fork and exec
  • Windows has CreateProcess method
  • Creates a new process and loads the specified
    program into that process (one step)
  • Requires include ltwindows.hgt
  • More parameters than fork exec
  • Most can be NULL
  • Just need to specify program to run and a few
    flags

28
Example using CreateProcess
  • include ltwindows.hgt
  • include ltiostream.hgt
  • void main( ) STARTUPINFO siPROCESS_INFORMATION
    piZeroMemory( si, sizeof(si) )si.cb
    sizeof(si)if ( ! CreateProcess( NULL,
    ..\\print\\debug\\print.exe 5 10", NULL,
    NULL, TRUE, 0, NULL, NULL, si, pi) ) cerr ltlt
    "CreateProcess failed." ltlt endlWaitForSingleObje
    ct( pi.hProcess, INFINITE )CloseHandle(
    pi.hProcess )CloseHandle( pi.hThread )
  • Need two variables to handle basic info must
    initialize correctly
  • Specify the location of the new process (our
    print process from last time)
  • Wait for it to finish

29
CreateProcess syntax
  • BOOL CreateProcess (
  • LPCTSTR lpApplicationName, // pointer to
    executable module
  • LPTSTR lpCommandLine, // pointer to command line
    string
  • LPSECURITY_ATTRIBUTES lpProcessAttrib, // process
    security
  • LPSECURITY_ATTRIBUTES lpThreadAttrib, // thread
    security
  • BOOL bInheritHandles, // handle inheritance flag
  • DWORD dwCreationFlags, // creation flags
  • LPVOID lpEnvironment, // pointer to new
    environment block
  • LPCTSTR lpCurrentDirectory, // pointer to current
    dir name
  • LPSTARTUPINFO lpStartupInfo, // pointer to
    STARTUPINFO
  • LPPROCESS_INFORMATION lpProcessInformation //
    pointer to PROCESS_INFORMATION
  • )

30
Comments on CreateProcess
  • Can specify program in either 1st or 2nd args
  • If in first, give location of program to run
  • If in second, give the command line to execute
  • Creation flags
  • If 0, runs in existing window
  • Also have other flags (combine with )
  • CREATE_NEW_CONSOLE probably most useful
  • Specify priority, linkage to parent, etc.
  • Structures pi and si used for process
    communication (how to start, basic info)

31
Class Exercises
  • Write a program (in the Windows VC environment)
    that starts up a copy of notepad as part of its
    execution
  • Notepad is located in (?) directory
  • Dont forget to double the \ in string literal
Write a Comment
User Comments (0)
About PowerShow.com