ON_MESSAGE(WM_PRINT, OnPrint)...

LRESULT CMyRichEdit::OnPrint(WPARAM wParam, LPARAM lParam)
{
if(GetCompBase()->IsPrintProtect())
{
return 0;
}
UpdateData();

CDC* pDC = CDC::FromHandle((HDC)wParam);

CRect rUIP;
GetClientRect(&rUIP);
pDC->FillSolidRect(rUIP,m_compBase.GetBkColor());

CString str6Text;
GetWindowText(str6Text);

if(str6Text.GetLength())
{
if(! m_compCommon.GetMultiLine())
{

CSize Extent = pDC->GetTextExtent(str6Text);
int int6Text =rUIP.CenterPoint().y;

rUIP.top =int6Text - Extent.cy/2;
rUIP.bottom =int6Text + Extent.cy/2;
}

int nSaveDC = pDC->SaveDC();

// This code basically taken from MS KB article Q129860
FORMATRANGE frIP;
double nHorizRes = rUIP.Width();
double nVertRes = rUIP.Height();
double nLogPixelsX = GetDeviceCaps((HDC)wParam, LOGPIXELSX);
double nLogPixelsY = GetDeviceCaps((HDC)wParam, LOGPIXELSY);

// Ensure the printer DC is in MM_TEXT mode.
SetMapMode ( (HDC)wParam, MM_TEXT );

// Rendering to the same DC we are measuring.
ZeroMemory(&frIP, sizeof(frIP));
frIP.hdc = frIP.hdcTarget = (HDC)wParam;

// Set up the page (convert 0.01mm to twips)
frIP.rcPage.left = 0;
frIP.rcPage.top = (rUIP.top/nLogPixelsX) * 1440;
frIP.rcPage.right = (nHorizRes/nLogPixelsX) * 1440;
frIP.rcPage.bottom = (rUIP.bottom/nLogPixelsY) * 1440;

// Set up 0" margins all around.
frIP.rc.left = frIP.rcPage.left ;//+ 1440; // 1440 TWIPS = 1 inch.
frIP.rc.top = frIP.rcPage.top ;//+ 1440;
frIP.rc.right = frIP.rcPage.right ;//- 1440;
frIP.rc.bottom = frIP.rcPage.bottom ;//- 1440;

// Default the range of text to print as the entire document.
frIP.chrg.cpMin = CharFromPos(CPoint(0,0));
frIP.chrg.cpMax = CharFromPos(CPoint(nHorizRes,nVertRes));
if(frIP.chrg.cpMax ==0 ) frIP.chrg.cpMax =-1;

FormatRange(&frIP,true);

// Print as much text as can fit on a page. The return value is
// the index of the first character on the next page. Using TRUE
// for the wParam parameter causes the text to be printed.
FormatRange(&frIP,true);
DisplayBand(&frIP.rc );

// Tell the control to release cached information.
FormatRange(NULL,false);
pDC->RestoreDC(nSaveDC);
}

GetClientRect(&rUIP);
rUIP.InflateRect(m_nFrameWidth, m_nFrameWidth);

if(m_compBase.IsFlat())
{
int nWidth = m_compBorder.GetBorderWidth();

CPen penBorder(PS_NULL,0,RGB(0,0,0));
CPen* pOldPen = pDC->SelectObject(&penBorder);
CBrush brushBk(m_compBase.GetBkColor());
CBrush* pOldBrush = pDC->SelectObject(&brushBk);

pDC->Rectangle(rUIP.left,rUIP.top,rUIP.right+1,rUIP.top+nWidth+1);
pDC->Rectangle(rUIP.right-nWidth,rUIP.top,rUIP.right+1,rUIP.bottom+1);
pDC->Rectangle(rUIP.left,rUIP.bottom-nWidth,rUIP.right+1,rUIP.bottom+1);
pDC->Rectangle(rUIP.left,rUIP.top,rUIP.left+nWidth+1,rUIP.bottom+1);

CBrush brushBorder(m_compBorder.GetBorderColor());
pDC->SelectObject(&brushBorder);
CRect rcTotal = rUIP;

if ( m_compBorder.GetBorderStyle() == BORDERSTYLETYPE_FLAT )
{
if (m_compBorder.IsHaveBorderTop() )
{
pDC->Rectangle(rUIP.left,rUIP.top,rUIP.right+1,rUIP.top+nWidth+1);
}
if ( m_compBorder.IsHaveBorderRight() )
{
pDC->Rectangle(rUIP.right-nWidth,rUIP.top,rUIP.right+1,rUIP.bottom+1);
}
if (m_compBorder.IsHaveBorderDown() )
{
pDC->Rectangle(rUIP.left,rUIP.bottom-nWidth,rUIP.right+1,rUIP.bottom+1);
}
if (m_compBorder.IsHaveBorderLeft() )
{
pDC->Rectangle(rUIP.left,rUIP.top,rUIP.left+nWidth+1,rUIP.bottom+1);
}
}

pDC->SelectObject(pOldPen);
pDC->SelectObject(pOldBrush);
}
else
{
CRect rcTotal = rUIP;

if ( m_compBorder.GetBorderStyle() == BORDERSTYLETYPE_SUNKEN )
{
pDC->Draw3dRect(rcTotal,RGB(128,128,128),RGB(255,255,255));
rcTotal.InflateRect(-1,-1,-1,-1);
pDC->Draw3dRect(rcTotal,RGB(0,0,0),gfxData.m_crBtnFace);
}
else
{
pDC->Draw3dRect(rcTotal,RGB(255,255,255),RGB(0,0,0));
rcTotal.InflateRect(-1,-1,-1,-1);
pDC->Draw3dRect(rcTotal,gfxData.m_crBtnFace,RGB(128,128,128));
}
}
return 0;
}