• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 272
  • Last Modified:

programatically closing app

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.
  • 3
1 Solution
Meir RivkinFull stack Software EngineerCommented:
i've encountered Dan Pilat's article on codeproject.com 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.
check http://www.codeproject.com/printing/printingmadeeasy.asp

"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.
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;
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.
shitalljAuthor Commented:
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?
In the document class the function SetModifiedFlag controls whether or not the document is dirty.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now