Solved

Print contents of a listbox

Posted on 1998-10-23
6
250 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
cat dog challenge 18 122
WinWaitActive parameters 12 31
zeroFront challenge 7 71
sumHeights  challenge 17 61
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now