Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Print contents of a listbox

Posted on 1998-10-23
6
Medium Priority
?
263 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 4

Accepted Solution

by:
pagladasu earned 600 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…

704 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