Link to home
Start Free TrialLog in
Avatar of Vriz
Vriz

asked on

How to close CFormView in Application?

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?
Avatar of migel
migel

Hi!
Please Show your code which you use to show FormView.
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
Avatar of Vriz

ASKER

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?
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
Avatar of Vriz

ASKER

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.
Hi,

pls Paste that code here.

VinExpert
Avatar of Vriz

ASKER

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.
Avatar of Vriz

ASKER

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.
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
Avatar of Vriz

ASKER

Adjusted points to 50
Avatar of Vriz

ASKER

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);
}
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
Avatar of Vriz

ASKER

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".
hmm!
can`t open this page
can you sen me it via Email?
my address: migel.geo@yahoo.com
Avatar of Vriz

ASKER

sure migel
ASKER CERTIFIED SOLUTION
Avatar of migel
migel

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Vriz

ASKER

Thank you so much migel, it works.
However, I would like to thank VinExpert for your help too.
Hi,

Welcome!!!!
:-)

VinExpert