Impression multipage d'une chaine de caratères.

Résolu
afavre69 Messages postés 6 Date d'inscription samedi 1 mars 2008 Statut Membre Dernière intervention 13 octobre 2008 - 28 août 2008 à 11:06
afavre69 Messages postés 6 Date d'inscription samedi 1 mars 2008 Statut Membre Dernière intervention 13 octobre 2008 - 3 sept. 2008 à 17:12
Bonjour,

Dans mon appli, j'ai un petit bouton Print me permettant d'imprimer une chaine de caractères.

Ma fonction marche parfaitement si le texte a imprimer ne dépasse pas 1 page.

Ma fonction, doit normalement calculer combien de texte je peut mettre sur une page. Dans le cas ou mon texte est plus long, alors une deuxième page est imprimée (ou plus si nécessaire), mais répète le début de mon texte et non la suite, mais par contre, la longueur du texte imprimé est bonne.




Avez-vous une idée ? Merci d'avance.

Voici ma partie de code :

bOk = StartDoc( hDC, &di );

while((bOk)&&(t < dwSize)){

StartPage(hDC);

SetRect(&rc, rcPage.left,rcPage.top,rcPage.right,rcPage.top);

count = 0;

// on calcule d'abord la longueur de texte contenu dans une page

while((rc.bottom<rcPage.bottom)&&(t + count < dwSize)){

rc.right = rcPage.right;

DrawText(hDC, prt2, count++, &rc,DT_CALCRECT|DT_NOCLIP|DT_WORDBREAK|DT_EXPANDTABS);

}

// on ajuste la hauteur de la page

if(rc.bottom>=rcPage.bottom)count--;

// on imprime la page

DrawText(hDC, prt2, count, &rc,DT_NOCLIP|DT_WORDBREAK|DT_EXPANDTABS);

pText += count;

t += count;

StrNCpy( prt2, prt, count);

bOk = EndPage(hDC);

}

7 réponses

cs_juju12 Messages postés 966 Date d'inscription samedi 3 avril 2004 Statut Membre Dernière intervention 4 mars 2010 4
28 août 2008 à 16:59
Dans quoi est stocké ton texte? pTexte, prt?
Parce que si c'est prt ton problème est normal, tu ne l'incrémentes pas.
3
afavre69 Messages postés 6 Date d'inscription samedi 1 mars 2008 Statut Membre Dernière intervention 13 octobre 2008
28 août 2008 à 17:28
Bonjour Juju12 et Merci d'avoir prix le temps de consulter mon message.

En faite c'est prt et non prt2 qui est un TCHAR. et je ne vois pas comment incrementer un TCHAR. je devrais sans doute passer par le pointeur pText, mais je ne vois pas comment faire la relation entre mon prt et mon pointeur.

Merci
3
cs_juju12 Messages postés 966 Date d'inscription samedi 3 avril 2004 Statut Membre Dernière intervention 4 mars 2010 4
28 août 2008 à 20:40
TCHAR szwText[]=__TEXT("Voila le texte à imprimer etc..."); // ou bien un pointeur sur le texte à imprimer
DWORD dwSize=...;
LPTSTR pText=szwText;
LPTSTR pEnd=szwText+dwSize;

bOk = StartDoc( hDC, &di );
while((bOk)&&(pText < pEnd))
{
StartPage(hDC);
SetRect(&rc, rcPage.left,rcPage.top,rcPage.right,rcPage.top);
count = 0;
// on calcule d'abord la longueur de texte contenu dans une page
while((rc.bottom<rcPage.bottom)&&(pText+ count < pEnd))
{
rc.right = rcPage.right;
DrawText(hDC, pText, count++, &rc,DT_CALCRECT|DT_NOCLIP|DT_WORDBREAK|DT_EXPANDTABS);
}
// on ajuste la hauteur de la page
if(rc.bottom>=rcPage.bottom)  count--;
// on imprime la page
DrawText(hDC, pText, count, &rc,DT_NOCLIP|DT_WORDBREAK|DT_EXPANDTABS);
pText += count;
bOk = EndPage(hDC);
}



Est-ce-que c'est mieux?
3
afavre69 Messages postés 6 Date d'inscription samedi 1 mars 2008 Statut Membre Dernière intervention 13 octobre 2008
29 août 2008 à 21:33
Bonsoir,


Merci pour la réponse, je rentre juste de déplacement, je teste ça ce week-end.


Alain
3

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
afavre69 Messages postés 6 Date d'inscription samedi 1 mars 2008 Statut Membre Dernière intervention 13 octobre 2008
1 sept. 2008 à 18:49
Bon,

Super ça marche mais j'ai un problème maintenant d'allocation mémoire qui  me plante l'appli après l'impression.

Voici les trois parties de code :

Ici, le bouton print qui envoi des info dans un editbox caché  (prt est défini en temps que TCHAR [10000] je ne sais pas comment lui donner une valeur variable pour le remplir:

case IDC_BT_PRINT:


StrCpy(prt, APPNAME);


StrCat(prt, L"\r\n");


StrCat(prt, L"_________________________________________________________________");


StrCat(prt, L"\n\r\n");


StrCat(prt, LS("IDS_RECEIVED"));


StrCat(prt, L": ");


StrCat(prt, sprintFileDateTime(&ftRecvd,TRUE,TRUE));


StrCat(prt, L"\r\n");


StrCat(prt,LS("IDS_FROM"));


StrCat(prt,L": ");


StrCat(prt, szFrom);


StrCat(prt, L"\r");


StrCat(prt, LS("IDS_TO"));


StrCat(prt,L": ");


StrCat(prt, szTo);



if
(lstrlen(szCc) == 0) {



} else {


StrCat(prt,LS("IDS_CC"));


StrCat(prt,L": ");


StrCat(prt,szCc);


}



if
(lstrlen(szBcc) == 0) {



} else {


StrCat(prt,LS("IDS_BCC"));


StrCat(prt,L": ");


StrCat(prt,szBcc);


}


StrCat(prt, L"\r\n");


StrCat(prt, L"\r\n");


StrCat(prt, LS("IDS_SUBJECT"));


StrCat(prt,L": ");


StrCat(prt, szSubject);


StrCat(prt, L"\r\n");


StrCat(prt, L"\r\n");


StrCat(prt, pfP);





HC(hWnd,IDC_BOXPRINT);





PrintDlgEdit(hWnd, IDC_BOXPRINT);





free(prt);



break
;



}

Ensuite Voici le traitement de mon PrintDlgEdit et le reste :


void PrintDlgEdit( HWND hWnd,
int idEdit )


{


TCHAR *pText;


DWORD dwlength;


dwlength = GetWindowTextLength(GetDlgItem(hWnd, idEdit));



if



(dwlength >= 0)


return



;pText = (TCHAR *) malloc((DWORD) dwlength  + 1);


if
(GetWindowText(GetDlgItem(hWnd,idEdit), pText, dwlength + 1)){

PrintStuff(hWnd, pText, dwlength);

}

free (pText);

}



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


// //


// PRINT FUNCTION //


// //


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




void



PrintStuff( HWND hWndParent, TCHAR *pText, DWORD dwSize ){

HDC hDC;

DOCINFO di;

RECT rc, rcPage, g_rtMargin;

HFONT hFont, hFont2, oldFont;

LOGFONT lf;

BOOL bOk;

DWORD t = 0;

LOGFONT logFont;



int



HauteurCaractere=0;


int



heightFont, count;


int



cxmm, cymm, cxpl, cypl; 

ZeroMemory(&lf,


sizeof
(lf));lstrcpy(lf.lfFaceName, L


"Arial"
);

// Police de font par default.

lf.lfHeight = 14;




// Taille de la police par default.

hFont = CreateFontIndirect(&lf);







// Detect Printer DC.

hDC = GetPrinterDC(hWndParent);







if
( !hDC ){

MessageBox(hWndParent, LS(


"IDS_NO_PRINT"
, APPNAME), APPNAME,MB_APPLMODAL | MB_OK );


return
;}


// You always have to use an AbortProc().






if
( SetAbortProc( hDC, AbortProc ) == SP_ERROR ){

MessageBox(hWndParent, LS(


"IDS_ABORT_PRINT"
), APPNAME, MB_APPLMODAL | MB_OK );


return
;}

heightFont = -MulDiv(lf.lfHeight, 1, GetDeviceCaps(hDC, LOGPIXELSY));

CopyMemory(&logFont, &lf,


sizeof
(LOGFONT));logFont.lfHeight = -MulDiv(heightFont, GetDeviceCaps(hDC, LOGPIXELSY), 1);

hFont2 = CreateFontIndirect(&logFont);

oldFont = (HFONT)SelectObject(hDC, hFont2);

cxpl = GetDeviceCaps(hDC, HORZRES);


// largeur en pixels

cypl = GetDeviceCaps(hDC, VERTRES);




// hauteur en pixels

cxmm = GetDeviceCaps(hDC, HORZSIZE);




// largeur en millimetres

cymm = GetDeviceCaps(hDC, VERTSIZE);




// hauteur en millimetres






// on calcule la dimension de la page en tenant compte la marge

SetRect(&g_rtMargin, 100, 100, 2000, 2400);


rcPage.left = (




int
)((

float
)(g_rtMargin.left*cxpl)/(

float
)(cxmm*100));rcPage.top = (


int
)((

float
)(g_rtMargin.top*cypl)/(

float
)(cymm*100));rcPage.right = cxpl - (


int
)((

float
)(g_rtMargin.right*cxpl)/(

float
)(cxmm*100));rcPage.bottom = cypl - (


int
)((

float
)(g_rtMargin.bottom*cypl)/(

float
)(cymm*100));GetWindowRect(hWndParent, &rc);

OffsetRect(&rc, -rc.left, -rc.top);


// Init the DOCINFO and start the document.

InitDocStruct( &di, APPNAME );










// Print one page.

bOk = StartDoc( hDC, &di );







while
((bOk)&&(t < dwSize)){

StartPage(hDC);

SetRect(&rc, rcPage.left,rcPage.top,rcPage.right,rcPage.top);

count = 0;


// on calcule d'abord la longueur de texte contenu dans une page






while
((rc.bottom<rcPage.bottom)&&(t + count < dwSize)){

rc.right = rcPage.right;

DrawText(hDC, pText, count++, &rc,DT_CALCRECT|DT_NOCLIP|DT_WORDBREAK|DT_EXPANDTABS);

count++;

}


// on ajuste la hauteur de la page






if
(rc.bottom>=rcPage.bottom)count--;


// on imprime la page




DrawText(hDC, pText, count, &rc,DT_NOCLIP|DT_WORDBREAK|DT_EXPANDTABS);





pText += count;


t += count;





bOk = EndPage(hDC);


}










// Indicate end of document.

EndDoc( hDC );







// Clean up

DeleteDC( hDC );


}





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


// //


// OBTAIN PRINTER DC //


// //


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




HDC GetPrinterDC(HWND hWndParent)


{


PRINTDLG pdlg;


memset( &pdlg, 0,




sizeof
( PRINTDLG ) );pdlg.nCopies = 1;

pdlg.nFromPage = 1;

pdlg.nToPage = 1;

pdlg.nMinPage = 1;

pdlg.nMaxPage = 1;

pdlg.lStructSize =


sizeof
( PRINTDLG );pdlg.hwndOwner = hWndParent;

pdlg.Flags = PD_USEDEVMODECOPIESANDCOLLATE | PD_PAGENUMS | PD_RETURNDC;

PrintDlg( &pdlg );


return
pdlg.hDC;}



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


// //


// ABORT PRINT PROCEDURE //


// //


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




BOOL CALLBACK AbortProc( HDC hDC,




int
Error ){

MSG msg;


while
( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) ){

TranslateMessage( &msg );

DispatchMessage( &msg );

}


return
TRUE;}



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


// INIT DOCINFO STRUCTURE //


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




void



InitDocStruct( DOCINFO* di, LPCWSTR docname){


// Always zero it before using it.

memset( di, 0,




sizeof
( DOCINFO ) );


// Fill in the required members.

di->cbSize =




sizeof
( DOCINFO );di->lpszDocName = docname;

}
3
cs_juju12 Messages postés 966 Date d'inscription samedi 3 avril 2004 Statut Membre Dernière intervention 4 mars 2010 4
2 sept. 2008 à 20:50
Content que ça marche;
Puis-je te conseiller de poser ta nouvelle question dans un nouveau post?
raison principale : celui-ci n'apparaît plus je pense dans la liste, et pour ma part je vais être coupé d'Internet pour une durée indéterminée...
3
afavre69 Messages postés 6 Date d'inscription samedi 1 mars 2008 Statut Membre Dernière intervention 13 octobre 2008
3 sept. 2008 à 17:12
Bonsoir Juju12,

Super ça fonctionne, j'ai reglé mon petit soucis.
Je peux poster le code dans son ensemble si quelqun en a besoin.

A+
3
Rejoignez-nous