Title: CS 325
1CS 325
- Unix Programming Environment
- and
- Windows Programming
- with Microsoft Foundation Classes (MFC)
2Lecture 23
- Today
- Dialogs
- 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
3Projects 5 6 - 7
- P5 WinOthello Due in Next Monday.
- P6 WinOthelloGold Before Spring Break?
- P7 WinFinalProject After Spring Break.
43 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)
5Using Dialog Boxes
- Dialog boxes allow a clean method for getting
information from the user - Pop up a window
- Have the user enter data (as desired)
- Have buttons the user can select that perform
various actions - Dialog boxes can exist within a bigger
application, or can be a windows application all
by itself
6Simple (standalone) dialog app
- Goal create a dialog box that has one input
field, one display field, and three buttons - Input field allows the user to enter a number
- One button increments this value, one decrements
- Third button exits the routine
- New value displayed in the one output field
- Files
- dialogDlg.cpp, dialogDlg.h (C class
code/header) - dialogs_ids.h (mnemonics ids for linkage)
- dialog.rc (resource file, defines form of dialog
box)
7The file dialog_ids.h
- define IDC_NUM 2222
- define IDC_ANS 3333
- define IDC_INC 4444
- define IDC_DEC 5555
- define IDC_QUIT 9999
- Define message identifiers and their numeric
equivalent - Allows us to refer to messages within the program
using meaningful names
8The file dialog.rc (part 1)
- Include files needed
- Define the MyDialog dialog box
- Definition gives location
- Style of dialog box
- Fields in dialog box (next slide)
- Style
- ModalFrame keeps application from continuing
until dialog box is finished - Caption provides a caption on the top of the
dialog box - Popup indicates a stand-alone window (not part of
a bigger app)
- include ltafxwin.hgt
- include "dialog_ids.h"
- MyDialog DIALOG 200,200,200,250
- STYLE DS_MODALFRAME WS_CAPTION WS_POPUP
WS_VISIBLE
9The file dialog.rc (part 2)
- Set the dialog caption
- Define a static control field (displays text,
does not generate events), left justified
(LTEXT), (20,20) is top left corner, box is 50x10 - Defines an edit box, associated with IDC_NUM,
location size, ES_NUMBER indicates will only
accept numeric input - Define button, associated with IDC_INC, location
size - Define an edit box (for output), associate with
IDC_ANS, location size, readonly, user tabs do
not stop on this field - Define button, associated with IDC_QUIT, location
size
- CAPTION "Dialog Example"
- LTEXT "Enter a number", IDC_STATIC,
20,20,50,10 - EDITTEXT IDC_NUM, 20, 60, 50, 20, ES_NUMBER
- DEFPUSHBUTTON "Increment", IDC_INC, 20, 100,
50, 30 - DEFPUSHBUTTON "Decrement", IDC_DEC, 80, 100,
50, 30 - EDITTEXT IDC_ANS, 20, 140, 50, 20, ES_READONLY
NOT WS_TABSTOP - DEFPUSHBUTTON "Exit", IDC_QUIT, 20, 180, 30, 20
10The file dialogDlg.h
- include ltafxwin.hgt
- include "dialog_ids.h
- class CDialogDlg public CDialog
-
- public
- CDialogDlg()
- afx_msg void OnInc( )
- afx_msg void OnDec( )
- afx_msg void OnQuit( )
- private
- DECLARE_MESSAGE_MAP( )
- Notice here we do not inherit from CFrameWnd, but
rather CDialog - Our dialog class has a constructor and three
basic events it reacts to - Reacts when the user clicks the OnInc button
- Reacts when the user clicks the OnDec button
- Reacts when the user clicks the Quit button
- Standard message map to set up linkages between
class events
11The file dialogDlg.cpp (part 1)
- include ltstrstreagt
- using namespace std
- include dialogDlg.h"
- CDialogDlgCDialogDlg()
- CDialog("MyDialog")
- afx_msg void CDialogDlgOnQuit()
- SendMessage(WM_CLOSE)
- afx_msg void CDialogDlgOnDec()
- Include ltstrstreamgt
- Need to convert integers to string for output
- Constructor
- Invokes parent constructor with name of resources
(found in dialog.rc file) to use to build the
dialog box - Handle Quit button
- OnDec button not yet implemented
12The file dialogDlg.cpp (part 2)
- Get access to the input box(represented by
IDC_NUM in our resource file) - Move the text in the dialog box into an array
(arText) - Convert it to an integer
- Increment the integer
- Write the integer to an output string stream,
making sure to insert the NULL terminator - Get access to the output box (represented by
IDC_ANS in our resource file) - Move output string to this box
- Clear the input box
- Set the focus to the input box
- afx_msg void CDialogDlgOnInc()
- CEdit pNum (CEdit ) GetDlgItem(IDC_NUM)c
har arText32pNum-gtGetWindowText(arText,
32)int num atoi(arText)numostrstream
ss ltlt num ltlt '\0'CEdit pAns (CEdit )
GetDlgItem(IDC_ANS)pAns-gtSetWindowText(s.str())
pNum-gtSetWindowText("")pNum-gtSetFocus( )
13The file dialogDlg.cpp (part 3)
- Message map captures three events (clicking of
three buttons) - Overall application framework is different
- Creates a dialogApp object, makes it the
applications main window - DoModal() implies nothing else can be done until
this window is closed - Return FALSE indicating the program is now over
(dialog window was closed)
- BEGIN_MESSAGE_MAP
- ( CDialogDlg, CDialog )
- ON_COMMAND( IDC_INC, OnInc )
- ON_COMMAND( IDC_DEC, OnDec )
- ON_COMMAND( IDC_QUIT, OnQuit )
- END_MESSAGE_MAP( )
- class CDialogApp public CWinApp
- public
- BOOL InitInstance()
- CDialogDlg myDialogWin
- myDialogWin.DoModal()
- return FALSE
-
- dialogApp
14Class Exercises
- The code for the dialog example is on the web
- Zip1 (Zip file)
- Complete the OnDec routine
- Add WS_SYSMENU to Style, what happens?
- Clean up organization of the dialog window
- Make fields just large enough to do the job
- Re-arrange the fields (all buttons on one line,
etc)
15Dialog boxes in a bigger app
- Our last example used dialog boxes in a
standalone environment - Can include dialog boxes in larger applications
- Use to get information
- Use to display results
- Provides a simple interface for many actions
- Write a basic application
- Must enter a specific code to start the
application - Application just puts colored squares on window
16File organization for sample app
- LoginWin.h header file for main window
- LoginWin.cpp code for main window routines
- Menu items (Login and Quit)
- Handle left mouse clicks
- dialogDlg.h header file for dialog box
- dialogDlg.cpp code for dialog box
- Validate the secret code that was entered
- identifiers.h global identifiers (mnemonics)
- demo.rc resource file (menu dialog box)
17MessageMaps
- In the past
- Only one for the main window (instance of class
derived from CFrameWnd). - Only one for the main dialog(instance of class
derived from Cdialog). - Today, we will have 2 message maps for one
application - One for the instance of class derived from
CFrameWnd - One for the instance of class derived from CDialog
18Message Maps
- //Message Map for instance of class derived from
CFrameWnd - BEGIN_MESSAGE_MAP (CLoginWin, CFrameWnd)ON_COMMA
ND(IDM_LOGIN, OnLogin)ON_COMMAND(IDM_QUIT,
OnQuit)ON_WM_LBUTTONDOWN() - END_MESSAGE_MAP()
- //Message Map for instance of class derived from
CDialog - BEGIN_MESSAGE_MAP ( CDialogDlg, CDialog )
- ON_COMMAND( IDC_OK, OnOK )
- ON_COMMAND( IDC_EXIT, OnExit )
- END_MESSAGE_MAP( )
19Login.h DialogDlg.h files
- include ltafxwin.hgt
- include "identifiers.h"
- class CDialogDlg public CDialog
- public
- CDialogDlg()
- afx_msg void OnOK()
- afx_msg void OnExit()
- private
- DECLARE_MESSAGE_MAP()
-
- include ltafxwin.hgt
- include "identifiers.h"
- class CLoginWin public CFrameWnd
- public
- CLoginWin()
- afx_msg void OnLogin()
- afx_msg void OnQuit()
- afx_msg void OnLButtonDown
- (UINT, CPoint)
- private
- int m_iFlag
- DECLARE_MESSAGE_MAP( )
20Resource File (demo.rc)
- include ltafxres.hgt
- include "identifiers.h"
- MyMenus MENU
- POPUP "File"
- MENUITEM "Login", IDM_LOGIN
- MENUITEM "Quit", IDM_QUIT
-
- MyDialog DIALOG 20,20,100,100
- CAPTION "Login Process"
-
- LTEXT "Enter the secret number",
- IDC_STATIC, 10,10,50,10
- EDITTEXT IDC_NUM, 10, 40, 50, 20,
- ES_NUMBER
- DEFPUSHBUTTON "OK", IDC_OK,
- 10, 70, 50, 10
- DEFPUSHBUTTON "Exit Program",
- IDC_EXIT, 10, 85, 50, 10
21LoginWin.cpp file (part 1)
- include LoginWin.h"
- include "dialogDlg.h"
- CLoginWinCLoginWin() m_iFlag(0)
- Create(NULL, "Login Example",WS_OVERLAPPEDWINDOW
,CRect(100,100,500,500), NULL,"MyMenus") -
- afx_msg void CLoginWinOnQuit()
SendMessage(WM_CLOSE) - afx_msg void CLoginWinOnLogin()
- CDialogDlg loginBox
- if (loginBox.DoModal() IDOK) m_iFlag 1
- else SendMessage(WM_CLOSE)
- Standard include files constructor
- Exit routine when user selects Quit option from
the menu - Create a dialog window when user selects Login
from menu. Check return code of dialog box to
determine action.
22LoginWin.cpp file (part 2)
- afx_msg void CLoginWinOnLButtonDown (UINT
flags, CPoint point) - if (m_iFlag) CClientDC dc(this) int
size, r, g, b size rand()50 r
rand()255 g rand()255 b
rand()255 CBrush pBrush new CBrush()
pBrush-gtCreateSolidBrush(RGB(r,g,b))
dc.FillRect(CRect(point.x, point.y,
point.xsize, point.ysize), pBrush) - delete pBrush
-
- Routine draws randomly placed, randomly colored
rectangles on the window (assuming the user
entered a valid number)
23LoginWin.cpp file (part 3)
- BEGIN_MESSAGE_MAP (CLoginWin, CFrameWnd)ON_COMMA
ND(IDM_LOGIN, OnLogin)ON_COMMAND(IDM_QUIT,
OnQuit)ON_WM_LBUTTONDOWN() - END_MESSAGE_MAP()
- class CLoginApp public CWinApp
- public
- BOOL InitInstance() m_pMainWnd new
CLoginWinm_pMainWnd-gtShowWindow
(m_nCmdShow)m_pMainWnd-gtUpdateWindow()retu
rn TRUE -
- loginApp
- Message map for main window
- Must handle two menu options and left mouse
button - Standard application driver routine
24DialogDlg.cpp (part 1)
- include "dialog.h"
- CDialogDlgCDialogDlg() CDialog("MyDialog")
- afx_msg void CDialogDlgOnExit()
EndDialog(IDNO) - BEGIN_MESSAGE_MAP
- ( CDialogDlg, CDialog )
- ON_COMMAND( IDC_OK, OnOK )
- ON_COMMAND( IDC_EXIT, OnExit )
- END_MESSAGE_MAP( )
- Standard Constructor
- If user selects exit, then return IDNO
- Message map handles two events (two buttons)
25DialogDlg.cpp (part 2)
- afx_msg void CDialogDlgOnOK() CEdit pNum
(CEdit ) GetDlgItem(IDC_NUM)char
arText32pNum-gtGetWindowText(arText, 32)int
num atoi(arText)if (num 31415)
MessageBox(Authorization Approved", "LoginResu
lts") EndDialog(IDOK)else
MessageBox("Authorization Failed", "Login
Results") EndDialog(IDNO) -
- Check number the user entered
- Get access to the number via GetDlgItem
- Move the number into an array and convert integer
- Check against the secret number
- Display results for user
- When this function (OnOK) ends, return from
DoModal either IDOK or IDNO
26Class Exercises
- Execute this program
- Zip2
- Add the stanza ES_PASSWORD at the end of the
EDITTEXT field for IDC_NUM in the demo.rc file.
How does this change things? - EDITTEXT IDC_NUM, 10, 40, 50, 20, ES_NUMBER
ES_PASSWORD
27DoDataExchange
- 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
28DoDataExchange
- 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
29DoDataExchange
Dialog Object
Actual Dialog
UpdateData() automatically by DoModal()
m_strName
UpdateData() explicitly by OnOK()
Dialog constructor
getName()
IDM_ID
m_strID
Main Window Object
30LoginDlg.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
31LoginDlg.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
32LoginDlg.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?
33Types 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 )
34LoginWin.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()
35LoginWin.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
36LoginWin.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") -
37DoDataExchange
Dialog Object
Actual Dialog
UpdateData() automatically by DoModal()
m_strName
UpdateData() explicitly by OnOK()
Dialog constructor
getName()
IDM_ID
m_strID
Main Window Object
38Class 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