Problem with OnPrint in MFC
Greetings.
I have the problem that my OnPrint-methode don't print my Bitmap. It's shown in the printpreview, but not printed. I didn't see the fault, cause it goes well. Without changing anything now it isn't printing the picture.....
Thx for help
Puppet
_____________________________________________________________________________-
Here the source:
void COutput2::OnPrint(CDC* pDC, CPrintInfo* pInfo)
{
int nSavedDC = pDC->SaveDC();
// Fussnoten setzen
CFont* pOldFont = pDC->SelectObject(&m_fontSet);
pDC->SetTextAlign(TA_LEFT|TA_BASELINE);
pDC->TextOut(HORZ_MARGIN,VERT_MARGIN,srkChange.strStatus);
// Tree initialisieren
CTreeCtrl& Tree = m_TreeWnd.GetTreeCtrl();
HTREEITEM hItem=Tree.GetRootItem();
int maxItemCnt = (prntY-VERT_MARGIN*2)/m_nRowHeight-2;
// Position im Tree ermitteln
for(int pos=0; posm_nCurPage-1); pos++)
{
Tree.Expand(hItem,TVE_EXPAND);
hItem = Tree.GetNextItem(hItem,TVGN_NEXTVISIBLE);
}
// Bildquelle übergeben
bm.LoadBitmap(IDB_TREE_ICON);
BITMAP bmpInfo;
bm.GetBitmap(&bmpInfo);
// Speicher anlegen und alten sichern
CDC SpeicherDC;
SpeicherDC.CreateCompatibleDC(pDC);
CBitmap * oldbm = SpeicherDC.SelectObject(&bm);
int ItemCnt=0;
do
{
ItemCnt++;
// Position ermitteln
int x = HORZ_MARGIN+CalculateRootCnt(hItem)*20;
int y = VERT_MARGIN+m_nRowHeight*(ItemCnt+1);
// Bild ermitteln
int nImage, nSelectedImage;
Tree.GetItemImage(hItem,nImage,nSelectedImage);
// Bild transparent machen (rosa->weiss)
for(int picx=0;picx for(int picy=0;picy if(SpeicherDC.GetPixel(picx,picy)==RGB(255,0,255))
SpeicherDC.SetPixel(picx,picy,RGB(255,255,255));
// Bild an DC übergeben
pDC->BitBlt(x,y,bmpInfo.bmWidth/4,bmpInfo.bmHeight,
&SpeicherDC,bmpInfo.bmWidth/4*nImage,0,SRCCOPY);
// Tree-Text verarbeiten
CString text = Tree.GetItemText(hItem);
char tmp[256];
strcpy(tmp,text);
text = strtok( tmp, "\t" );
pDC->TextOut(x+25,y+BIAS,text);
text = strtok( NULL, "\t" );
pDC->TextOut(x+25+300,y+BIAS,text);
// Tree "aufklappen", nächstes Item
Tree.Expand(hItem,TVE_EXPAND);
hItem = Tree.GetNextItem(hItem,TVGN_NEXTVISIBLE);
}
while(hItem && ItemCnt
// Fusszeile -> Seitenanzahl
CString nPage;
nPage.Format(" %d",pInfo->m_nCurPage);
nPage = UINTtoCString(IDS_OUT_PAGE) + nPage;
pDC->TextOut(prntX/2-nPage.GetLength(),prntY-20,nPage);
// alte Objekte zurücksetzen
pDC->SelectObject(pOldFont);
//pDC->SelectObject(oldbm);
pDC->RestoreDC(nSavedDC);
}
//OnPrint///////////////////////////////////////////////////////////////////
Re: Problem with OnPrint in MFC
Can you be more specific with the problem.
Re: Problem with OnPrint in MFC
I try :-)
This source builds up an tree in the dc from the printer.
My main problem is, that in the printpreview my CBitmap (bm.LoadBitmap(IDB_TREE_ICON);) is shown and even if i print on the paper the images are lost.
I send for each treeitem an picture to the dc so that it looks in the preview like an normal tree (some details like pointing between the folders cames soon).
Re: Problem with OnPrint in MFC
This is because the printer DC does not support BitBlt while the screen DC during preview does.
You need to use StretchDIBits to draw the bitmap to the printer. See this article of mine:
http://www.codeproject.com/printing/...cksandtips.asp
Re: Problem with OnPrint in MFC
Actually, the printer may or may not support BitBlt, and may or may not support StretchDiBits.
To find out, you can use GetDeviceCaps with the proper index (RASTERCAPS) and check to see what's supported.
See the link to MSDN above for details.
Hope that helps.