15 мая 2023 года "Исходники.РУ" отмечают своё 23-летие!
Поздравляем всех причастных и неравнодушных с этим событием!
И огромное спасибо всем, кто был и остаётся с нами все эти годы!

Главная Форум Журнал Wiki DRKB Discuz!ML Помощь проекту


Notify view of application closure

Ganesh Vaideeswaran -- gvaidees@intertrust.com
Tuesday, April 01, 1997


Hi,

Environment: VC++ 4.2b, NT 4.0

How can the mainframe notify a view, that the user has chosen to exit the   
    

      


app.

I am displaying a set of rows from a database on a CListView. The user
can edit certain fields
in each item of the list view. Now when the chooses to exit out of the
app, I would like
to warn him/her that unsaved changes will be lost.

I would like to bring up a Yes/No/Cancel box with a custom message.

Now the problem, is that the main frame has no knowledge about the
changes made to the database
thro' the view. Only the view has any knowledge of changes made. But the   
    

      


view does not get a WM_CLOSE message.

Thanks






Mihir Dalal -- m_dalal@ECE.concordia.CA
Wednesday, April 02, 1997

[Mini-digest: 4 responses]


On Tue, 1 Apr 1997, Ganesh Vaideeswaran wrote:

> 
> Hi,
> 
> Environment: VC++ 4.2b, NT 4.0
> 
> How can the mainframe notify a view, that the user has chosen to exit the   
> app.
> 
> I am displaying a set of rows from a database on a CListView. The user
> can edit certain fields
> in each item of the list view. Now when the chooses to exit out of the
> app, I would like
> to warn him/her that unsaved changes will be lost.
> 
> I would like to bring up a Yes/No/Cancel box with a custom message.
> 
> Now the problem, is that the main frame has no knowledge about the
> changes made to the database
> thro' the view. Only the view has any knowledge of changes made. But the   
> view does not get a WM_CLOSE message.

So simply "GetActiveView()->SendMessage(WM_CLOSE,...)" in the appropriate
message handler in the mainframe which gets called when the user exits
your application.

Trap the WM_CLOSE in CYourView and write your Yes/No/Cancel MessageBox 
(or whatever) in that.  

Mihir. 
 _________________________________________________________________________
   Mihir Dalal , M.Engg. (Electrical) Student
            Department of Electrical and Computer Engineering
                 Concordia University, Montreal, Canada
              http://www.ECE.Concordia.CA/~m_dalal/addr.html

-----From: Jim Lawson Williams 

At 09:29 01-04-97 P, Ganesh Vaideeswaran  wrote:
>
>Hi,
>
>Environment: VC++ 4.2b, NT 4.0
>
>How can the mainframe notify a view, that the user has chosen to exit the   
>app.
>

Apparently my recent response to a similar question passed you by.
See below.  There's no need to notify a view/document.  OnCloseDocument()
will be called automatically.
  
Jim LW

At 02:16 20-03-97 -0500, Patrick Davis wrote:
>Environment: Win95 MSVC++4.0
>
>Can anyone help me in getting a pointer to my document class
>data from within the CMainFrame class ?

G'day!
>From anywhere within your program you can find your CWinApp-derived object
by means of AfxGetApp().  In a wizard-generated MDI program, it's obvious
your app. knows about the mainframe through m_pMainWnd.  In SDI, this is
less obvious because the set-up is done (in \Msdev\mfc\src\DOCSINGL.CPP) by:

	CWinThread* pThread = AfxGetThread();
	if (bCreated && pThread->m_pMainWnd == NULL)
	{
		// set as main frame (InitialUpdateFrame will show the window)
		pThread->m_pMainWnd = pFrame;
	}
	InitialUpdateFrame(pFrame, pDocument, bMakeVisible);

With the necessary pointers you can do the saving, either as changes occur,
or by catching OnCloseDocument().

Regards,
Jim LW




>From the BBC's "Barchester Chronicles":

    "I know that ultimately we are not supposed to understand.
    But I also know that we must try."

       -- the Reverend Septimus Harding, 
          crypt-analyst, art-critic, tax-consultant, C++ programmer,
          humanitarian

(If vegetarians eat vegetables, what do humanitarians eat?)
-----From: "Dalmer Junior" 

	You can create a member function for your view that say if the Database
changed.  When you receive a WM_CLOSE in your frame, you can take the
active view and "ask" for your view about the document.  Follow a example.

void CMainFrame::OnClose() 
{
	CView*	pActView;

	ASSERT( pActView = GetActiveView() );
	
	....

	if( !pActView->IsDatabaseModified() ) {
		//Show your MessageBox
		...
	}

	CFrameWnd::OnClose();
}

I never did this but I hope help you.

(:P
[]s,
-- 
*****************************************************************
*  Dalmer B. A. Junior                       Light-Infocon      *
*  mail: dalmer@light-infocon.com          Software Engineer    *
*  Office Tel: (954) 450-0085                  		        *
*  FAX       : (954) 438-1995                 --------  __o     *
*  http://www.light-infocon.com              -------  _`\<,_    *
*  "Life is Short Ride Tough"		    -------  (*)/ (*)   *
*****************************************************************
 

----------
> From: Ganesh Vaideeswaran 
> To: 'mfc-l@netcom.com'
> Subject: Notify view of application closure
> Date: Tuesday, April 01, 1997 4:29 AM
> 
> 
> Hi,
> 
> Environment: VC++ 4.2b, NT 4.0
> 
> How can the mainframe notify a view, that the user has chosen to exit the
  
>     
> 
>       
> 
> 
> app.
> 
> I am displaying a set of rows from a database on a CListView. The user
> can edit certain fields
> in each item of the list view. Now when the chooses to exit out of the
> app, I would like
> to warn him/her that unsaved changes will be lost.
> 
> I would like to bring up a Yes/No/Cancel box with a custom message.
> 
> Now the problem, is that the main frame has no knowledge about the
> changes made to the database
> thro' the view. Only the view has any knowledge of changes made. But the 
 
>     
> 
>       
> 
> 
> view does not get a WM_CLOSE message.
> 
> Thanks
> 
> 
-----From: DFPav@aol.com

1. Put your data (database connection, etc) in your document.
2.  When your view, changes the data, make sure, the document's modified flag
gets set
3.  When you attempt to close the MainFrame, it will try to close the
document.
4.  If the Document closes with the modified flag set it will pop up your
Yes/No/Cancel dialog that is pretty standard.
5. This occurs within the virtual CDocument::SaveModified
6. RTFM starting with the above function and work from there.

10Q
Dan



Mike Blaszczak -- mikeblas@nwlink.com
Wednesday, April 02, 1997

[Mini-digest: 4 responses]

At 09:29 4/1/97 P, Ganesh Vaideeswaran wrote:

>Environment: VC++ 4.2b, NT 4.0

>How can the mainframe notify a view, that the user has chosen to exit the   
>app.

Code a handler for the WM_CLOSE and/or system menu close message and/or
the File/Exit menu handler your application has.  In that code, make
a call to a member function you've written for the view.

>I am displaying a set of rows from a database on a CListView. The user
>can edit certain fields
>in each item of the list view. Now when the chooses to exit out of the
>app, I would like to warn him/her that unsaved changes will be lost.

Normally, people manage this kind of information in the document, since it's
to be the object in the application that carries the data the user is
editing. 
Even if your document isn't serving that purpose in your application, you
might
decide to use the same mechanism because all of the prompting is already wired
up for you and you won't have to worry about notifying your view of the
applications demise.

Read up on CDocument::SetModifiedFlag() to learn more about this technique.

>I would like to bring up a Yes/No/Cancel box with a custom message.

If you close a document that has the modified flag set, MFC will do this
for you.  It doesn't matter how you're closing the document.

>Now the problem, is that the main frame has no knowledge about the
>changes made to the database thro' the view. Only the view has
>any knowledge of changes made. But the view does not get a WM_CLOSE
>message.

I think your view should be telling the associated document about the
fact that there were changes made.


.B ekiM
http://www.nwlink.com/~mikeblas/
These words are my own. I do not speak on behalf of Microsoft.
       One is too many and a million is not enough.
-----From: Frank Pijpers 

Hello,

In general when the main frame receives a message that you want the (a)
view to process all you have to do is to re-route the message. Thus there
are three steps:
1) Add a message handler to the main frame for the message you want the
view to handle.
2) Have that message handler re-route the message to the view.
3) Add a handler for the message you want to handle to the view class.

The message handler in the main frame would look something like (assuming
only one view type):

BOOL CMainFrame::OnAMessage(CWnd* pWnd, A_MESSAGE_STRUCT* pAMessageStruct) 
{
	// Re-route the message to the view
	CView* pView = GetActiveView();
	if (pView)
	{
		pView->SendMessage(A_MESSAGE, WPARAM(this), 
			(LPARAM)pAMessageStruct);
	}
	
	return CFrameWnd::AMessage(pWnd, pAMessageStruct);
}

Sincerely,


Frank


At 09:29 AM 4/1/97 P, you wrote:
>
>Hi,
>
>Environment: VC++ 4.2b, NT 4.0
>
>How can the mainframe notify a view, that the user has chosen to exit the   
>    
>
>      
>
>
>app.
>
>I am displaying a set of rows from a database on a CListView. The user
>can edit certain fields
>in each item of the list view. Now when the chooses to exit out of the
>app, I would like
>to warn him/her that unsaved changes will be lost.
>
>I would like to bring up a Yes/No/Cancel box with a custom message.
>
>Now the problem, is that the main frame has no knowledge about the
>changes made to the database
>thro' the view. Only the view has any knowledge of changes made. But the   
>    
>
>      
>
>
>view does not get a WM_CLOSE message.
>
>Thanks
>
>
>
>
>
-----From: Stuart Downing 

>----------
From:  Ganesh Vaideeswaran[SMTP:gvaidees@intertrust.com]
>Subject:  Notify view of application closure
>How can the mainframe notify a view, that the user has chosen to exit the   
>app.

CFrameWnd handles the WM_QUERYENDSESSION message and attempts to save
any modified documents that are open in the app.  You can overide
the virtual CDocument::SaveModified to change the default behaviour 
(which is to prompt the user whether to save/don't save/cancel end 
session).  For a database app, you are almost certainly writing
changes to the database as the user navigates from record to record.
It doesn't make sense to try to use CDocument::SetModifiedFlag, because
you need to keep track of the modified state on a view-by-view basis,
not for the whole document.

Keep track in each view of whether the user has modified the current 
record.
Add a method to the view, (let's call it SaveModified) that determines if 
the current record is modified, and if so prompts the user whether to 
save/don't save/cancel.  It returns TRUE if it's OK to close the view,
FALSE otherise.
Override your CDocument's SaveModified.  In there, walk through all the
views attached to the document, calling CYourView::SaveModified().  If
all the views return TRUE, the document's SaveModified also returns TRUE.
(As soon as any view's SaveModified returns FALSE, the document's should
return FALSE immediately).

Other considerations...
* In the view's SaveModified, you'll probably want to activate the view 
if the record has been modified.  That way the user can see the record
that they are being prompted about.
* If the user has entered invalid data into the current field, you'll
want SaveModified to present an error message and return FALSE.
* If you're using MDI, you can also make use of the view's SaveModified
function in your CMDIChildWnd's WM_CLOSE handler to deal with the user
closing the view when changes are pending.

Market research time...
How much would you be willing to pay for some nice base classes that
do all this for you? (Sorry, I haven't written them - yet :)
-----
Stuart Downing
sdowning@fame.com
FAME Information Services, Inc.



-----From: Ben Burnett 

Ganesh Vaideeswaran wrote:
> 
> Hi,
> 
> Environment: VC++ 4.2b, NT 4.0
> 
> How can the mainframe notify a view, that the user has chosen to exit the
> 
> 
> 
> 
> app.
> 
> I am displaying a set of rows from a database on a CListView. The user
> can edit certain fields
> in each item of the list view. Now when the chooses to exit out of the
> app, I would like
> to warn him/her that unsaved changes will be lost.
> 
> I would like to bring up a Yes/No/Cancel box with a custom message.
> 
> Now the problem, is that the main frame has no knowledge about the
> changes made to the database
> thro' the view. Only the view has any knowledge of changes made. But the
> 
> view does not get a WM_CLOSE message.
> 
> Thanks

In your doc class there is a function called SetModifiedFlag( option );

In the view, when ever the user edits the data use the ptr to your doc
and set the SetModifiedFlag( option ) to TRUE, make the view question
you on exit as to wether to save or not.

Hope this helps.


-- 
----------------------------------------------
 Reality is for those who have no imagination 

 Ben Burnett   /   Pulse productions
 e-mail: benner@supernet.ab.ca




Become an MFC-L member | Вернуться в корень Архива |