Printing

This is a discussion on Printing within the Windows Programming forums, part of the Platform Specific Boards category; Hello again fellow programmers, Got a question. Does anyone have any good references on printing a document using MFC. I ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    184

    Printing

    Hello again fellow programmers,

    Got a question. Does anyone have any good references on printing a document using MFC. I know that MFC is not very favorable among a lot of people. I have an app that I am using to display images. The app was written using MFC. I have made many modifications to it such as view sizes and such. I have researched everywhere and have tried everything I can think of to implement printing using the CView::OnFilePrint function. When I add an MFC class to the project using CView as the base class, and then add "ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)", my print option in the window is grayed out and cannot be used. On the other hand, adding "ON_COMMAND(ID_FILE_PRINT, CWinApp::OnFilePrintSetup)" to the Message Map of the class that handles the open option that has the base class CWinApp..............the print option is available and opens the printer options dialog. Of course it does not do anything. What am I missing here? I know there is something. I will follow any leads. Any help will be appreciated.

    Sincerely Grateful,
    Kendal

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,699
    Printing is just like drawing to the screen - you have a printer DC that you draw to.
    Check out the online help

    gg

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    184
    I have been through that online help numerous times with no avail. The problem I am having is that the print option in my menu is grayed out so that I cannot select it. Why would it be grayed out???? If I use the same object in the message mapping using CWinApp as the base class and calling CWinApp::OnFilePrintSetup.........the print option becomes available. What would cause the Print option to be grayed out????? Any suggestions? As far as the printer DC.....the CView::OnFilePrint function is supposed to display the print dialog that will retrieve the Printer DC. That is my understanding from reading. If I am wrong, please let me know.

    Thanks again,
    Kendal
    Last edited by gvector1; 03-21-2003 at 08:36 AM.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,699
    The only reason that I know why it would be grayed is because the ID associated with the menu selection isn't "hooked up" in MFC. Make sure ID_FILE_PRINT is the correct ID.

    gg

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    184
    It is correct. I selected it from the dropdown list. Besides that when I use that same ID in with the CWinApp Message Map for the OnFilePrintSetup, it is no longer grayed and I can select it. It of course displays the Print Setup dialog, but does nothing. Here is some of the code from the project. I added the class KClass in with this code because I know the MyApp class works and if I uncomment the line of code in the MyApp message map, the print button becomes available. The code is very messy so please forgive me. I double checked to make sure that the ID for the print option matches the one in the code.

    Code:
    //
    #include "StdAfx.h"
    #include "TraceWin.h"
    #include "resource.h"
    #include "MainFrm.h"
    #include "View.h"
    #include "StatLink.h"
    #include "PictCtrl.h"
    
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif
    
    //////////////////
    // Standard application class
    //
    class CMyApp : public CWinApp {
    public:
    	DECLARE_DYNAMIC(CMyApp)
    	virtual BOOL InitInstance();
    protected:
    	DECLARE_MESSAGE_MAP()
    	afx_msg void OnAppAbout();
    };
    class KClass : public CView {
    public:
    	DECLARE_DYNAMIC(KClass)
    	virtual void OnDraw(CDC* pDC);      // overridden to draw this view
    #ifdef _DEBUG
    	virtual void AssertValid() const;
    	virtual void Dump(CDumpContext& dc) const;
    #endif
    protected:
    	DECLARE_MESSAGE_MAP()
    };
    
    BEGIN_MESSAGE_MAP(CMyApp, CWinApp)
    	ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
    	ON_COMMAND(ID_FILE_NEW,	 CWinApp::OnFileNew)
    	ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
    	//ON_COMMAND(ID_FILE_PRINT, CWinApp::OnFilePrintSetup)
    END_MESSAGE_MAP()
    
    BEGIN_MESSAGE_MAP(KClass, CView)
    	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
    END_MESSAGE_MAP()
    
    IMPLEMENT_DYNAMIC(KClass,CView)
    IMPLEMENT_DYNAMIC(CMyApp, CWinApp)
    
    CMyApp theApp;
    KClass KApp;
    
    
    BOOL CMyApp::InitInstance()
    {
    	SetRegistryKey("MSDN Magazine");	// Save settings in registry, not INI file
    	LoadStdProfileSettings(8);			// Standard file options (8 MRU files)
    
    	// initialize coolbars. Note: assumes you have
    	// comctl32.dll version 470 or greater
    	//
    	INITCOMMONCONTROLSEX sex;
    	sex.dwSize = sizeof(INITCOMMONCONTROLSEX);
    	sex.dwICC = ICC_COOL_CLASSES;
    	InitCommonControlsEx(&sex);
    
    	// Create standard doc/frame/view
    	AddDocTemplate(new CSingleDocTemplate(IDR_MAINFRAME,
    		RUNTIME_CLASS(CPictureDoc),
    		RUNTIME_CLASS(CMainFrame),
    		RUNTIME_CLASS(CPictureView)));
    
    	// Parse command line for standard shell commands, DDE, file open
    	CCommandLineInfo cmdInfo;
    	ParseCommandLine(cmdInfo);
    
    	// Dispatch commands specified on the command line
    	return ProcessShellCommand(cmdInfo);
    }
    
    //////////////////
    // Custom about dialog uses CStaticLink for hyperlinks.
    //    * for text control, URL is specified as text in dialog editor
    //    * for icon control, URL is specified by setting m_iconLink.m_link
    //
    class CAboutDialog : public CDialog {
    protected:
    	// static controls with hyperlinks
    	CStaticLink	m_wndLink1;
    	CStaticLink	m_wndLink2;
    	CStaticLink	m_wndLink3;
    	CPictureCtrl m_wndPict;
    
    public:
    	CAboutDialog() : CDialog(IDD_ABOUTBOX) { }
    	virtual BOOL OnInitDialog();
    };
    
    /////////////////
    // Initialize dialog: subclass static text/icon controls
    //
    BOOL CAboutDialog::OnInitDialog()
    {
    	// subclass static controls. URL is static text or 3rd arg
    	m_wndLink1.SubclassDlgItem(IDC_STATICURLPD,this);
    	m_wndLink2.SubclassDlgItem(IDC_STATICURLMSDN,this);
    	m_wndPict.SubclassDlgItem(IDC_IMAGECATFLY,this);
    	return CDialog::OnInitDialog();
    }
    
    //////////////////
    // Handle Help | About : run the About dialog
    //
    void CMyApp::OnAppAbout()
    {
    	static CAboutDialog dlg;
    	dlg.DoModal();
    }
    
    ///////////////////////////////////////////////////////////////////
    // KPrintClass drawing
    
    void KClass::OnDraw(CDC* pDC)
    {
    	CDocument* pDoc = GetDocument();
    	// TODO: add draw code here
    }
    
    // KPrintClass diagnostics
    
    #ifdef _DEBUG
    void KClass::AssertValid() const
    {
    	CView::AssertValid();
    }
    
    void KClass::Dump(CDumpContext& dc) const
    {
    	CView::Dump(dc);
    }
    #endif //_DEBUG
    What do you think???

    Thanks again,
    Kendal

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# Printing Problem
    By silverlight001 in forum C# Programming
    Replies: 0
    Last Post: 03-23-2009, 02:13 AM
  2. generic printing preferences dialog box
    By stanlvw in forum Windows Programming
    Replies: 8
    Last Post: 06-27-2008, 03:20 AM
  3. printing data to a file
    By coralreef in forum C Programming
    Replies: 3
    Last Post: 11-02-2006, 08:10 PM
  4. need help relating printing?
    By omarlodhi in forum Linux Programming
    Replies: 0
    Last Post: 03-03-2006, 04:46 AM
  5. Printing
    By Dual-Catfish in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 05-25-2002, 09:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21