Presentation is loading. Please wait.

Presentation is loading. Please wait.

Steps to Build Frame Window Recipe Application

Similar presentations


Presentation on theme: "Steps to Build Frame Window Recipe Application"— Presentation transcript:

1 Steps to Build Frame Window Recipe Application
Camen Vaca Ruiz MFC Windows Applications Syracuse University

2 Goals 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” Syracuse University

3 Running Application Syracuse University

4 Build a blank solution Syracuse University

5 Single Document Interface
Syracuse University

6 Construction Steps- Resource Editor
Click on View/ Resource View FWRecipe/menu/IDR_MAINFRAME. That shows the menu bar. 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. Syracuse University

7 RESOURCE 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. Syracuse University

8 Use 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 Syracuse University

9 Construction 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. Syracuse University

10 Construction Steps- Add Member variable
Syracuse University

11 Construction Steps- 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. Syracuse University

12 Adding a function Syracuse University

13 Adding a function Syracuse University

14 Steps 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 Syracuse University

15 Adding 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). Syracuse University

16 Adding Message Handler for Mouse
Syracuse University

17 Adding 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. Syracuse University

18 Adding code and some variables
Syracuse University

19 Add 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 ChildView::OnCreate function and add the statements: m_pStatic = new CStatic(); CRect rect(CPoint(100,70),CSize(90,22)); m_pCStatic->Create(“”,WS_CHILD|WS_VISIBLE|WS_BORDER, rect, this, IDC_STATIC1); This creates the static control. Add the bottom four lines of code from the CChildView::OnMouseMove function in my example. Syracuse University

20 Add 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. Syracuse University

21 Add 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 ChildView::OnCreate function and add the statements: m_pStatic = new CStatic(); CRect rect(CPoint(100,70),CSize(90,22)); m_pCStatic->Create(“”,WS_CHILD|WS_VISIBLE|WS_BORDER, rect, this, IDC_STATIC1); This creates the static control. Add the bottom four lines of code from the CChildView::OnMouseMove function in my example. Syracuse University

22 Static variable defined in ChildView.h
Syracuse University

23 Add Code to Handle Messages
Syracuse University

24 Initialize 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 CMainFrame::CMainFrame() constructor. Add the intializers to the CChildView::CChildView() : 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 CChildView::~CChildView() destructor: delete m_pCStatic; Be sure you have these include directives in your ChildView.cpp file: #include "MainFrm.h" #include <sstream> #include <iomanip> using namespace std; Compile and see the running program in all its glory. Syracuse University

25 Initialize MainFrame Member
Syracuse University

26 Initialize Child View Members
Syracuse University

27 Running Application Syracuse University

28 Structure of FWRecipe Program
Syracuse University

29 Initialize 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 CMainFrame::CMainFrame() constructor. Add the intializers to the CChildView::CChildView() : 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 CChildView::~CChildView() destructor: delete m_pCStatic; Compile and see the running program in all its glory. Syracuse University

30 Closing 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 haven’t 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 application’s data in response to user inputs. Syracuse University


Download ppt "Steps to Build Frame Window Recipe Application"

Similar presentations


Ads by Google