programatically closing app

Posted on 2002-06-25
Last Modified: 2013-11-20
I have SDI (CRichEditView) app. I want to do following:
On command line I want to send some parameter, load and print the document file and close the app. Right now I am able to print the file (by calling OnFilePrint) but I get the regular printer select dialog. I don't want this dialog and want to print on default printer. Also after printing I want to close the app. I am trying to send WM_CLOSE message to main window. It closes the app but gives debug error after closing. Also before closing, it asks if I want to save the file. I don't want this prompt (I don't want to save it) and want to smoothly close the app without error.

How can I achieve it?
Please help.
Question by:shitallj
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
LVL 42

Expert Comment

ID: 7109559
i've encountered Dan Pilat's article on regarding printing features.

u might won't need all the functionalities that the print classes provides but there is a solution for your select print dialog.

"Don't need a print dialog -
Printing some reports requires either your own custom dialog, or no dialog at all. This can be a problem, because the standard CPrintDialog typically creates the device context for you. In these non-standard situations, you can use the GPrintJob::UseDefaults() function to create a default device context for you.

for closing the application try ExitInstance() or send ID_APP_EXIT to your app instance.

Accepted Solution

Melange earned 200 total points
ID: 7111186
If you're using the standard SDI/document architecture, then using /p (along with the filename if needed) on the command line will launch the app in "print direct" mode, which sounds exactly like what you want (prints with no user input and then exits).

You don't even need to call OnFilePrint yourself.

To simulate Print Direct manually, in the OnPreparePrinting function of the view class set the m_bDirect flag of the CPrintInfo structure before calling DoPreparePrinting:

pInfo->m_bDirect = TRUE;

Expert Comment

ID: 7111201
Note: /p and /pt are built-in to MFC's standard command line processing. /p says to "print and exit" using the current default printer. /pt specifies which printer to use (via 3 following parameters for printer device driver).

These are also invoked by the shell if you drag the associated document for your app to a printer icon. The settings for this are set in the registry under MFC's default registry settings for your application.

Author Comment

ID: 7114315
Hi Melange,
your solution is partlyl working for me. Yes, I want to print and close the document but I am not going to load the document from a file. Instead, I am going to create it from the parameters sent on command line. So I added your suggestion pInfo->m_bDirect = TRUE; before calling OnFilePrint and it is printing without showing up printer dialog. Now my problem is, after printing I want to close it but don't want to show up the prompt for saving it. (don't want to save it). I think there must be some member variable like m_bDirty which I should set to FALSE before sending ID_APP_EXIT or WM_CLOSE command. Any suggestion?

Expert Comment

ID: 7114834
In the document class the function SetModifiedFlag controls whether or not the document is dirty.

Featured Post

Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

617 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