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
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Strange date stored 22 155
Expand data scrubbing tool 13 35
ASCII Non-Printable characters/codes and their HTML equivalents 6 132
unix example issues 18 99
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit If you want to manage em…

830 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