Print contents of a listbox

I have made a CFormView Derived app, and in that app I open a modal Dialog Box. In that dialog box are there just a ListBox containing alot of lines. I would like to print that content. I wonder if it is possible to use TextOut to the variable connected to the List box. It should be as easy as possible nothing fancy just functional.
Thanx...
FfazeAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
pagladasuConnect With a Mentor Commented:
You can try out the following:

1. Declare a control variable (using classwizard) of CListbox type and associated with the list box. Assume its name is m_listbox
2. Find the number of strings in the list box
    int itemcount=m_listbox.GetCount();
3. Now you can use a for..loop to iterate thru its contents, retrieve each string and use TextOut to print them
   CString item;
   for(int i=0;i<itemcount;i++){
      m_listbox.GetText(i,item);
     //now item contains the string
    //use TextOut as reqired by you to print it.
   }
Hope the above guidelines will help you out.
Thanks.
Pagladasu

0
 
FfazeAuthor Commented:
I´m sorry but I´m quite new at MFC. I can´t seem to get it to work. Maybe you can help me out with TextOut function aswell. I´ve tried a couple of things but nothing comes out on the printer. I´ve only printed before in the CFormView class and there are the CDC a variable that comes in to the function automaticly. I don´t know how to do with this variable and how to get TextOut to work as it should. Please help me and I will raise the points.
Thanx...
//Ffaze
0
 
pagladasuCommented:
//I am sending you a sample code with the following assumptions
//You are using a Single Document Application with the view class
//derived from CFormView. Also you have enabled print and print
//preview support. Assume that the view class is call MyAppView.
//Also I am assuming that there is a list box with ID value
//IDC_LIST1.

//Note: I really don't need OnBeginPrinting and OnEndPrinting
//So I remark it out

#if 0

void CMyAppView::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo)
{
      // TODO: Add your specialized code here and/or call the base class
      
      //CFormView::OnBeginPrinting(pDC, pInfo);
}

void CMyAppView::OnEndPrinting(CDC* pDC, CPrintInfo* pInfo)
{
      // TODO: Add your specialized code here and/or call the base class
      
      //CFormView::OnEndPrinting(pDC, pInfo);
}

#endif

//Add the following in OnPrepareDC

void CMyAppView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo)
{
      // TODO: Add your specialized code here and/or call the base class
      pDC->SetMapMode(MM_LOENGLISH);      
      //CFormView::OnPrepareDC(pDC, pInfo);
}

//This will be the code in OnPreparePrinting
BOOL CMyAppView::OnPreparePrinting(CPrintInfo* pInfo)
{
      return DoPreparePrinting(pInfo);
}

//The major code begins at this point - in OnPrint
void CMyAppView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
{
      // TODO: Add your specialized code here and/or call the base class
      CString str;
      pDC->SetMapMode(MM_TWIPS);
      CFont font;
      TEXTMETRIC tm;
      int nHeight;
      font.CreateFont(-180,0,0,0,400,FALSE,FALSE,
         0,ANSI_CHARSET,OUT_DEFAULT_PRECIS,
         CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,
         DEFAULT_PITCH|FF_MODERN,"Courier New");
      CFont *pOldFont=(CFont *)(pDC->SelectObject(&font));
      pDC->GetTextMetrics(&tm);
      CPoint point(36,-1440);
      nHeight=tm.tmHeight + tm.tmExternalLeading;
      int nListCount=GetDlgItem(IDC_LIST1)->GetCount();
      for(int i=0;i<nListCount;i++){
            point.y-=2*nHeight;
            str=GetDlgItem(IDC_LIST1)->GetText(i,str);
            pDC->TextOut(point.x,point.y,str);
      }
      pInfo->m_bContinuePrinting=FALSE;
      pDC->SelectObject(pOldFont);
      //CFormView::OnPrint(pDC, pInfo);
}


0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
FfazeAuthor Commented:
The problem are that the List box are not in the CMyView Class but in the CMyDlg Class created after a button click in CMyView Class. So I want to print the listbox in the CMyDlg class. There are no OnPrint functions in CMyDlg but there´s got to be a way. Thanx for all your help.

Increased points...
0
 
pagladasuCommented:
All right Ffaze. I overlooked the modal dialog box part in your question. Let us say there is a button captioned 'Print' whose BN_CLICKED event is mapped to OnPrintList() function.
You can insert the following code in OnPrintList function:

CPrintDialog * pDlg=new CPrintDialog(FALSE);
pDlg->DoModal();
HDC hdc=pDlg->GetPrinterDC();
CDC *pDC=CDC::FromHandle(hdc);
CString str;
TEXTMETRIC tm;
int nHeight;
pDC->GetTextMetrics(&tm);
CPoint point(0,0);
nHeight=tm.tmHeight + tm.tmExternalLeading;
DOCINFO di;
::memset(&di,0,sizeof(DOCINFO));
di.cbSize=sizeof(DOCINFO);
di.lpszDocName="Test Document";
if(pDC->StartDoc(&di)!=SP_ERROR){
 int nListCount=((CListBox *)GetDlgItem(IDC_LIST1))->GetCount();
 pDC->StartPage();
 for(int i=0;i<nListCount;i++){
   ((CListBox *)GetDlgItem(IDC_LIST1))->GetText(i,str);
   point.y +=nHeight;
   pDC->TextOut(0,point.y,str);
 }
 pDC->EndPage();
 pDC->EndDoc();
}
::RestoreDC(hdc,-1);

0
 
FfazeAuthor Commented:
Thank you very much for all your work. it worked perfectly.
//Ffaze
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.