Kann mal jemand Klartext reden

Silvercreast

Erfahrenes Mitglied
Hallo

ich bin bald am verzweifeln , ich habe eine MFC - Anwendung geschrieben in der ich nach Liedern suchen kann usw. die werden dann in einer CListCtrl angezeigt. Jetzt möchte ich es schaffen diese auch auszudrucken. Hab dazu auch was bei codeguru gefunden und habe auch in edlichen Foren und auf Internet Seiten mir Informationen geholt was das drucken aus nem Dialog betrifft. Alle erzählen mir was von einer CPrintDialog funktion die ich benutzen muss. Aber erstens ist mir nicht so bewusst ( klar ) was ich mit CPrintDialog anfangen kann (muss) und in dem Code den ich mir besorgt habe wird die CPrintDialog auf False gesetzt aber Warum? .Ihr könnt euch ja mal den Code angucken. So das alles ist für mich ziemlich verwirrend . Kann mir das Drucken aus nem Dialogoder die CPrintDialog funktion mal jemand erklären

Wäre sehr dankbar
Code:
void PrintList(CListCtrl &list)
{
    CDC             dc;
    CPrintDialog    printDlg(FALSE);
    CRect           r;
    int             nHeight;

    // ask the user to select a printer
    if (printDlg.DoModal() == IDCANCEL)
        return;

    // Attach a printer DC
    dc.Attach(printDlg.GetPrinterDC());
    dc.m_bPrinting = TRUE;

    // use Textmappingmode, that's easiest to map the fontsize
    dc.SetMapMode(MM_TEXT);

    // setup font specifics
    LOGFONT LogFont;

    CFont	aFont, *oldFont;

    LogFont.lfHeight = -MulDiv(10, GetDeviceCaps(dc, LOGPIXELSY), 72);
    LogFont.lfWidth = 0;
    LogFont.lfEscapement = 0;
    LogFont.lfOrientation = 0;
    LogFont.lfWeight = 0;
    LogFont.lfItalic = false;
    LogFont.lfUnderline = 0;
    LogFont.lfStrikeOut = 0;
    LogFont.lfCharSet = ANSI_CHARSET;
    LogFont.lfOutPrecision = OUT_TT_PRECIS;
    LogFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
    LogFont.lfQuality = DEFAULT_QUALITY;
    LogFont.lfPitchAndFamily = DEFAULT_PITCH | FF_SWISS;
    lstrcpy (LogFont.lfFaceName, "MS Sans Serif");
    dc.SetBkMode(OPAQUE);
    aFont.CreateFontIndirect ( &LogFont );
    // ok, we've build the font, now use it
    oldFont = dc.SelectObject( &aFont );

    // Get the application title
    CString strTitle;
    strTitle.LoadString(AFX_IDS_APP_TITLE);

    // Initialise print document details

    DOCINFO di;
    ::ZeroMemory (&di, sizeof (DOCINFO));
    di.cbSize = sizeof (DOCINFO);
    // application title appears in the spooler view
    di.lpszDocName = strTitle;

//*****************Begin a new print job**************************************************
	CString Line;	

	BOOL bPrintingOK = dc.StartDoc( &di );


	

    // Get the printing extents and store in the m_rectDraw field of a
    // CPrintInfo object
    CPrintInfo Info;
    int w = dc.GetDeviceCaps(HORZRES);
    int h = dc.GetDeviceCaps(VERTRES);
    Info.m_rectDraw.SetRect(0,0, w, h);

    const char *startAt = LPCTSTR(Line);
    int totalDone = 0;
    int lengthToGo = Line.GetLength();

 //   for (UINT page = Info.GetMinPage();
  //  bPrintingOK ; page++)
    {
        // begin new page
		int page  = Info.GetMinPage();
        dc.StartPage();
        Info.m_nCurPage = page;

		
        // calc how much text fits on one page
        r = Info.m_rectDraw;
        r.bottom = r.top;
        int i = 0;
        while (r.bottom < Info.m_rectDraw.bottom && (totalDone + i < lengthToGo) )
        {
            r.right = Info.m_rectDraw.right;
            nHeight = dc.DrawText(startAt, i++, r,
                DT_CALCRECT|DT_WORDBREAK|DT_NOCLIP|DT_EXPANDTABS);
        }
        // go one back to assure correct height
        if (r.bottom >= Info.m_rectDraw.bottom)
            i--;


		for (int item = 0; item<list.GetItemCount(); item++)
		{
			CRect rect( 0, 0, 300, 100 );
   		
			rect = Info.m_rectDraw;
			Line =  list.GetItemText(item, 0)
			 + list.GetItemText(item, 1)
			 + list.GetItemText(item, 2)
			 + list.GetItemText(item, 3)
			 + list.GetItemText(item, 4);
			// print that text
			dc.DrawText( 
				LPCTSTR(Line), 
				Line.GetLength(), 
				rect, 
				DT_WORDBREAK|DT_NOCLIP|DT_EXPANDTABS);
			rect.OffsetRect( 0, 20 );
	
		}

        // go to next page
     //   startAt += i;
      //  totalDone += i;

        // end page
        bPrintingOK = (dc.EndPage() > 0);
//    }
	}

    // end a print job
    if (bPrintingOK)
        dc.EndDoc();
    else
        // abort job.
        dc.AbortDoc();


    // restore font
    dc.SelectObject(oldFont);
    // free font memory
    aFont.DeleteObject();
    // detach the printer DC
    dc.Detach();
}

gruß Silver
 
Zuletzt bearbeitet:
1. Zum PrintDialog:
Es gibt zwei vorgefertigte PrintDialog-Boxen in der MFC, der PrintDialog, in dem man die Druck-Parameter eingibt und die PrintSetupDialog-Box, in der man die Druckeranpassung vornimmt. Welche davon angezeigt wird, hängt vom Parameter ab (FALSE oder TRUE).
Bei FALSE wird der normale PrintDialog angezeigt, bei TRUE der SetupDialog.

2. Zum Drucken aus deinem Dialog:
Leider schreibst du nicht, ob du eine dialogbasierte Anwendung hast oder einen Dialog in einer normalen SDI- oder MDI-Anwendung. Aber im Prinzip ist der Ablauf derselbe. Du mußt in deinem Dialog die Druckfunktion, hier also "PrintList(.....) auf geeignete Weise aufrufen, über einen Button "Drucken" vielleicht. Dann erscheint in diesem Code erstmal der Print-Dialog, in dem du den Drucker wählst usw. und dann werden die Daten über StartDoc(), StartPage(), EndPage() und EndDoc() zum Drucker gesendet.
Ich hatte vor kurzem dieselben Probleme ;) und hatte deshalb hier ein Thema eröffnet. Schau mal hier nach, da gibt es eine Reihe von Antworten zu Druckerfragen.
http://www.tutorials.de/tutorials212487.html

Mfg

langer
 
Zurück