Solved

How to close CFormView in Application?

Posted on 2000-02-19
18
803 Views
Last Modified: 2013-11-20
I'm using Single Doc Application for my prog. App starts with a blank white backgd. User can open a formview by clicking on menu item. However, I would like to implement code in a Cancel Button in the formview to close the view to go back to the blank white backgd. How do I do this?
0
Comment
Question by:Vriz
  • 9
  • 6
  • 3
18 Comments
 
LVL 12

Expert Comment

by:migel
ID: 2539655
Hi!
Please Show your code which you use to show FormView.
0
 
LVL 8

Expert Comment

by:VinExpert
ID: 2541104
Hi,

Map the WM_DESTROY message to Ur form view class and map the function for the Cancel button too. Then from the OnCancel() function call OnDestroy();

Thats it. The form view closes.
Hope this helps.
VinExpert
0
 

Author Comment

by:Vriz
ID: 2542322
Hi VinExpert,

I am still a beginner to Visual C++ programming and do not understand exactly how to do what you said. Could you explain in detail as to where to put the code?


1) Like how do I map the WM_DESTROY message to my form view class (eg. CNewFormView)?

2)  And how do I map the function for the Cancel button?

3) Then finally, do I write CNewFormView::OnDestroy() at my CNewFormView::OnCancel() function?
0
 
LVL 8

Expert Comment

by:VinExpert
ID: 2542381
Well, I will explain it U.

1. U have the form view with OK and Cancel button I suppose. The Id of the Ok button is IDOK(open the dialog template and right click on the ok button, select properties, u will get the id and other properties.) llly the ID of the cancel button is IDCANCEL.

2. Now select the Classwizard option from the view menu of Ur vc editor.
3. Select the view class in the class(for U it is CNewFormView) combo box.
4. Then bellow it r the messages list, among that list select WM_DESTROY, then say add function.

5. Similarly, in the Object IDs list U will find the IDCANCEL, click on it and click on BN_CLICKED option, then say add function. it will add OnCancel(). Now click on Edit Code button.

6. It will bring u to the NewFormView.cpp, OnCancel() function. There add the following like before the base class function call.
OnDestroy();

7. That should solve Ur problem.
Feel free to ask questions.
VinExpert
0
 

Author Comment

by:Vriz
ID: 2542525
Hi VinExpert,

I've done as you said but when I click on the button, nothing happens. By the way, I named the button "IDCANCEL_OP" and the function added is OnOp() cos IDCANCEL does not have BN_CLICKED but only BN_DOUBLECLICKED.
0
 
LVL 8

Expert Comment

by:VinExpert
ID: 2542551
Hi,

pls Paste that code here.

VinExpert
0
 

Author Comment

by:Vriz
ID: 2542670
I have add a function for WM_DESTROY and the following for my formview. I also received a error message when building the exe.

void CNewFormView::OnOp()
{
      // TODO: Add your control notification handler code here
      OnDestroy();
}


Error Message:
error LNK2001: unresolved external symbol "protected: void __thiscall CNewFormView::OnDestroy(void)" (?OnDestroy@CNewFormView@@IAEXXZ)
Debug/Control.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
0
 

Author Comment

by:Vriz
ID: 2542708
I have add a function for WM_DESTROY and the following for my formview. I also received a error message when building the exe.

void CNewFormView::OnOp()
{
      // TODO: Add your control notification handler code here
      OnDestroy();
}


Error Message:
error LNK2001: unresolved external symbol "protected: void __thiscall CNewFormView::OnDestroy(void)" (?OnDestroy@CNewFormView@@IAEXXZ)
Debug/Control.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
0
 
LVL 8

Expert Comment

by:VinExpert
ID: 2544733
Hi,

OHhhhh,

OK, let me explain u from scratch. I hope U need MDI. Start like this.

1. Open VC++ and from the file menu select new.
2. Select MFC AppWizard(exe) and give the name of the app as Test and say OK.
3.Dont change  the options and say Finish. It will prepare U the MDI app.
4. Now open the Test.cpp and go to InitInstance() and comment out the following lines as
// Parse command line for standard shell commands, DDE, file open
//      CCommandLineInfo cmdInfo;
//      ParseCommandLine(cmdInfo);

      // Dispatch commands specified on the command line
//      if (!ProcessShellCommand(cmdInfo))
//            return FALSE;
5. Now build and execute it. It will get the app with blank page.
6. Now U need to add Ur view. Close the running app.
7. From VC++ editor, select Insert->Resource, say dialog and say new. It will bring U the dialog.
8. Go to the properties of the dialog(right click on dialog), and select Styles tab, and change style to child.
9. Now double click on the dialog, it will pop up the add new class dialog, say Ok, Brings up the New class dialog. Give the name as CNewFormView and change the base class to CFormView(in the same dialog option is there). Say OK.
10. Then U R still in MFC ClassWizard dialog, Now click on Add Class button, and say New.
11. Give the Name as CNewChild and change the base class to CMDIChildWnd. Say OK.
12.Say Ok to class wizard also.
13. Go to InitInstance of Test.cpp and add the following lines
      pMyDocTemplate = new CMultiDocTemplate(
            IDR_TESTTYPE,
            RUNTIME_CLASS(CTestDoc),
            RUNTIME_CLASS(CNewChild), // custom MDI child frame
            RUNTIME_CLASS(CNewFormView));
      AddDocTemplate(pMyDocTemplate);
(U will find similar code there, insert it after that)
14. Add the includes to the Test.cpp as
#include "NewChild.h"
#include "NewFormView.h"

15. Open Test.h and add the following variable as public member
CMultiDocTemplate* pMyDocTemplate;

16. In Test.cpp go to function OnAppAbout(), modify it as
void CTestApp::OnAppAbout()
{
//      CAboutDlg aboutDlg;
//      aboutDlg.DoModal();
      pMyDocTemplate->OpenDocumentFile(NULL);
}

17. Now compile and run it. U will get blank app, then select the Help->About Test option. U will get the formview with Ok and Cancel button. U can't close it using Cancel, close it using the title bar button.
18. Now go to class wizard once again and select the class name as CNewFormView
19. Scroll down the object Ids list to get the IDCANCEL, select it, it will display two options, select BN_CLICKED and say Add Function. Leave function name as it is and say ok. Say Ok to class wizard too.
20. Now open the NewFormView.cpp, modify the OnCancel() function as
void CNewFormView::OnCancel()
{
      this->GetParent()->SendMessage(WM_CLOSE,0,0);
}

That will close the view on Cancel button click. Try it out.
VinExpert
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:Vriz
ID: 2544944
Adjusted points to 50
0
 

Author Comment

by:Vriz
ID: 2544945
VinExpert, all works well except for the cancel button. When I click on it, nothing happens. And also, do I have to use MDI? Cos now my prog is in SDI, if I want to switch to MDI, does it mean that I have to start all over again? Thanks for the help though. I'm increasing the points to 50.

void CNewFormView::OnCancel()
{
      // TODO: Add your control notification handler code here
      this->GetParent()->SendMessage(WM_CLOSE,0,0);
}
0
 
LVL 8

Expert Comment

by:VinExpert
ID: 2544956
Hi,

Do one thing, put the break point in OnCancel() function, and tell me whether it is getting called or not. If yes then try using
this->SendMessage(WM_CLOSE,0,0);

Bye the way I want to know how u have created the formview in an SDI so that I can simulate Ur prob.

VinExpert
0
 

Author Comment

by:Vriz
ID: 2545009
I have inserted a break point at the function but when I click on the button, nothing happens. I have zipped my program using SDI into test.zip and placed it on my webpage at "go.to/vriz".
0
 
LVL 12

Expert Comment

by:migel
ID: 2545045
hmm!
can`t open this page
can you sen me it via Email?
my address: migel.geo@yahoo.com
0
 

Author Comment

by:Vriz
ID: 2545080
sure migel
0
 
LVL 12

Accepted Solution

by:
migel earned 50 total points
ID: 2545088
Ok
got it :-)
hmm so simple :-)
here is code:
in the DialogView.h
add:
afx_msg void OnCancel();

// add include to the DialogView.cpp
#include "testdoc.h"
#include "testview.h"

// Importand Message MAP!!!!
BEGIN_MESSAGE_MAP(CDialogView, CFormView)
//{{AFX_MSG_MAP(CDialogView)
ON_EN_KILLFOCUS(IDC_EDIT1, OnKillfocusEdit1)
//}}AFX_MSG_MAP
ON_COMMAND(IDCANCEL, OnCancel)
END_MESSAGE_MAP()
//
void CDialogView::OnCancel()
{
      // TODO: Add your command handler code here
      // Sets a 32-bit value at a specified offset into the extra window memory
      CDocument* pDoc = GetDocument();

      CRuntimeClass* pNewViewClass = RUNTIME_CLASS(CTestView);
      // creates the view associated with a document
      CCreateContext context;
      // CRuntimeClass of the new view to create
      context.m_pNewViewClass = pNewViewClass;
      // current doc to associate with the view
      context.m_pCurrentDoc = pDoc;
      // cast a pointer to an object from one class to a pointer of a related type
      CFrameWnd* pWndFrame = GetParentFrame();
      CView* pNewView = STATIC_DOWNCAST(CView, pWndFrame->CreateView(&context));
      if (pNewView != NULL)
      {
            // the new view is there, but invisible and not active...
            pNewView->ShowWindow(SW_SHOW);
            // after view is first attached to the document, but before the view is initially displayed
            pNewView->OnInitialUpdate();
            pWndFrame->SetActiveView(pNewView);
            pWndFrame->RecalcLayout();
//            m_nCurrentFile = nCmdID;

            // finally destroy the myself..
            DestroyWindow();
      }      
}
0
 

Author Comment

by:Vriz
ID: 2545186
Thank you so much migel, it works.
However, I would like to thank VinExpert for your help too.
0
 
LVL 8

Expert Comment

by:VinExpert
ID: 2545248
Hi,

Welcome!!!!
:-)

VinExpert
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now