Solved

Print contents of a listbox

Posted on 1998-10-23
6
259 Views
Last Modified: 2013-11-20
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...
0
Comment
Question by:Ffaze
  • 3
  • 3
6 Comments
 
LVL 4

Accepted Solution

by:
pagladasu earned 150 total points
ID: 1323741
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
 

Author Comment

by:Ffaze
ID: 1323742
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
 
LVL 4

Expert Comment

by:pagladasu
ID: 1323743
//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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 

Author Comment

by:Ffaze
ID: 1323744
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
 
LVL 4

Expert Comment

by:pagladasu
ID: 1323745
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
 

Author Comment

by:Ffaze
ID: 1323746
Thank you very much for all your work. it worked perfectly.
//Ffaze
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question