Code:
#ifndef PRINTING_H
#define PRINTING_H
class CPrinting
{
public:
CPrinting();
~CPrinting();
bool DoPageSetup(HWND hParenWindow);
bool DoPrint(HINSTANCE hInstance, HWND hParentWindow, LPSTR pszTitleName, int iEditControlID, int iPrintDialogID, int iFileNameID);
private:
static BOOL CALLBACK PaintHook(HWND hwnd, UINT messages, WPARAM wParam, LPARAM lParam);
static BOOL CALLBACK AbortProcedure(HDC hDC, int iCode);
static BOOL CALLBACK PrintDialogProcedure(HWND hwnd, UINT messages, WPARAM wParam, LPARAM lParam);
PAGESETUPDLG m_psd;
PRINTDLG m_pd;
DOCINFO m_di;
TEXTMETRIC m_tm;
LPSTR m_pszBuffer;
char m_szJobName[MAX_PATH + 64];
HWND m_hPrintDialog;
bool m_bUserAbort;
int m_iHorzRes;
int m_iVertRes;
int m_iChar;
int m_iCharsPerLine;
int m_iLinesPerPage;
int m_iTotalLines;
int m_iTotalPages;
int m_iLineNumber;
};
#endif // PRINTING_H
Code:
#include <windows.h>
#include "printing.h"
CPrinting::CPrinting()
{
}
CPrinting::~CPrinting()
{
}
bool CPrinting::DoPageSetup(HWND hParentWindow)
{
ZeroMemory(&m_psd, sizeof(m_psd));
m_psd.lStructSize = sizeof(m_psd);
m_psd.hwndOwner = hParentWindow;
m_psd.hDevMode = NULL;
m_psd.hDevNames = NULL;
m_psd.Flags = PSD_INTHOUSANDTHSOFINCHES | PSD_MARGINS | PSD_ENABLEPAGEPAINTHOOK;
m_psd.rtMargin.top = 1000;
m_psd.rtMargin.left = 1250;
m_psd.rtMargin.right = 1250;
m_psd.rtMargin.bottom = 1000;
m_psd.lpfnPagePaintHook = (LPPAGEPAINTHOOK)PaintHook;
if(!PageSetupDlg(&m_psd))
{
return false;
}
return true;
}
BOOL CALLBACK CPrinting::PaintHook(HWND hwnd, UINT messages, WPARAM wParam, LPARAM lParam)
{
switch(messages)
{
case WM_PSD_MARGINRECT:
{
LPRECT lprc;
COLORREF crMarginRect;
HDC hDC, hDCOld;
hDC = (HDC)wParam;
lprc = (LPRECT)lParam;
crMarginRect = GetSysColor(COLOR_HIGHLIGHT);
hDCOld = static_cast<HDC>(SelectObject(hDC, CreatePen(PS_DASHDOT, static_cast<int>(.5), crMarginRect)));
Rectangle(hDC, lprc->left, lprc->top, lprc->right, lprc->bottom);
SelectObject(hDC, hDCOld);
return true;
}
break;
default:
return false;
}
return true;
}
bool CPrinting::DoPrint(HINSTANCE hInstance, HWND hParentWindow, LPSTR pszTitleName, int iEditControlID, int iPrintDialogID, int iFileNameID)
{
ZeroMemory(&m_pd, sizeof(m_pd));
m_pd.lStructSize = sizeof(m_pd);
m_pd.hwndOwner = hParentWindow;
m_pd.hDevMode = NULL;
m_pd.hDevNames = NULL;
m_pd.Flags = PD_USEDEVMODECOPIESANDCOLLATE | PD_RETURNDC;
m_pd.nCopies = 1;
m_pd.nFromPage = 1;
m_pd.nToPage = 0xFFFFFFFF;
m_pd.nMinPage = 1;
m_pd.nMaxPage = 0xFFFFFFFF;
if(!PrintDlg(&m_pd))
{
return false;
}
GetWindowText(hParentWindow, m_szJobName, sizeof(m_szJobName));
ZeroMemory(&m_di, sizeof(m_di));
m_di.cbSize = sizeof(m_di);
m_di.lpszDocName = m_szJobName;
GetTextMetrics(m_pd.hDC, &m_tm);
m_iHorzRes = GetDeviceCaps(m_pd.hDC, HORZRES);
m_iVertRes = GetDeviceCaps(m_pd.hDC, VERTRES);
m_iChar = m_tm.tmHeight + m_tm.tmExternalLeading;
m_iCharsPerLine = m_iHorzRes / m_tm.tmAveCharWidth;
m_iLinesPerPage = m_iVertRes / m_iChar;
m_iTotalLines = SendDlgItemMessage(hParentWindow, iEditControlID, EM_GETLINECOUNT, 0, 0);
m_iTotalPages = (m_iLinesPerPage + m_iTotalLines - 1) / m_iLinesPerPage;
m_pszBuffer = new char[sizeof(char) * m_iCharsPerLine + 1];
EnableWindow(hParentWindow, false);
m_hPrintDialog = CreateDialog(hInstance,
MAKEINTRESOURCE(iPrintDialogID),
hParentWindow,
PrintDialogProcedure);
SetDlgItemText(m_hPrintDialog, iFileNameID, pszTitleName);
SetAbortProc(m_pd.hDC, AbortProcedure);
if(StartDoc(m_pd.hDC, &m_di) > 0)
{
for(int iColCopy = 0;
iColCopy < ((m_pd.Flags & PD_COLLATE) ? m_pd.nCopies : 1);
iColCopy++)
{
for(int iPage = 0; iPage < m_iTotalPages; iPage++)
{
for(int iNoColCopy = 0;
iNoColCopy < ((m_pd.Flags & PD_COLLATE) ? 1 : m_pd.nCopies);
iNoColCopy++)
{
if(StartPage(m_pd.hDC) < 0)
{
return false;
break;
}
for(int iLine = 0; iLine < m_iLinesPerPage; iLine++)
{
m_iLineNumber = m_iLinesPerPage * iPage + iLine;
*(int *)m_pszBuffer = m_iCharsPerLine;
TextOut(m_pd.hDC, 0, m_iChar * iLine, m_pszBuffer,
static_cast<int>(SendDlgItemMessage(hParentWindow, iEditControlID, EM_GETLINE,
static_cast<WPARAM>(m_iLineNumber), (LPARAM)m_pszBuffer)));
}
if(EndPage(m_pd.hDC) < 0)
{
return false;
break;
}
if(m_bUserAbort)
{
break;
}
}
if(false || !m_bUserAbort)
{
break;
}
}
if(false || !m_bUserAbort)
{
break;
}
}
}
if(!m_bUserAbort)
{
EnableWindow(hParentWindow, true);
}
delete[] m_pszBuffer;
DeleteDC(m_pd.hDC);
return true && !m_bUserAbort;
}
BOOL CALLBACK CPrinting::AbortProcedure(HDC hDC, int iCode)
{
MSG messages;
while(!m_bUserAbort && PeekMessage(&messages, NULL, 0, 0, PM_REMOVE))
{
if(!m_hPrintDialog || IsDialogMessage(m_hPrintDialog, &messages))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
}
return !m_bUserAbort;
}
BOOL CALLBACK CPrinting::PrintDialogProcedure(HWND hwnd, UINT messages, WPARAM wParam, LPARAM lParam)
{
switch(messages)
{
case WM_INITDIALOG:
EnableMenuItem(GetSystemMenu(hwnd, false), SC_CLOSE, MF_GRAYED);
return true;
case WM_COMMAND:
{
m_bUserAbort = true;
EnableWindow(GetParent(hwnd), true);
DestroyWindow(hwnd);
m_hPrintDialog = 0;
}
return true;
}
return false;
}
Compile log: