300 likes | 315 Views
Steps to Build Frame Window Recipe Application. Camen Vaca Ruiz MFC Windows Applications. Goals of the Project. D isplay a static text message in the center of the client area. Show current mouse coordinates in a text message above the static message.
E N D
Steps to Build Frame WindowRecipe Application Camen Vaca Ruiz MFC Windows Applications Syracuse University
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
Running Application Syracuse University
Build a blank solution Syracuse University
Single Document Interface Syracuse University
Construction 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. Syracuse University
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
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
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
Construction Steps- Add Member variable Syracuse University
Construction 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. Syracuse University
Adding a function Syracuse University
Adding a function Syracuse University
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
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
Adding Message Handler for Mouse Syracuse University
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
Adding code and some variables Syracuse University
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
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
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
Static variable defined in ChildView.h Syracuse University
Add Code to Handle Messages Syracuse University
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
Initialize MainFrame Member Syracuse University
Initialize Child View Members Syracuse University
Running Application Syracuse University
Structure of FWRecipe Program Syracuse University
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
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