Solved

Print contents of a listbox

Posted on 1998-10-23
6
255 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Looking for a specific application/software 2 109
isEverywhere  challenge 19 75
wordmultiple challenge 12 131
Thin secure Windows 10 5 93
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
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.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

785 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