Solved

Printing View and Dialog

Posted on 1999-01-15
12
623 Views
Last Modified: 2013-11-20
How can I print a ViewForm?
I have a lot of info regarding a database.

So I want to print all statics, textboxes and combostrings
etc.

How do i accomplish this, (i have used the wizard to create my program and it has print preview, print etc.  But I want to set it up to print the form, (close to screen dump)

then I would like to go to a Dialog (pop up) and print ITS contents as well. (as is, (close to screen dump))

Please post SOURCE code, that would be the desirable choice.
(not notes on CDC classes and then this and this)

Please just comment what you have done, thanks.

I know 20 points is little, but it is all I have left. (24)

Thanks in advance.
P.S I do need this rather urgently but I can wait till monday.
0
Comment
Question by:The_Brain
  • 7
  • 5
12 Comments
 
LVL 1

Author Comment

by:The_Brain
Comment Utility
Edited text of question
0
 
LVL 3

Accepted Solution

by:
stefanr earned 30 total points
Comment Utility
Below is the complete source code for a base class that enables printing (dumping) of a CFormView. The process for a dialog is similar.

//**************************H************************************

// PrintableFormView.h : interface of the CPrintableFormView class
//
/////////////////////////////////////////////////////////////////////////////

class CPrintableFormView;

#if !defined(AFX_PRINTABLEFORMVIEW_H__8F244F5E_B4BF_11D1_9325_006008987AFD__INCLUDED_)
#define AFX_PRINTABLEFORMVIEW_H__8F244F5E_B4BF_11D1_9325_006008987AFD__INCLUDED_

#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000

// This class implements a printable form view that dumps the view contents visible on the screen to the printer.
class YFX_YFC_EXT_CLASS CPrintableFormView : public CFormView
{
protected: // create from serialization only
      CPrintableFormView(LPCTSTR lpszTemplateName);
      CPrintableFormView(UINT nIDTemplate);

      DECLARE_DYNAMIC(CPrintableFormView)

public:
      //{{AFX_DATA(CPrintableFormView)
            // NOTE: the ClassWizard will add data members here
      //}}AFX_DATA

// Attributes
public:

// Operations
public:
   void SetMargins(const int nLeft, const int nTop, const int nRight, const int nBottom)
      {
         m_nMarginLeft = nLeft;
         m_nMarginTop = nTop;
         m_nMarginRight = nRight;
         m_nMarginBottom = nBottom;
      }
   int GetLeftMargin() const { return m_nMarginLeft; }
   int GetTopMargin() const { return m_nMarginTop; }
   int GetRightMargin() const { return m_nMarginRight; }
   int GetBottomMargin() const { return m_nMarginBottom; }

// Overrides
      // ClassWizard generated virtual function overrides
      //{{AFX_VIRTUAL(CPrintableFormView)
      protected:
      virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
      virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
      virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
      virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
      virtual void OnPrint(CDC* pDC, CPrintInfo* pInfo);
      //}}AFX_VIRTUAL

// Implementation
public:
      virtual ~CPrintableFormView();
#ifdef _DEBUG
      virtual void AssertValid() const;
      virtual void Dump(CDumpContext& dc) const;
#endif

protected:

// Generated message map functions
protected:
      int m_nMarginBottom;
      int m_nMarginRight;
      int m_nMarginTop;
      int m_nMarginLeft;
      CBitmap* m_pOldBitmap;
      CBitmap* m_pbmMemory;
      CDC* m_pMemoryDC;
      CRect m_rectClipBox;
      //{{AFX_MSG(CPrintableFormView)
      afx_msg void OnRButtonDown(UINT nFlags, CPoint point);
      //}}AFX_MSG
      DECLARE_MESSAGE_MAP()
private:
      void Init();
      void DeleteMemoryDC(CDC** ppDC, CBitmap** ppbm, CBitmap* pOldBitmap);
      CBitmap* CreateMemoryDC(CDC** ppDC, CBitmap** ppbm);
};

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Developer Studio will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_PRINTABLEFORMVIEW_H__8F244F5E_B4BF_11D1_9325_006008987AFD__INCLUDED_)

//**************************CPP**********************************

// PrintableFormView.cpp : implementation of the CPrintableFormView class
//

#include "stdafx.h"

#include "PrintableFormView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CPrintableFormView

IMPLEMENT_DYNAMIC(CPrintableFormView, CFormView)

BEGIN_MESSAGE_MAP(CPrintableFormView, CFormView)
      //{{AFX_MSG_MAP(CPrintableFormView)
      ON_WM_RBUTTONDOWN()
      //}}AFX_MSG_MAP
      // Standard printing commands
      ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
      ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
      ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPrintableFormView construction/destruction

/////////////////////////////////////////////////////////////////////////////
// Init - initializes the object's member variables.
/////////////////////////////////////////////////////////////////////////////
void CPrintableFormView::Init()
{
      //{{AFX_DATA_INIT(CPrintableFormView)
            // NOTE: the ClassWizard will add member initialization here
      //}}AFX_DATA_INIT
      // TODO: add construction code here
   m_pMemoryDC = NULL;
   m_pbmMemory = NULL;
   m_pOldBitmap = NULL;
   m_rectClipBox = CRect(0, 0, 0, 0);
   m_nMarginBottom = 0;
   m_nMarginRight = 0;
   m_nMarginTop = 0;
   m_nMarginLeft = 0;
}

/////////////////////////////////////////////////////////////////////////////
// CPrintableFormView - constructor.
/////////////////////////////////////////////////////////////////////////////
CPrintableFormView::CPrintableFormView(LPCTSTR lpszTemplateName)
      : CFormView(lpszTemplateName)
{
   Init();
}

/////////////////////////////////////////////////////////////////////////////
// CPrintableFormView - constructor.
/////////////////////////////////////////////////////////////////////////////
CPrintableFormView::CPrintableFormView(UINT nIDTemplate)
      : CFormView(nIDTemplate)
{
   Init();
}

/////////////////////////////////////////////////////////////////////////////
// ~CPrintableFormView - destructor.
/////////////////////////////////////////////////////////////////////////////
CPrintableFormView::~CPrintableFormView()
{
   delete m_pMemoryDC;
   delete m_pbmMemory;
}

/////////////////////////////////////////////////////////////////////////////
// CreateMemoryDC - used internally to make a copy of the view as a bitmap.
/////////////////////////////////////////////////////////////////////////////
CBitmap* CPrintableFormView::CreateMemoryDC(CDC * * ppDC, CBitmap * * ppbm)
{
   ASSERT(ppDC != NULL);
   ASSERT(ppbm != NULL);
   ASSERT(*ppDC == NULL);
   ASSERT(*ppbm == NULL);

   CClientDC ClientDC(this);
   ClientDC.SetMapMode(MM_LOMETRIC);
   
   GetClientRect(m_rectClipBox);
   ClientDC.DPtoLP(m_rectClipBox);
#ifdef _DEBUG
   afxDump << "m_rectClipBox = " << m_rectClipBox << "\n";
#endif


   CDC* pDC = new CDC();
   if (!pDC->CreateCompatibleDC(&ClientDC))
   {
      TRACE("Failed to create compatible DC\n");
      delete pDC;
      return NULL;
   }

   CBitmap* pbm = new CBitmap();
   if (!pbm->CreateCompatibleBitmap(&ClientDC, m_rectClipBox.Width(), abs(m_rectClipBox.Height())))
   {
      TRACE("Failed to create compatible bitmap\n");
      delete pbm;
      delete pDC;
      return NULL;
   }

   CBitmap* pOldBitmap = pDC->SelectObject(pbm);

   pDC->SetMapMode(MM_LOMETRIC);
   UpdateWindow();
   pDC->BitBlt(0, 0, m_rectClipBox.Width(), m_rectClipBox.Height(), &ClientDC, 0, 0, SRCCOPY); // Make in-memory copy of this dialog.

   *ppbm = pbm;
   *ppDC = pDC;
   return pOldBitmap;
}

/////////////////////////////////////////////////////////////////////////////
// DeleteMemoryDC - used internally to free the resources allocated in CreateMemoryDC.
/////////////////////////////////////////////////////////////////////////////
void CPrintableFormView::DeleteMemoryDC(CDC * * ppDC, CBitmap * * ppbm, CBitmap * pOldBitmap)
{
   ASSERT(ppDC != NULL);
   ASSERT(ppbm != NULL);

   if (pOldBitmap != NULL && *ppDC != NULL)
      (*ppDC)->SelectObject(pOldBitmap);

   delete *ppDC;
   delete *ppbm;
   *ppDC = NULL;
   *ppbm = NULL;
}

/////////////////////////////////////////////////////////////////////////////
// DoDataExchange - called by the framework to do data exchange and validation.
/////////////////////////////////////////////////////////////////////////////
void CPrintableFormView::DoDataExchange(CDataExchange* pDX)
{
      CFormView::DoDataExchange(pDX);
      //{{AFX_DATA_MAP(CPrintableFormView)
            // NOTE: the ClassWizard will add DDX and DDV calls here
      //}}AFX_DATA_MAP
}

/////////////////////////////////////////////////////////////////////////////
// CPrintableFormView printing

/////////////////////////////////////////////////////////////////////////////
// OnPreparePrinting - called from OnFilePrint before the dialog is printed.
/////////////////////////////////////////////////////////////////////////////
BOOL CPrintableFormView::OnPreparePrinting(CPrintInfo* pInfo)
{
      // default preparation
      return DoPreparePrinting(pInfo);
}

/////////////////////////////////////////////////////////////////////////////
// OnBeginPrinting - called from OnFilePrint at the beginning of a print job.
/////////////////////////////////////////////////////////////////////////////
void CPrintableFormView::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo)
{
   m_pOldBitmap = CreateMemoryDC(&m_pMemoryDC, &m_pbmMemory); // MM_LOMETRIC mapping mode is preset.
   if (NULL == m_pOldBitmap)
      return;

   const int knHorzSize = 10 * pDC->GetDeviceCaps(HORZSIZE); // MM_LOMETRIC (0.1 mm).
   const int knVertSize = 10 * pDC->GetDeviceCaps(VERTSIZE); // MM_LOMETRIC (0.1 mm).
   //TRACE("HORZSIZE = %d mm, VERTSIZE = %d mm\n", knHorzSize/10, knVertSize/10);

   SetMargins(200, 100, 200, 100); // MM_LOMETRIC (0.1 mm).

   const int knPrintWidth = knHorzSize - m_nMarginLeft - m_nMarginRight;
   const int knPrintHeight = knVertSize - m_nMarginTop - m_nMarginBottom;
   const int knMaxPage = (abs(m_rectClipBox.Width())/knPrintWidth + 1) *
                         (abs(m_rectClipBox.Height())/knPrintHeight + 1);
   //TRACE("Max Page = %d\n", knMaxPage);
   ASSERT(knPrintWidth > 0 && knPrintHeight > 0 && knMaxPage > 0);
   pInfo->SetMaxPage(knMaxPage);
}

/////////////////////////////////////////////////////////////////////////////
// OnEndPrinting - called from OnFilePrint after the form has been printed.
/////////////////////////////////////////////////////////////////////////////
void CPrintableFormView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
   DeleteMemoryDC(&m_pMemoryDC, &m_pbmMemory, m_pOldBitmap);
}

/////////////////////////////////////////////////////////////////////////////
// OnPrint - called from OnFilePrint to print a page of the form.
/////////////////////////////////////////////////////////////////////////////
void CPrintableFormView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
{
   //TRACE("void CPrintableFormView::OnPrint(CDC* pDC, CPrintInfo* pInfo)\n");

   ASSERT(pDC != NULL);
   ASSERT(pInfo != NULL);
   ASSERT(m_pMemoryDC != NULL);

   const int knHorzSize = 10 * pDC->GetDeviceCaps(HORZSIZE); // MM_LOMETRIC (0.1 mm).
   const int knVertSize = 10 * pDC->GetDeviceCaps(VERTSIZE); // MM_LOMETRIC (0.1 mm).

   pDC->SetMapMode(MM_LOMETRIC);

   const int knPrintWidth = knHorzSize - m_nMarginLeft - m_nMarginRight;
   const int knPrintHeight = knVertSize - m_nMarginTop - m_nMarginBottom;
   const int knMaxPageX = (abs(m_rectClipBox.Width())/knPrintWidth + 1);
   const int knMaxPageY = (abs(m_rectClipBox.Height())/knPrintHeight + 1);

   ASSERT(knPrintWidth > 0 && knPrintHeight > 0);

   int x = m_nMarginLeft;
   int y = -m_nMarginTop;
   int nWidth = knPrintWidth;
   int nHeight = -knPrintHeight;
   int xSrc = ((pInfo->m_nCurPage-1) % knMaxPageX) * knPrintWidth;
   int ySrc = ((pInfo->m_nCurPage-1) / knMaxPageX) * knPrintHeight;
   ySrc = -ySrc;

   if (abs(m_rectClipBox.Width() - xSrc) < abs(nWidth))
      nWidth = m_rectClipBox.Width() - xSrc;
   if (abs(m_rectClipBox.Height() - ySrc) < abs(nHeight))
      nHeight = m_rectClipBox.Height() - ySrc;

   //TRACE("pDC->BitBlt(x = %d, y = %d, nWidth = %d, nHeight = %d, m_pMemoryDC, xSrc = %d, ySrc = %d, SRCCOPY);\n", x, y, nWidth, nHeight, xSrc, ySrc);
   pDC->BitBlt(x, y, nWidth, nHeight, m_pMemoryDC, xSrc, ySrc, SRCCOPY);
}

/////////////////////////////////////////////////////////////////////////////
// CPrintableFormView diagnostics

#ifdef _DEBUG
void CPrintableFormView::AssertValid() const
{
      CFormView::AssertValid();
}

void CPrintableFormView::Dump(CDumpContext& dc) const
{
      CFormView::Dump(dc);
}
#endif

/////////////////////////////////////////////////////////////////////////////
// CPrintableFormView message handlers

/////////////////////////////////////////////////////////////////////////////
// OnRButtonDown - message handler for the WM_RBUTTONDOWN Windows message.
/////////////////////////////////////////////////////////////////////////////
void CPrintableFormView::OnRButtonDown(UINT nFlags, CPoint point)
{
   CMenu menu;

   CString strMenuItemPrint;
   strMenuItemPrint.LoadString(YFX_IDS_MENUITEM_FILE_PRINT);

   if (menu.CreatePopupMenu())
   {
      menu.AppendMenu(MF_STRING | MF_ENABLED, ID_FILE_PRINT, strMenuItemPrint);

      CPoint pointClient = point;
      ClientToScreen(&pointClient);
      menu.TrackPopupMenu(TPM_CENTERALIGN | TPM_LEFTBUTTON, pointClient.x, pointClient.y, this, NULL);
   }

   CFormView::OnRButtonDown(nFlags, point);
}

0
 
LVL 1

Author Comment

by:The_Brain
Comment Utility
Sorry for asking a terribly dumb question, but I don't have too much time to look through the code to find out how to use it.

How do I use this?
(I have already created a project and of the CDaoRecordView type
my program is complete except for printing.)

Where do I use what?
do I use this in OnPrint()?

etc.

Thanks for the answer, I will most likely increase the points by 10 (that is I will have ten more points since I have asked the question.)

I will post it up to 25 so long, but please send a clear description on  HOW to use it.

If you say, declare something, tell me how, I DON'T want to mess up, this is due on Tuesday that is why I need more explaining.  thanks a trillion... (I hope you understand that I cannot afford to waste anytime...)
oh yeah, and how do I use this for the dialog?




0
 
LVL 3

Expert Comment

by:stefanr
Comment Utility
You use it just like the standard CFormView, as a base class for your view. You may disregard the OnRButtonDown function, since its only purpose is to create a pop-up menu to offer the Print command for a frame window without any menu. Otherwise, the only functions of interest is the SetMargins, GetBottomMargin, GetLeftMargin, GetRightMargin, and GetTopMargin. The margins set is in logical units. The default margins (set in OnBeginPrinting) is { left 200, top 100, right 200, bottom 100 }.
For the CPrintableFormView, it suffices to use the standard print command. Just create your CFormView as usual, then replace all references to CFormView in the .h and .cpp file with CPrintableFormView, and you're done.

Just remember that if you (i.e. ClassWizard) override OnPreparePrinting to call DoPreparePrinting.

B.T.W. disregard the YFX_YFC_EXT_CLASS macro, since it is a custom macro to be used used only to export/import from a DLL in our own system.

The process for a dialog is similar. In the code below I have made it use the same functions for the printing process as in a CView, copying some code from the MFC source files. I have also added to/created a new menu on the dialog (in OnCreate) to obtain a way to begin the printing process:

//**************************H************************************

class CPrintableDlg;

#if !defined(AFX_PRINTABLEDLG_H__77B9AC52_AEB6_11D1_9322_006008987AFD__INCLUDED_)
#define AFX_PRINTABLEDLG_H__77B9AC52_AEB6_11D1_9322_006008987AFD__INCLUDED_

#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
// PrintableDlg.h : header file
//

// This class implements a printable dialog that dumps the dialog contents visible on the screen to the printer.

/////////////////////////////////////////////////////////////////////////////
// CPrintableDlg dialog

class CPrintableDlg : public CDialog
{
// Construction
public:
   BOOL DoPreparePrinting(CPrintInfo* pInfo);
   CPrintableDlg();
   CPrintableDlg(LPCTSTR lpszTemplateName, CWnd* pParent = NULL);
   CPrintableDlg(UINT nIDTemplate, CWnd* pParent = NULL);   // standard constructor
   virtual  ~CPrintableDlg();

   void SetMargins(const int nLeft, const int nTop, const int nRight, const int nBottom)
      {
         m_nMarginLeft = nLeft;
         m_nMarginTop = nTop;
         m_nMarginRight = nRight;
         m_nMarginBottom = nBottom;
      }
   int GetLeftMargin() const { return m_nMarginLeft; }
   int GetTopMargin() const { return m_nMarginTop; }
   int GetRightMargin() const { return m_nMarginRight; }
   int GetBottomMargin() const { return m_nMarginBottom; }

// Dialog Data
      //{{AFX_DATA(CPrintableDlg)
            // NOTE: the ClassWizard will add data members here
      //}}AFX_DATA


// Overrides
      // ClassWizard generated virtual function overrides
      //{{AFX_VIRTUAL(CPrintableDlg)
      protected:
      virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
      //}}AFX_VIRTUAL

// Implementation
protected:
      CMenu* m_pSubMenu;
      CMenu* m_pMenu;
      int m_nMarginBottom;
      int m_nMarginRight;
      int m_nMarginTop;
      int m_nMarginLeft;
      CBitmap* m_pbmMemory;
      CRect m_rectClipBox;
      CDC* m_pMemoryDC;
      virtual void OnPrint(CDC* pDC, CPrintInfo* pInfo);
      virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
      virtual void OnPrepareDC(CDC* pDC, CPrintInfo* pInfo = NULL);
      virtual void OnEndPrintPreview(CDC* pDC, CPrintInfo* pInfo, POINT point, CPreviewView* pView);
      virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
      virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
      void DeleteMemoryDC(CDC** ppDC, CBitmap** ppbm, CBitmap* pOldBitmap);
      CBitmap* CreateMemoryDC(CDC** ppDC, CBitmap** ppbm);

      // Generated message map functions
      //{{AFX_MSG(CPrintableDlg)
      afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
      //}}AFX_MSG
      afx_msg void OnFilePrint();
      DECLARE_MESSAGE_MAP()
private:
      void Init();
};

//{{AFX_INSERT_LOCATION}}
// Microsoft Developer Studio will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_PRINTABLEDLG_H__77B9AC52_AEB6_11D1_9322_006008987AFD__INCLUDED_)

//**************************CPP**********************************

// PrintableDlg.cpp : implementation file
//

#include "stdafx.h"

#include "PrintableDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// Printing Dialog (copied from VIEWPRINT.CPP).

class CPrintingDialog1 : public CDialog
{
public:
   //{{AFX_DATA(CPrintingDialog1)
   enum { IDD = AFX_IDD_PRINTDLG };
   //}}AFX_DATA
   CPrintingDialog1(CWnd* pParent);
   virtual ~CPrintingDialog1();

   virtual BOOL OnInitDialog();
   virtual void OnCancel();

public:
   static BOOL m_bUserAbort; // BAD!!! But how else?
};

/*static*/ BOOL CPrintingDialog1::m_bUserAbort = FALSE;

CPrintingDialog1::CPrintingDialog1(CWnd* pParent)
{
   Create(CPrintingDialog1::IDD, pParent);      // modeless !
   CWnd* pwnd = GetDlgItem(IDCANCEL);
   if (pwnd != NULL)
      pwnd->EnableWindow(FALSE); // Not able to cancel printing (see m_bUserAbort).
   m_bUserAbort = FALSE;
}

CPrintingDialog1::~CPrintingDialog1()
{
}

BOOL CPrintingDialog1::OnInitDialog()
{
   SetWindowText(::AfxGetAppName());
   CenterWindow();
   return CDialog::OnInitDialog();
}

void CPrintingDialog1::OnCancel()
{
   m_bUserAbort = TRUE;  // flag that user aborted print
   CDialog::OnCancel();
}

/////////////////////////////////////////////////////////////////////////////
// _YfxAbortProc (_AfxAbortProc copied from VIEWPRINT.CPP).

static BOOL CALLBACK _YfxAbortProc(HDC, int)
{
      MSG msg;
      while (!CPrintingDialog1::m_bUserAbort && ::PeekMessage(&msg, NULL, NULL, NULL, PM_NOREMOVE))
      {
            if (!AfxGetThread()->PumpMessage())
                  return FALSE;   // terminate if WM_QUIT received
      }
      return !CPrintingDialog1::m_bUserAbort;
}

/////////////////////////////////////////////////////////////////////////////
// CPrintableDlg dialog

/////////////////////////////////////////////////////////////////////////////
// Init - initializes the object's member variables.
/////////////////////////////////////////////////////////////////////////////
void CPrintableDlg::Init()
{
   //{{AFX_DATA_INIT(CPrintableDlg)
      // NOTE: the ClassWizard will add member initialization here
   //}}AFX_DATA_INIT
   m_pbmMemory = NULL;
   m_pMemoryDC = NULL;
   m_rectClipBox = CRect(0, 0, 0, 0);
   m_nMarginBottom = 0;
   m_nMarginRight = 0;
   m_nMarginTop = 0;
   m_nMarginLeft = 0;

   m_pSubMenu = NULL;
   m_pMenu = NULL;
}

/////////////////////////////////////////////////////////////////////////////
// CPrintableDlg - constructor.
/////////////////////////////////////////////////////////////////////////////
CPrintableDlg::CPrintableDlg(LPCTSTR lpszTemplateName, CWnd * pParent)
    : CDialog(lpszTemplateName, pParent)
{
   Init();
}

/////////////////////////////////////////////////////////////////////////////
// CPrintableDlg - constructor.
/////////////////////////////////////////////////////////////////////////////
CPrintableDlg::CPrintableDlg(UINT nIDTemplate, CWnd* pParent /*=NULL*/)
    : CDialog(nIDTemplate, pParent)
{
   Init();
}

/////////////////////////////////////////////////////////////////////////////
// CPrintableDlg - constructor.
/////////////////////////////////////////////////////////////////////////////
CPrintableDlg::CPrintableDlg()
{
   Init();
}

/////////////////////////////////////////////////////////////////////////////
// CPrintableDlg - destructor.
/////////////////////////////////////////////////////////////////////////////
CPrintableDlg::~CPrintableDlg()
{
   delete m_pMemoryDC;
   delete m_pbmMemory;
   delete m_pMenu;
   delete m_pSubMenu;
}

/////////////////////////////////////////////////////////////////////////////
// DoDataExchange - called by the framework to perform data exchange and validation.
/////////////////////////////////////////////////////////////////////////////
void CPrintableDlg::DoDataExchange(CDataExchange* pDX)
{
   CDialog::DoDataExchange(pDX);
   //{{AFX_DATA_MAP(CPrintableDlg)
      // NOTE: the ClassWizard will add DDX and DDV calls here
   //}}AFX_DATA_MAP
}

/////////////////////////////////////////////////////////////////////////////
// CreateMemoryDC - used internally to make a copy of the dialog as a bitmap.
/////////////////////////////////////////////////////////////////////////////
CBitmap* CPrintableDlg::CreateMemoryDC(CDC** ppDC, CBitmap** ppbm)
{
   ASSERT(ppDC != NULL);
   ASSERT(ppbm != NULL);
   ASSERT(*ppDC == NULL);
   ASSERT(*ppbm == NULL);

   CWindowDC WindowDC(this);
   WindowDC.SetMapMode(MM_LOMETRIC);
   
   WindowDC.GetClipBox(m_rectClipBox);
#ifdef _DEBUG
   //afxDump << "m_rectClipBox = " << m_rectClipBox << "\n";
#endif


   CDC* pDC = new CDC();
   if (!pDC->CreateCompatibleDC(&WindowDC))
   {
      TRACE("Failed to create compatible DC\n");
      delete pDC;
      return NULL;
   }

   CBitmap* pbm = new CBitmap();
   if (!pbm->CreateCompatibleBitmap(&WindowDC, m_rectClipBox.Width(), abs(m_rectClipBox.Height())))
   {
      TRACE("Failed to create compatible bitmap\n");
      delete pbm;
      delete pDC;
      return NULL;
   }

   CBitmap* pOldBitmap = pDC->SelectObject(pbm);

   pDC->SetMapMode(MM_LOMETRIC);
   UpdateWindow();
   pDC->BitBlt(0, 0, m_rectClipBox.Width(), m_rectClipBox.Height(), &WindowDC, 0, 0, SRCCOPY); // Make in-memory copy of this dialog.

   *ppbm = pbm;
   *ppDC = pDC;
   return pOldBitmap;
}

/////////////////////////////////////////////////////////////////////////////
// DeleteMemoryDC - used internally to free the resources allocated in CreateMemoryDC.
/////////////////////////////////////////////////////////////////////////////
void CPrintableDlg::DeleteMemoryDC(CDC** ppDC, CBitmap** ppbm, CBitmap* pOldBitmap)
{
   ASSERT(ppDC != NULL);
   ASSERT(ppbm != NULL);

   if (pOldBitmap != NULL && *ppDC != NULL)
      (*ppDC)->SelectObject(pOldBitmap);

   delete *ppDC;
   delete *ppbm;
   *ppDC = NULL;
   *ppbm = NULL;
}

BEGIN_MESSAGE_MAP(CPrintableDlg, CDialog)
   //{{AFX_MSG_MAP(CPrintableDlg)
      ON_WM_CREATE()
      //}}AFX_MSG_MAP
   ON_COMMAND(ID_FILE_PRINT, OnFilePrint)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPrintableDlg message handlers

/////////////////////////////////////////////////////////////////////////////
// DoPreparePrinting - do always call this function from OnPreparePrinting.
/////////////////////////////////////////////////////////////////////////////
BOOL CPrintableDlg::DoPreparePrinting(CPrintInfo * pInfo)
{
   ASSERT(pInfo != NULL);
   ASSERT_VALID(pInfo->m_pPD);

   CWinApp* pApp = ::AfxGetApp();

   pInfo->m_pPD->m_pd.nFromPage = (WORD)pInfo->GetMinPage();
   pInfo->m_pPD->m_pd.nToPage = (WORD)pInfo->GetMaxPage();

   if (pApp->DoPrintDialog(pInfo->m_pPD) != IDOK)
      return FALSE;       // do not print

   ASSERT(pInfo->m_pPD != NULL);
   ASSERT(pInfo->m_pPD->m_pd.hDC != NULL);
   if (pInfo->m_pPD->m_pd.hDC == NULL)
      return FALSE;

   pInfo->m_nNumPreviewPages = pApp->m_nNumPreviewPages;
   VERIFY(pInfo->m_strPageDesc.LoadString(AFX_IDS_PREVIEWPAGEDESC));
   return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// OnBeginPrinting - called from OnFilePrint at the beginning of a print job.
/////////////////////////////////////////////////////////////////////////////
void CPrintableDlg::OnBeginPrinting(CDC * pDC, CPrintInfo * pInfo)
{
   const int knHorzSize = 10 * pDC->GetDeviceCaps(HORZSIZE); // MM_LOMETRIC (0.1 mm).
   const int knVertSize = 10 * pDC->GetDeviceCaps(VERTSIZE); // MM_LOMETRIC (0.1 mm).
   //TRACE("HORZSIZE = %d mm, VERTSIZE = %d mm\n", knHorzSize/10, knVertSize/10);

   SetMargins(200, 100, 200, 100); // MM_LOMETRIC (0.1 mm).

   const int knPrintWidth = knHorzSize - m_nMarginLeft - m_nMarginRight;
   const int knPrintHeight = knVertSize - m_nMarginTop - m_nMarginBottom;
   const int knMaxPage = (abs(m_rectClipBox.Width())/knPrintWidth + 1) *
                         (abs(m_rectClipBox.Height())/knPrintHeight + 1);
   //TRACE("Max Page = %d\n", knMaxPage);
   ASSERT(knPrintWidth > 0 && knPrintHeight > 0 && knMaxPage > 0);
   pInfo->SetMaxPage(knMaxPage);
}

/////////////////////////////////////////////////////////////////////////////
// OnEndPrinting - called from OnFilePrint after the dialog has been printed.
/////////////////////////////////////////////////////////////////////////////
void CPrintableDlg::OnEndPrinting(CDC * pDC, CPrintInfo * pInfo)
{
   UNUSED(pDC);
   UNUSED(pInfo);
}

/////////////////////////////////////////////////////////////////////////////
// OnEndPrintPreview - not implemented (yet).
/////////////////////////////////////////////////////////////////////////////
void CPrintableDlg::OnEndPrintPreview(CDC * pDC, CPrintInfo * pInfo, POINT point, CPreviewView * pView)
{
   UNUSED(pDC);
   UNUSED(pInfo);
   UNUSED(point);
   UNUSED(pView);
}

/////////////////////////////////////////////////////////////////////////////
// OnPrepareDC - called from OnFilePrint before the OnPrint member function is
//               called for each page.
/////////////////////////////////////////////////////////////////////////////
void CPrintableDlg::OnPrepareDC(CDC * pDC, CPrintInfo * pInfo)
{
   ASSERT_VALID(pDC);
   UNUSED(pDC); // unused in release builds

   // Default to one page printing if doc length not known
   if (pInfo != NULL)
      pInfo->m_bContinuePrinting = (pInfo->GetMaxPage() != 0xffff || (pInfo->m_nCurPage == 1));
}

/////////////////////////////////////////////////////////////////////////////
// OnPreparePrinting - called from OnFilePrint before the dialog is printed.
/////////////////////////////////////////////////////////////////////////////
BOOL CPrintableDlg::OnPreparePrinting(CPrintInfo * pInfo)
{
   return DoPreparePrinting(pInfo);
}

/////////////////////////////////////////////////////////////////////////////
// OnPrint - called from OnFilePrint to print a page of the dialog.
/////////////////////////////////////////////////////////////////////////////
void CPrintableDlg::OnPrint(CDC * pDC, CPrintInfo * pInfo)
{
   ASSERT(pDC != NULL);
   ASSERT(pInfo != NULL);
   ASSERT(m_pMemoryDC != NULL);

   const int knHorzSize = 10 * pDC->GetDeviceCaps(HORZSIZE); // MM_LOMETRIC (0.1 mm).
   const int knVertSize = 10 * pDC->GetDeviceCaps(VERTSIZE); // MM_LOMETRIC (0.1 mm).

   pDC->SetMapMode(MM_LOMETRIC);

   const int knPrintWidth = knHorzSize - m_nMarginLeft - m_nMarginRight;
   const int knPrintHeight = knVertSize - m_nMarginTop - m_nMarginBottom;
   const int knMaxPageX = (abs(m_rectClipBox.Width())/knPrintWidth + 1);
   const int knMaxPageY = (abs(m_rectClipBox.Height())/knPrintHeight + 1);

   ASSERT(knPrintWidth > 0 && knPrintHeight > 0);

   int x = m_nMarginLeft;
   int y = -m_nMarginTop;
   int nWidth = knPrintWidth;
   int nHeight = -knPrintHeight;
   int xSrc = ((pInfo->m_nCurPage-1) % knMaxPageX) * knPrintWidth;
   int ySrc = ((pInfo->m_nCurPage-1) / knMaxPageX) * knPrintHeight;
   ySrc = -ySrc;

   if (abs(m_rectClipBox.Width() - xSrc) < abs(nWidth))
      nWidth = m_rectClipBox.Width() - xSrc;
   if (abs(m_rectClipBox.Height() - ySrc) < abs(nHeight))
      nHeight = m_rectClipBox.Height() - ySrc;

   //TRACE("pDC->BitBlt(x = %d, y = %d, nWidth = %d, nHeight = %d, m_pMemoryDC, xSrc = %d, ySrc = %d, SRCCOPY);\n", x, y, nWidth, nHeight, xSrc, ySrc);
   pDC->BitBlt(x, y, nWidth, nHeight, m_pMemoryDC, xSrc, ySrc, SRCCOPY);
}

/////////////////////////////////////////////////////////////////////////////
// OnFilePrint - message handler for the WM_COMMAND message, where LOWORD(wParam) = ID_FILE_PRINT.
/////////////////////////////////////////////////////////////////////////////
void CPrintableDlg::OnFilePrint()
{
   TRACE("void CPrintableDlg::OnFilePrint()\n");

   CBitmap* pOldBitmap = CreateMemoryDC(&m_pMemoryDC, &m_pbmMemory); // MM_LOMETRIC mapping mode is preset.
   if (NULL == pOldBitmap)
      return;

   CPrintInfo printInfo;
   ASSERT(printInfo.m_pPD != NULL); // must be set

   if (LOWORD(GetCurrentMessage()->wParam) == ID_FILE_PRINT_DIRECT)
   {
      CCommandLineInfo* pCmdInfo = AfxGetApp()->m_pCmdInfo;

      if (pCmdInfo != NULL)
      {
         if (pCmdInfo->m_nShellCommand == CCommandLineInfo::FilePrintTo)
         {
            printInfo.m_pPD->m_pd.hDC = ::CreateDC(pCmdInfo->m_strDriverName,
               pCmdInfo->m_strPrinterName, pCmdInfo->m_strPortName, NULL);
            if (printInfo.m_pPD->m_pd.hDC == NULL)
            {
               DeleteMemoryDC(&m_pMemoryDC, &m_pbmMemory, pOldBitmap);
               ::AfxMessageBox(AFX_IDP_FAILED_TO_START_PRINT);
               return;
            }
         }
      }

      printInfo.m_bDirect = TRUE;
   }

   if (!OnPreparePrinting(&printInfo))
   {
      DeleteMemoryDC(&m_pMemoryDC, &m_pbmMemory, pOldBitmap);
      return;
   }

   // hDC must be set (did you remember to call DoPreparePrinting?)
   ASSERT(printInfo.m_pPD->m_pd.hDC != NULL);

   CString strTitle;
   GetWindowText(strTitle);

   DOCINFO docInfo = { 0 };
   docInfo.cbSize = sizeof(DOCINFO);
   docInfo.lpszDocName = strTitle;
   docInfo.lpszOutput = NULL;

   CString strPortName = printInfo.m_pPD->GetPortName();
   int nFormatID = AFX_IDS_PRINTONPORT;

   // setup the printing DC
   CDC dcPrint;
   dcPrint.Attach(printInfo.m_pPD->m_pd.hDC);  // attach printer dc
   dcPrint.m_bPrinting = TRUE;
   OnBeginPrinting(&dcPrint, &printInfo);
   dcPrint.SetAbortProc(_YfxAbortProc);

   CPrintingDialog1 dlgPrintStatus(this); // Creates a modeless dialog

   CString strTemp;
   dlgPrintStatus.SetDlgItemText(AFX_IDC_PRINT_DOCNAME, strTitle);
   dlgPrintStatus.SetDlgItemText(AFX_IDC_PRINT_PRINTERNAME, printInfo.m_pPD->GetDeviceName());
   ::AfxFormatString1(strTemp, nFormatID, strPortName);
   dlgPrintStatus.SetDlgItemText(AFX_IDC_PRINT_PORTNAME, strTemp);
   dlgPrintStatus.ShowWindow(SW_SHOW);
   dlgPrintStatus.UpdateWindow();

   // start document printing process
   if (dcPrint.StartDoc(&docInfo) == SP_ERROR)
   {
      // cleanup and show error message
      OnEndPrinting(&dcPrint, &printInfo);
      DeleteMemoryDC(&m_pMemoryDC, &m_pbmMemory, pOldBitmap);
      dlgPrintStatus.DestroyWindow();
      dcPrint.Detach();   // will be cleaned up by CPrintInfo destructor
      ::AfxMessageBox(AFX_IDP_FAILED_TO_START_PRINT);
      return;
   }

   // Guarantee values are in the valid range
   UINT nEndPage = printInfo.GetToPage();
   UINT nStartPage = printInfo.GetFromPage();

   if (nEndPage < printInfo.GetMinPage())
      nEndPage = printInfo.GetMinPage();
   if (nEndPage > printInfo.GetMaxPage())
      nEndPage = printInfo.GetMaxPage();

   if (nStartPage < printInfo.GetMinPage())
      nStartPage = printInfo.GetMinPage();
   if (nStartPage > printInfo.GetMaxPage())
      nStartPage = printInfo.GetMaxPage();

   int nStep = (nEndPage >= nStartPage) ? 1 : -1;
   nEndPage = (nEndPage == 0xffff) ? 0xffff : nEndPage + nStep;

   VERIFY(strTemp.LoadString(AFX_IDS_PRINTPAGENUM));

   // begin page printing loop
   BOOL bError = FALSE;
   for (printInfo.m_nCurPage = nStartPage; printInfo.m_nCurPage != nEndPage; printInfo.m_nCurPage += nStep)
   {
      OnPrepareDC(&dcPrint, &printInfo);

      // check for end of print
      if (!printInfo.m_bContinuePrinting)
         break;

      // write current page
      TCHAR szBuf[80] = { 0 };
      ::wsprintf(szBuf, strTemp, printInfo.m_nCurPage);
      dlgPrintStatus.SetDlgItemText(AFX_IDC_PRINT_PAGENUM, szBuf);

      // set up drawing rect to entire page (in logical coordinates)
      printInfo.m_rectDraw.SetRect(0, 0, dcPrint.GetDeviceCaps(HORZRES), dcPrint.GetDeviceCaps(VERTRES));
      dcPrint.DPtoLP(&printInfo.m_rectDraw);

      // attempt to start the current page
      if (dcPrint.StartPage() < 0)
      {
         bError = TRUE;
         break;
      }

      // must call OnPrepareDC on newer versions of Windows because
      // StartPage now resets the device attributes.
      OnPrepareDC(&dcPrint, &printInfo);

      ASSERT(printInfo.m_bContinuePrinting);

      // page successfully started, so now render the page
      OnPrint(&dcPrint, &printInfo);
      dlgPrintStatus.UpdateWindow();
      if (dcPrint.EndPage() < 0 || !_YfxAbortProc(dcPrint.m_hDC, 0))
      {
         bError = TRUE;
         break;
      }
   }

   // cleanup document printing process
   if (!bError)
      dcPrint.EndDoc();
   else
      dcPrint.AbortDoc();

   OnEndPrinting(&dcPrint, &printInfo);    // clean up after printing
   DeleteMemoryDC(&m_pMemoryDC, &m_pbmMemory, pOldBitmap);
   dlgPrintStatus.DestroyWindow();

   dcPrint.Detach();   // will be cleaned up by CPrintInfo destructor
}

/////////////////////////////////////////////////////////////////////////////
// OnCreate - message handler for the WM_CREATE message.
/////////////////////////////////////////////////////////////////////////////
int CPrintableDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
   if (CDialog::OnCreate(lpCreateStruct) == -1)
      return -1;

   CString strMenuItemFile;
   CString strMenuItemPrint;
   strMenuItemFile.LoadString(YFX_IDS_MENUITEM_FILE);
   strMenuItemPrint.LoadString(YFX_IDS_MENUITEM_FILE_PRINT);

   CMenu* pMenu = GetMenu(); // Get current menu.
   if (NULL == pMenu)
   {
      // Current menu doesn't exist. Create a new one.

      CRect rectDlgBefore(0, 0, 0, 0);
      GetClientRect(rectDlgBefore); // Save size of client area before adding new menu.

      CRuntimeClass* pRC = RUNTIME_CLASS(CMenu);
      ASSERT(pRC != NULL);
      m_pMenu = (CMenu*) pRC->CreateObject();
      ASSERT(m_pMenu != NULL);
      m_pSubMenu = (CMenu*) pRC->CreateObject();
      ASSERT(m_pSubMenu != NULL);
      if (!m_pMenu->CreateMenu() || !m_pSubMenu->CreatePopupMenu())
      {
         return -1;
      }
      if (!m_pSubMenu->AppendMenu(MF_ENABLED | MF_STRING, ID_FILE_PRINT, strMenuItemPrint) ||
          !m_pMenu->AppendMenu(MF_ENABLED | MF_POPUP | MF_STRING, UINT(m_pSubMenu->m_hMenu), strMenuItemFile) ||
          !SetMenu(m_pMenu))
      {
         return -1;
      }

      CRect rectDlgAfter(0, 0, 0, 0);
      GetClientRect(rectDlgAfter); // Get size of client area after adding new menu.

      // Adjust window size for dialog box to make room for new menu.

      int dy = abs(rectDlgBefore.Height() - rectDlgAfter.Height());
      MoveWindow(lpCreateStruct->x, lpCreateStruct->y, lpCreateStruct->cx, lpCreateStruct->cy + dy);
   }
   else
   {
      // Current menu exist. Add new "Print" menu item to it.

      int nMenuItemCount = (int) pMenu->GetMenuItemCount();
      ASSERT(nMenuItemCount != -1);
      ASSERT(nMenuItemCount != 0);
      CMenu* pSubMenu = pMenu->GetSubMenu(nMenuItemCount-1);
      if (!pSubMenu->AppendMenu(MF_ENABLED | MF_STRING, ID_FILE_PRINT, strMenuItemPrint))
      {
         return -1;
      }
   }

   DrawMenuBar(); // Redraw the menu bar.

   return 0;
}

Just create your dialog the ususal way, and replace all references to CDialog with CPrintableDialog the same way as for a CFormView. Then select the menu command on the dialog to print.
0
 
LVL 1

Author Comment

by:The_Brain
Comment Utility
This is very nice of you to provide code for me, But still how would I wirte it in code...

(I have created a project weeks ago, I just wan't  to print the stuff on the View and Dialog (sep).

I have OnPreparePrinting(#@$!#@!#$)
and Other calls of prinitng.

it displays the print preview dialog automaitcally but I don't see anyhting on the pages.  At home it prints but it prints nothing.

Iam using CDC calls.

OnBeginPrinting(CDC* pDoc, CPrintInfo* pInfo)
{
   pDC->StartDoc("CTimeTrackDoc");
   //calls OnPrepareDC//
   pDC->StartPage();
   pDC->EndPage();
   pDC->EndDoc();
What am I doing wrong?
}
0
 
LVL 3

Expert Comment

by:stefanr
Comment Utility
CFormView have the code to show the printing dialog, but it doesn't have code to actually print the contents in the dialog. The easy way (which I have done here) is to create a new base class that simply dumps the visible part of the form view to a printer. The more difficult way (that can print parts not visible too) is to do all the printing yourself. That would mean that you have to provide code to print each and every control in the view. That is unique to each view imaginable.
To dump the view, I create a memory DC to make a copy of the view's content (CreateMemoryDC). OnBeginPrinting is used only to calculate how many pages the dump needs. You don't have to call pDC->StartDoc, since that is done in OnFilePrint. It also calls pDC->StartPage and pDC->EndPage for each page. It is the OnPrint function, called from OnFilePrint, that does the actual printing of a page. In my code it simply copies the data in the bitmap created in CreateMemoryDC to the printer, thus printing the bitmap that is a copy of the view or dialog as it looked like when the printing process was started. You actually don't have to override anything if you use the CPrintableView/CPrintableDlg as it looks to print a dump of it.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 1

Author Comment

by:The_Brain
Comment Utility
Ok cool but now here is the part that I am MOST interested in.
How can I use the Header and CPP files in my program? do I include them and call the functions?
say
#include "thatfileyougave.h"
#include "thatfileyougave.cpp"

later
PrintDialog(CMydialog)??? I need to know if I can include it and use it, or did I have I have to start with the "printable View" From the start?? sorry to be so annoying but I need to know HOW I can use it.  I understand what it does, But now I am not sure about the functions...
Thanks...

to sum up...
I want to call some function say
printsomething(TheCurrentVisible View);
and it must print it...
Is this what it does or Am I mistaken. (if not, could you do something like that for me.  I will be your friend for life if you do... :^) and I will try to increase the points as I get them 5 at a time.
0
 
LVL 3

Expert Comment

by:stefanr
Comment Utility
To print, call OnFilePrint() for both classes. That is the function called when the menu item Print is called. It generates a WM_COMMAND message, where LOWORD(wParam) == ID_FILE_PRINT.

The Printable*.cpp file(s) are included into the project (under the Source Files folder) and the include file(s) are #included in the header file(s) of your derived CFormView/CDialog class(es). Then you use the search and replace function on your header and source file(s) to replace all references to CFormView to CPrintableFormView, and CDialog to CPrintableDlg.

It should now look something like this:

//**************************H************************************

#include "PrintableDlg.h"

class CYourDerivedDlg : public CPrintableDlg
{
// Construction
public:
      CYourDerivedDlg(CWnd* pParent = NULL);   // standard constructor

// Dialog Data
      //{{AFX_DATA(CYourDerivedDlg)
      enum { IDD = IDD_DERIVED };
      //}}AFX_DATA


// Overrides
      // ClassWizard generated virtual function overrides
      //{{AFX_VIRTUAL(CYourDerivedDlg)
      protected:
      virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
      //}}AFX_VIRTUAL

// Implementation
protected:

      // Generated message map functions
      //{{AFX_MSG(CYourDerivedDlg)
      virtual BOOL OnInitDialog();
      //}}AFX_MSG
      DECLARE_MESSAGE_MAP()
};

//**************************CPP**********************************
CYourDerivedDlg::CYourDerivedDlg(CWnd* pParent /*=NULL*/)
      : CPrintableDlg(CYourDerivedDlg::IDD, pParent)
{
      //{{AFX_DATA_INIT(CYourDerivedDlg)
      //}}AFX_DATA_INIT
}


void CYourDerivedDlg::DoDataExchange(CDataExchange* pDX)
{
      CPrintableDlg::DoDataExchange(pDX);
      //{{AFX_DATA_MAP(CYourDerivedDlg)
      //}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CYourDerivedDlg, CPrintableDlg)
      //{{AFX_MSG_MAP(CYourDerivedDlg)
      //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CYourDerivedDlg message handlers

BOOL CYourDerivedDlg::OnInitDialog()
{
      CPrintableDlg::OnInitDialog();
      
      // TODO: Add extra initialization here
      
      return TRUE;  // return TRUE unless you set the focus to a control
                    // EXCEPTION: OCX Property Pages should return FALSE
}

If you have created a dialog (i.e. CYourDerivedDlg dlg;), then you can initiate printing anytime by calling dlg.OnFilePrint();
You can also initiate printing by selecting the Print command on the dialog's menu (unless you decide to remove the code that inserts that in OnCreate). The procedure is similar for the CPrintableFormView, to initiate printing you call OnFilePrint() for the view object. The AppWizard has also inserted code that calls that function when you have the view active and selects File:Print in the main menu.

If you want a global function that calls the OnFilePrint function, then you create a pair of (inline) functions, like this:

inline void PrintIt(CPrintableDlg* pDlg) { pDlg->OnFilePrint(); }
inline void PrintIt(CView* pView) { pView->OnFilePrint(); }

Since they are inlines, they should be put in a common header.
0
 
LVL 1

Author Comment

by:The_Brain
Comment Utility
Ok, so let me get just one thing straight.

I include the two files. (4 including Dlg)

and I just call OnFilePrint()?
Is that what you are telling me?
(I am very nervous about your derived story, does this mean that I have to start all over again?  Sorry if I am really bugging you about this but this is essential. (I am trying all sorts of thing to get it to work... no luck.)

Ok, one last sum up.

I need to know if I can indepently just include these extra files and call what functions.

that is it. (I don't reallllly need all the code, I just need to know what functions to use. and is it like I described above, is it independent?
thanks for all the trouble.
0
 
LVL 1

Author Comment

by:The_Brain
Comment Utility
Ok, so let me get just one thing straight.

I include the two files. (4 including Dlg)

and I just call OnFilePrint()?
Is that what you are telling me?
(I am very nervous about your derived story, does this mean that I have to start all over again?  Sorry if I am really bugging you about this but this is essential. (I am trying all sorts of thing to get it to work... no luck.)

Ok, one last sum up.

I need to know if I can indepently just include these extra files and call what functions.

that is it. (I don't reallllly need all the code, I just need to know what functions to use. and is it like I described above, is it independent?
thanks for all the trouble.

the last option is to send my program to you 300K (zip) and you can see if you get it to print... is that ok if your next comment doesn't help me?  Please do send a suggestion, but if That doesn't help, well (could you leave your e-mail address on your next comment, I will send my program pronto. thanks again
0
 
LVL 3

Expert Comment

by:stefanr
Comment Utility
The classes are made to work exactly like the standard CFormView and CDialog, with the addition of the printing (dumping) code. You really don't have to change any code that you already have written, just change the reference to CFormView and CDialog using search and replace. You can also use ClassWizard just as usual. OnFilePrint is the standard function that displays the standard printing dialog, and then prints if OK was selected.
You could also put the code into the classes directly, but then you have to do so every time you need a printable dialog or form view. By making them a base class you can easily use them over and over again, as soon you need to print them.

If that is not clear enough, you can mail to stefanr@ydab.se, I try to see what can be done. Don't forget the return mail address in that case.
0
 
LVL 1

Author Comment

by:The_Brain
Comment Utility
Oh well, I never got it working, but I am gonna give you the points, since I can ask you at a later stage what I am missing...
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now