Title: Steps to Build Frame Window Recipe Application
1Steps to Build Frame WindowRecipe Application
Camen Vaca Ruiz MFC Windows Applications
2Goals of the Project
- Display a static text message in the center of
the client area. - Show current mouse coordinates in a text message
above the static message. - Show current mouse coordinates in static control
above the message from item 2., demonstrates how
that can be done. - Toggle the message from item 2. on and off, using
a menu selection labeled toggle
3Running Application
4Build a blank solution
5Single Document Interface
6Construction Steps- Resource Editor
- Click on
- View/ Resource View
- FWRecipe/menu/IDR_MAINFRAME. That shows the menu
bar. - Click on
- Edit item, and select each of its items in turn
and hit the delete key to remove them. - Type in the word Toggle in the box below the Edit
tag.
- You can simply hit Delete.
7RESOURCE VIEW- Add menus
- Compile the program to see an empty frame window
with the normal system menu - When you click on edit you will see the single
Toggle item grayed out. It is grayed out because
we have not defined a message handler for that
yet.
8Use class wizard to add Menu Handler
- To add a handler in the menu option
- Right click on Toggle/ Add Event Handler
- Choose CMainFrame unde Class Name
- Change Function handler name to OnToggle
- Click Add and Edit
- Add the code of the example to the function body
9Construction Steps- Add Member variable
- To add a member variable
- Click Menu View ? Class View (excellent view for
navigating around your code and initiating class
wizard activities) - Right click on CmainFrame class and select Add
Member Variable. - Enter the type BOOL and name m_bToggle.
- MAKE SURE YOU SELECT Private Public is the
default selection. - Click Finish This creates a private member
variable named m_bToggle in the CmainFrame class
to hold the state of the menu toggle.
10Construction Steps- Add Member variable
11Construction Steps- Add a function
- Add a function
- Right click on CmainFrame in the Class View again
- Select Add Member Function
- Enter the return type BOOL and the name getToggle
- This time leave the selection of Access as
public. This will create an accessor function
for your view to get the state of the toggle. - Add the code return m_bToggle to the function
body.
12Adding a function
13Adding a function
14Steps to support View
- Go to Class View
- Double click on the CChildView class
- View the inline function OnPaint() that the
wizard has defined for you. - Add the code from the class example to the
OnPaint() function
15Adding Message Handlers
- Right click on the CChildView class
- Choose Properties. Click on Messages button
- Select WM_LBUTTONDOWN, and Add
- Repeat for WM_MOUSEMOVE and WM_CREATE
- Note that the class wizard has already provided
the message hanler for the WM_PAINT message (the
OnPaint function we looked at before).
16Adding Message Handler for Mouse
17Adding code to message Handlers
- Add code from the class example to OnMouseMove
and OnLButtonDown message handler functions,
skipping the last four lines in OnMouseMove. - Add member variables (int)
- m_DisplayX, m_DisplayY, m_MouseX, m_MouseY,
m_TriangleX and m_TriangleY as private - Do that using the wizard like we did before, or
add them manually by typing into the class
declaration. - These code additions will draw the static text in
the center of the diagram, draw a triangle at the
cursor when the left button is clicked, and show
the mouse coordiates in a text messge. Compile
and show that this works.
18Adding code and some variables
19Add Static Control to Client Area
- Now, we want to add the static control to also
show mouse coordinates. - Go to the FileView and double click on resource.h
to see the contents of that file in the edit
window. Add define IDC_STATIC1 and pick a
number to enter on the right which is one more
that the highest number shown in the top group.
Add 1 to the value of _APS_NEXT_COMMAND_VALUE. - Add the member variable m_pCStatic as a private
data member of the CChildView class typed as
Cstatic. - Go to the ChildViewOnCreate function and add
the statements - m_pStatic new CStatic()
- CRect rect(CPoint(100,70),CSize(90,22))
- m_pCStatic-gtCreate(,WS_CHILDWS_VISIBLEWS_BOR
DER, rect, this, IDC_STATIC1) - This creates the static control. Add the bottom
four lines of code from the CChildViewOnMouseMov
e function in my example.
20Add Static Control to Client Area
- Now, we want to add the static control to also
show mouse coordinates. - Go to the Solution Explore
- Double click on resource.h to see the contents of
that file in the edit window. - Add define IDC_STATIC1 and pick a number to
enter on the right which is one more that the
highest number shown in the top group. - Add 1 to the value of _APS_NEXT_COMMAND_VALUE.
21Add Static Control to Client Area
- Add the member variable m_pCStatic as a private
data member of the CChildView class typed as
Cstatic. - Go to the ChildViewOnCreate function and add
the statements - m_pStatic new CStatic()
- CRect rect(CPoint(100,70),CSize(90,22))
- m_pCStatic-gtCreate(,WS_CHILDWS_VISIBLEWS_BOR
DER, rect, this, IDC_STATIC1) - This creates the static control. Add the bottom
four lines of code from the CChildViewOnMouseMov
e function in my example.
22Static variable defined in ChildView.h
23Add Code to Handle Messages
24Initialize Members in Main and Child Classes
- Finally, we need to give initial values to the
member variables we added to CMainFrame and
CChildView. - Add the intializer m_bToggle(TRUE) to the
CMainFrameCMainFrame() constructor. - Add the intializers to the CChildViewCChildView(
) m_DisplayX(100), m_DisplayY(100),
m_MouseX(0), m_MouseY(0), m_TriangleX(-1),
m_TriangleY(-1), m_pCStatic(NULL)and add to
the CChildViewCChildView() destructor
delete m_pCStatic - Be sure you have these include directives in your
ChildView.cpp file - include "MainFrm.h"
- include ltsstreamgt
- include ltiomanipgt
- using namespace std
- Compile and see the running program in all its
glory.
25Initialize MainFrame Member
26Initialize Child View Members
27Running Application
28Structure of FWRecipe Program
29Initialize Members in Main and Child Classes
- Finally, we need to give initial values to the
member variables we added to CMainFrame and
CChildView. - Add the intializer m_bToggle(TRUE) to the
CMainFrameCMainFrame() constructor. - Add the intializers to the CChildViewCChildView(
) m_DisplayX(100), m_DisplayY(100),
m_MouseX(0), m_MouseY(0), m_TriangleX(-1),
m_TriangleY(-1), m_pCStatic(NULL)and add to
the CChildViewCChildView() destructor
delete m_pCStatic - Compile and see the running program in all its
glory.
30Closing Notes
- Test application by compiling and running each
time you add a new Windows Message Handler. - Make sure you include stdafx in the cpp file
for each server module you add we havent used
any server modules in this example, but you will
want to for any relatively complex application. - Test each server module as a stand-alone module,
using its test stub, before you call it from the
interface code. - Your server modules are usually called by the
interface message handlers - They affect transformations on the applications
data in response to user inputs.