ListView Drucken ???



  • Hallo,
    kann man eine Listview mit Spaltennamen und die ergebnisse drucken ???

    Danke.



  • Hiermit kannst du das TListView zu Papier bringen.

    void PrintListview(TListView *lv, TPrintDialog *PrintDialog, String sTitel) {
    
    	if (PrintDialog->Execute() ) {
    
    		String sCurStr;
    		TRect rCurRect;
    
    		Printer()->Title = sTitel;
    		Printer()->BeginDoc();
    		float iPageHeight = Printer()->PageHeight;
    		float iPageWidth = Printer()->PageWidth;
    
    		float v = (iPageHeight + (2 * GetDeviceCaps(Printer()->Handle, PHYSICALOFFSETY))) / (29.7 * 0.95);
    		//0.95 is a strange correction factor on the clients printer
    		float h = (iPageWidth + (2 * GetDeviceCaps(Printer()->Handle, PHYSICALOFFSETX))) / 21;
    
    		// calculate total width
    		float iTotColsWidth = 0;
    		for (int i = 0; i < lv->Columns->Count; i++) {
    			iTotColsWidth = iTotColsWidth + lv->Column[i]->Width;
    		}
    
    		// calculate space between lMargin and rMargin
    		float iInnerWidth = iPageWidth - 2*(1.5 * h); // space between margins ?
    
    		std::vector<float> vCols;
    
    		vCols.push_back(1.5 * h); //left margin
    
    		//calculate start of each column
    		for (int i = 0; i < lv->Columns->Count; i++) {
    
    			vCols.push_back( vCols.back() + ((float)lv->Column[i]->Width / iTotColsWidth * iInnerWidth) );
    		}
    
    		vCols.push_back(iPageWidth - (1.5 * h)); //rigth margin
    
    		int iTopMargin = 2.5 * v;
    
    		TCanvas *zf = Printer()->Canvas;
    
    		zf->Font->Size = 10;
    		zf->Font->Style = TFontStyles();
    		zf->Font->Name = "Arial";
    		zf->Font->Color = clBlack;
    		int iTextHeight = zf->TextHeight("WWW");
    		int iLinesOnPage = (iPageHeight - (5 * v)) / iTextHeight;
    		int iNumPages = 1;
    
    		// get number of pages to print
    		while ((iNumPages * iLinesOnPage) < lv->Items->Count) {
    			iNumPages++;
    		}
    
    		// start
    		int iCurLine = 0;
    
    		for (int iCurItem = 0; iCurItem < lv->Items->Count; iCurItem++) {
    
    			if (iCurLine > iLinesOnPage || iCurLine == 0) {
    
    				if (iCurLine > iLinesOnPage) Printer()->NewPage();
    
    				iCurLine = 1;
    
    				if (Printer()->PageNumber == iNumPages) {
    
    					zf->MoveTo(vCols[1], iTopMargin);
    					for (int i = 1; i < lv->Columns->Count; i++) {
    
    						zf->LineTo(vCols[i], iTopMargin + (iTextHeight * (lv->Items->Count - iCurItem + 2)));
    						zf->MoveTo(vCols[i + 1], iTopMargin);
    					}
    				}
    				else {
    
    					// draw vertical lines between data
    					for (int i = 1; i < lv->Columns->Count; i++ ) {
    
    						zf->MoveTo(vCols[i], iTopMargin);
    						zf->LineTo(vCols[i], iTopMargin + (iTextHeight * (iLinesOnPage + 1)));
    					}
    				}
    
    				zf->Font->Style = TFontStyles() << fsBold;
    
    				// print column headers
    				for (int i = 0; i < lv->Columns->Count; i++) {
    
    					zf->TextRect(Rect(vCols[i] + (0.1 * h), iTopMargin - (0.1 * v), vCols[i + 1] - (0.1 * h)
    						, iTopMargin + iTextHeight - (0.1 * v)), ((vCols[i + 1] - vCols[i]) / 2) +
    						vCols[i] - (zf->TextWidth(lv->Columns->Items[i]->Caption) / 2),
    						iTopMargin - (0.1 * v), lv->Columns->Items[i]->Caption);
    				}
    
    				// draw horizontal line beneath column headers
    				zf->MoveTo(vCols[0] - (0.1 * h), iTopMargin + iTextHeight - (0.05 * v));
    				zf->LineTo(vCols[lv->Columns->Count] + (0.1 * h), iTopMargin + iTextHeight - (0.05 * v));
    
    				// print date and page number
    				zf->Font->Size = 8;
    				zf->Font->Style = TFontStyles();
    				int iTmpPos = (zf->TextWidth(DateToStr(Now()) + " - " +
    					IntToStr(Printer()->PageNumber) + " / " + IntToStr(iNumPages))) / 2;
    
    				iTmpPos = iPageWidth - 1.5 * h - (iTmpPos * 2);
    
    				zf->Font->Size = 8;
    				zf->Font->Style = TFontStyles();
    				zf->TextOut(iTmpPos, (0.5 * v), DateToStr(Now()) + " - " + IntToStr(Printer()->PageNumber) + " / " + IntToStr(iNumPages));
    
    				// print report title
    				zf->Font->Size = 16;
    				if (iTmpPos < ((iPageWidth + zf->TextWidth(sTitel)) / 2 + (0.75 * h)) )
    					zf->TextOut((iPageWidth - zf->TextWidth(sTitel)) / 2, (1 * v), sTitel);
    				else
    					zf->TextOut(3 * h, 1 * v, sTitel);
    
    				zf->Font->Size = 10;
    				zf->Font->Style = TFontStyles();
    			}
    
    			rCurRect.Top = iTopMargin + (iCurLine * iTextHeight);
    			rCurRect.Bottom = iTopMargin + ((iCurLine + 1) * iTextHeight);
    
    			// print contents of Listview
    			for (int iCurCol = -1; iCurCol < lv->Columns->Count - 1; iCurCol++) {
    
    				rCurRect.Left = vCols[iCurCol + 1] + (0.1 * h);
    				rCurRect.Right = vCols[iCurCol + 2] - (0.1 * h);
    				try {
    					if (iCurCol == -1)
    						sCurStr = lv->Items->Item[iCurItem]->Caption;
    					else
    						sCurStr = lv->Items->Item[iCurItem]->SubItems->Strings[iCurCol];
    				} catch (...) {
    					sCurStr = "";
    				}
    
    				// write string in TextRect
    				zf->TextRect(rCurRect, rCurRect.Left, rCurRect.Top, sCurStr);
    			}
    			iCurLine++;
    		}
    
    		Printer()->EndDoc();
    	}
    }
    


  • Ah, klasse.
    Dankeschön.


Anmelden zum Antworten