This is a code that runs fine. I can select an none default printer with this code.
I also can select the paper size with DEVMODE.
##########################
##########
##########
###
void CDrucken_DEVMODEDlg::OnBnC
lickedButt
on1()
{
HANDLE hDevMode;
HANDLE hDevNames;
if (GetPrinterDevice(_T("Dell
AIO Printer A920"), &hDevNames, &hDevMode))
//if (GetPrinterDevice(_T("EPSO
N LQ-680Pro Port 2 Wiegekarte"), &hDevNames, &hDevMode))
{
AfxGetApp()->SelectPrinter
(hDevNames
, hDevMode);
CPrintDialog dlgPrint(FALSE,PD_PAGENUMS
|PD_USEDEV
MODECOPIES
ANDCOLLATE
, this);
//------------------------
--- Papier Auswahl DEVMODE --------------------------
--------
if(AfxGetApp()->GetPrinter
DeviceDefa
ults(&dlgP
rint.m_pd)
)
{
LPDEVMODE dev = dlgPrint.GetDevMode();
GlobalUnlock(dev);
dev->dmOrientation=DMORIEN
T_PORTRAIT
;
//dev->dmOrientation=DMORI
ENT_LANDSC
APE;
//dev->dmPaperSize=DMPAPER
_A4;
//dev->dmPaperSize=DMPAPER
_A5;
dev->dmPaperLength=1020;
dev->dmPaperWidth=1480;
dev->dmDefaultSource=DMBIN
_TRACTOR;
dev->dmCopies=2;
dev->dmFields |=DM_PAPERLENGTH|DM_PAPERW
IDTH|DM_OR
IENTATION|
DM_DEFAULT
SOURCE|DM_
COPIES;
}
//------------------------
----------
----------
----------
----------
----------
--
CFont font;
CFont oldfont;
CString strSchriftart="Comic Sans MS";
BOOL fRet=font.CreateFont(
-50,
0,
0,
0,
600,
0,
0,
0,
DEFAULT_CHARSET,
OUT_CHARACTER_PRECIS,
CLIP_CHARACTER_PRECIS,
DEFAULT_QUALITY,
DEFAULT_PITCH|FF_DONTCARE,
strSchriftart);
if(fRet !=TRUE)
{
MessageBox("Error could not create font");
}
//------------------------
----------
----------
----------
----------
----------
--
dlgPrint.m_pd.Flags &= ~PD_RETURNDC; // Reset flag set by constructor.
// Force the CPrintDialog to use our device mode & name.
dlgPrint.m_pd.hDevMode = CopyHandle(hDevMode);
dlgPrint.m_pd.hDevNames = CopyHandle(hDevNames);
//========================
==========
==========
===
if(dlgPrint.DoModal()==IDO
K)
{
GlobalFree(hDevMode);
GlobalFree(hDevNames);
hDevMode = CopyHandle(dlgPrint.m_pd.h
DevMode); // Save new copies.
hDevNames = CopyHandle(dlgPrint.m_pd.h
DevNames);
// Save new copies.
//Use CreatePrinterDC() instead GetPrinterDC(). Like this
HDC hDCPrint = dlgPrint.CreatePrinterDC()
;
DOCINFO Print;
memset( &Print, 0, sizeof(DOCINFO) );
Print.cbSize = sizeof (DOCINFO);
Print.lpszDocName = "Drucken im Dialog";
Print.lpszOutput = (LPTSTR) NULL;
Print.lpszDatatype = (LPTSTR) NULL;
Print.fwType = 0;
if(StartDoc(hDCPrint,&Prin
t)>=0)
{
StartPage(hDCPrint);
SetMapMode(hDCPrint,MM_LOM
ETRIC);
int nLaenge = GetDeviceCaps(hDCPrint,VER
TSIZE);
int nBreite = GetDeviceCaps(hDCPrint,HOR
ZSIZE);
int nRechts = nBreite * 10 -50;
int nUnten = -nLaenge * 10 +50;
CString A="Urlaubsantrag";
//------------------------
----------
-
::SelectObject(hDCPrint,(H
FONT)font)
;
//------------------------
----------
-
CPrintInfo* pInfo=(CPrintInfo*)this;
int page=0;
page=pInfo->m_nCurPage;
CString str_page;
str_page.Format("%d",page)
;
TextOut(hDCPrint,100,-10,s
tr_page,st
rlen(str_p
age));
//strlen(A) Länge vom String
//TextOut(hDCPrint,100,200
,"Hallo",5
) geht so auch muß nur immer Länge zählen
//------------------------
----------
----------
----------
------
::TextOut(hDCPrint,100,-50
, A,strlen(A));
font.DeleteObject();::Sele
ctObject(h
DCPrint,(H
FONT)oldfo
nt);
TextOut(hDCPrint,100,-120,
"=========
====",13);
A="Name : ............... Vorname : ..........";
TextOut(hDCPrint,100,-200,
A,strlen(A));
A="vom : ............... bis : ..........";
TextOut(hDCPrint,100,-400,
A,strlen(A));
A="Datum: ........ Unterschrift: ...............";
TextOut(hDCPrint,100,-600,
A,strlen(A));
A="genehmigt: Datum: .........";
TextOut(hDCPrint,100,-800,
A,strlen(A));
//------------------------
----------
----------
----------
----------
--
EndPage(hDCPrint);
EndDoc(hDCPrint);
DeleteDC(hDCPrint);
}
}
GlobalFree(dlgPrint.m_pd.h
DevMode); // Because DoModal was called,
GlobalFree(dlgPrint.m_pd.h
DevNames);
// we need to free these.
}
}
##########################
##########
##########
###
Here is my new code. It has 3 functions:
void OnBeginnPrinting(CDC* pDC, CPrintInfo* pInfo);
void OnPrint(CDC* pDC, CPrintInfo* pInfo);
void OnEndPrinting((CDC* pDC, CPrintInfo* pInfo);
This code below works fine. I would like to change this code that I can select an default printer like the code above with HDC. How can I convert/change the HDC to CDC???
How can I do this. Please help...I am a beginner !
##########################
##########
##########
###
void CDrucken_DEVMODEDlg::OnBeg
inPrinting
(CDC* pDC, CPrintInfo* pInfo)
{
//Druckbereich in 0,1mm
pDC->SetMapMode(MM_LOMETRI
C);
pInfo->SetMaxPage(1);
//--------------- Neue Schrift erstellen ------------------
ASSERT(m_fontUeberschrift=
=0);
m_fontUeberschrift= new CFont;
ASSERT(m_fontUeberschrift)
;
m_fontUeberschrift->Create
Font(-50,
0,
0,
0,
600,
0,
0,
0,
DEFAULT_CHARSET,
OUT_CHARACTER_PRECIS,
CLIP_CHARACTER_PRECIS,
DEFAULT_QUALITY,
DEFAULT_PITCH|FF_DONTCARE,
"Arial");
//------------------------
----------
----------
----------
---
}
%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%
%%
void CDrucken_DEVMODEDlg::OnPri
nt(CDC* pDC, CPrintInfo* pInfo)
{
//Druckbereich in 0,1mm
pDC->SetMapMode(MM_LOMETRI
C);
CString A;
CString str;
A.Format("Page: %d",m_currentPageNo);
CFont* pOldFont = pDC->SelectObject(m_fontUe
berschrift
);
pDC->TextOut(100,-200,"Die
s ist meine Überschrift");
pDC->SelectObject(pOldFont
);
pDC->TextOut(100,-50,A);
//-----aktuelle Seite +1 -----------
m_currentPageNo=m_currentP
ageNo+1;
//------------------------
---------
}
//%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%
%%%
void CDrucken_DEVMODEDlg::OnEnd
Printing(C
DC* pDC, CPrintInfo* pInfo)
{
delete m_fontUeberschrift;
m_fontUeberschrift=NULL;
}
//%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%
%%%
//************************
**********
**********
*********
//************************
**********
**********
*********
//************************
**********
**********
*********
void CDrucken_DEVMODEDlg::OnBnC
lickedButt
on2()
{
HANDLE hDevMode;
HANDLE hDevNames;
// if (GetPrinterDevice(_T("Dell
AIO Printer A920"), &hDevNames, &hDevMode))
//{
// AfxGetApp()->SelectPrinter
(hDevNames
, hDevMode);
CPrintDialog dlgPrint(FALSE,PD_PAGENUMS
|PD_USEDEV
MODECOPIES
ANDCOLLATE
,this);
//xxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxx
if(dlgPrint.DoModal()==IDO
K)
{
CDC dc;
dc.Attach(dlgPrint.GetPrin
terDC());
dc.m_bPrinting=TRUE;
//------------------------
----------
----------
----------
----------
----------
-------
LPDEVMODE dev=dlgPrint.GetDevMode();
GlobalUnlock(dev);
dev->dmOrientation=DMORIEN
T_LANDSCAP
E; //Querformat
// dev->dmOrientation=DMORIEN
T_PORTRAIT
; //Hochformat
// dev->dmPaperSize=DMPAPER_A
4;
// dev->dmPaperSize=DMPAPER_A
5;
dev->dmPaperLength=1480;
dev->dmPaperWidth=1020;
dev->dmCopies=1;
dev->dmDefaultSource=DMBIN
_TRACTOR;
dev->dmFields |=DM_PAPERLENGTH|DM_PAPERW
IDTH|DM_OR
IENTATION|
DM_DEFAULT
SOURCE;
dc.ResetDC(dev);
dlgPrint.m_pd.Flags &= ~PD_RETURNDC; // Reset flag set by constructor.
//------------------------
----------
--
DOCINFO Print;
memset( &Print, 0, sizeof(DOCINFO) );
Print.cbSize = sizeof (DOCINFO);
Print.lpszDocName = "Drucken im Dialog";
Print.lpszOutput = (LPTSTR) NULL;
Print.lpszDatatype = (LPTSTR) NULL;
Print.fwType = 0;
//------------------------
----------
-
BOOL bPrintingOK = dc.StartDoc(&Print);
CPrintInfo pInfo;
pInfo.m_rectDraw.SetRect(0
,0,dc.GetD
eviceCaps(
HORZRES), dc.GetDeviceCaps(VERTRES))
;
//~~~~~~~~~~~~~~~~~~~~~~~~
~
OnBeginPrinting(&dc,&pInfo
); // call prepare
//~~~~~~~~~~~~~~~~~~~~~~~~
~
//========================
==========
=======
for(UINT page=pInfo.GetMinPage();pa
ge<=pInfo.
GetMaxPage
() &&bPrintingOK ; page++)
{
//begin a new page
dc.StartPage();
pInfo.m_nCurPage=page;
//~~~~~~~~~~~~~~~~~~~~~~~~
~
OnPrint(&dc,&pInfo); // call Print your Page
//~~~~~~~~~~~~~~~~~~~~~~~~
~
bPrintingOK=(dc.EndPage()>
0);
}
//========================
==========
=========
//~~~~~~~~~~~~~~~~~~~~~~~~
~
OnEndPrinting(&dc,&pInfo);
// call your clean up
//~~~~~~~~~~~~~~~~~~~~~~~~
~
if(bPrintingOK)
{
dc.EndDoc();
}
else
{
dc.AbortDoc();
}
dc.DeleteDC();
}
//xxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxx
// GlobalFree(dlgPrint.m_pd.h
DevMode); // Because DoModal was called,
//GlobalFree(dlgPrint.m_pd
.hDevNames
); // we need to free these.
}
##########################
##########
##########
###
Thank you very much
Best regards,
Thomas