CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jan 2007
    Location
    Italy
    Posts
    156

    [RESOLVED] Print preview ASSERT problem

    Hi to all

    Maybe this is a stupid question, but I looked around in internet and searched this forum for this problem. I didn't found anything free and useful

    The fact is, I have an application SDI with a view that produces a custom report. When I go for print preview, I got an ASSERT from line 562 in afxwin1.inl. The code that Asserts is

    Code:
    _AFXWIN_INLINE HGDIOBJ CDC::SelectObject(HGDIOBJ hObject) // Safe for NULL handles
    	{ ASSERT(m_hDC == m_hAttribDC); // ASSERT a simple CDC object
    		return (hObject != NULL) ? ::SelectObject(m_hDC, hObject) : NULL; }
    The app continues working after hitting the ignore button, but I think I shoul solve this fact; i suspect doing something wrong.


    Thanks in advance for the help
    - Buzzyous -

  2. #2
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917

    Re: Print preview ASSERT problem

    It tells you that your CDC object does not have windows device context attached to it.
    I do not know how you instantiate CDC object but both: m_hAttribDC and m_hDC are initiated to the same value (0) when object is instantiated. You must have done something that changes one value. This function is safe if both values are NULL.
    By the way to construct a valid object, you should somehow attach windows device context by either using Attach or obtaining device context from the window, or use Createxxx to create device context.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  3. #3
    Join Date
    Jan 2007
    Location
    Italy
    Posts
    156

    Re: Print preview ASSERT problem

    I did nothing, just used the native OnPrint, importing a CRect with the size given by CPrintInfo struct in onDraw if (pDC->IsPrinting()).

    it is the first time I need to print something in visual c++, I thought OnPreparePrinting() and OnPrepareDC() initializes a printing device context and passes it to OnDraw when printing.
    Do I have to initialize something else regarding CDCs?

    I used OnPrint with pInfo's m_nCurPage member to direct my printing. But Preview launches this ASSERT. Ignoring it, however, it works.
    - Buzzyous -

  4. #4
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917

    Re: Print preview ASSERT problem

    Indeed, OnPreparePrinting() and OnPrepareDC() should initialize device context properly.
    If you do not need something fancy to print, OnDraw should handle printing as it were a screen.
    It is really hard to guess what is going on, without seeing the code.
    Woould it be possible to post zipped your (or sample) project that reproduces this error?
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  5. #5
    Join Date
    Jan 2007
    Location
    Italy
    Posts
    156

    Re: Print preview ASSERT problem

    I could post it, but there is nothing apart the standard functions:

    Code:
    void CFFView::OnInitialUpdate() 
    {
    	CScrollView::OnInitialUpdate();
    	
    	// TODO: Add your specialized code here and/or call the base class
    	CSize sizeTotal;
    	sizeTotal.cx = 0;
    	sizeTotal.cy = 0;
    	SetScrollSizes(MM_TEXT, sizeTotal);
    }
    
    BOOL CFFView::OnPreparePrinting(CPrintInfo* pInfo)
    {
    	// default preparation
    	return DoPreparePrinting(pInfo);
    }
    
    void CFFView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
    {
    	// TODO: add extra initialization before printing
    }
    
    void CFFView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
    {
    	// TODO: add cleanup after printing
    }
    
    void CFFView::OnPrint(CDC* pDC, CPrintInfo* pInfo) 
    {
    	// TODO: Add your specialized code here and/or call the base class
    	if (pInfo) 
    	{
    		printArea = pInfo->m_rectDraw;
    		curPrintPage = pInfo->m_nCurPage;
    	}
    
    //Page dependent print yet to implement
    
    	CScrollView::OnPrint(pDC, pInfo);
    }
    
    
    BOOL CFFView::PreCreateWindow(CREATESTRUCT& cs)
    {
    	// TODO: Modify the Window class or styles here by modifying
    	//  the CREATESTRUCT cs
    
    	return CScrollView::PreCreateWindow(cs);
    }
    
    /////////////////////////////////////////////////////////////////////////////
    // CYardManView drawing
    
    void CFFView::OnDraw(CDC* pDC)
    {
    	CFont* oldfont = pDC->SelectObject(&reportFont);	
    //reportFont is a class member of CFFView
    
    	CString de; de.Format("pag %i", curPrintPage); AfxMessageBox(de);
    
    	//I set some graphic params, like titles, margins and some other layout elements. 
    	DefineReportParam(view); //view is also a class member of CFFView
    
    	DrawReport(view, pDC); 
    /*A function that draws a report. It uses CDC::TextOut(...), CDC::LineTo(...), CDC::MoveTo(...), CDC::FillSolidRect(...), CDC::GetTextExtent(...), CDC:SetTextAlign(...), CDC::SetTextColor(...), CDC::SeletcObject(...), CFont::DeleteObject(...), CFont::CreatePointFont */
    
    	pDC->SelectObject(oldfont);
    }
    I did no custom CDC implementation, only passed the one given by OnDraw to a function specialized in drawing a custom report, which uses only standard graphics commands.

    It could be the passing of the CDC* to a function outside OnDraw, or maybe some graphic command?

    Still think I should post the full CFFView code or zip it? I will if you think it could help
    - Buzzyous -

  6. #6
    Join Date
    Jan 2007
    Location
    Italy
    Posts
    156

    Re: Print preview ASSERT problem

    Ok, I found it.

    The problem is about selecting a font created for the monitor device context into de printing device context.

    Sorry for the bothering, this is solved now

    Thank you, I should've debugged the program deeper before

    I am sorry
    - Buzzyous -

  7. #7
    Join Date
    Jan 2007
    Location
    Italy
    Posts
    156

    Re: [RESOLVED] Print preview ASSERT problem

    Actually, I realized something there is wrong

    My OnPaint() function, at its start, already receives the pDC CDC with the two members m_hDC and m_hAttribDC different,without me having done anything, while when no printing is being performed, the CDC in OnDraw hav coherent members

    The strange thing number 1 is that these two members are different in the printing device context that the framework itself creates.
    Also, I examined a brand new MFC SDI document, and the plain override of OnPrint exhibits exactly the same problem, different handler pointers for the printing device context, so this fact is frameword-dependent, not derived from my code.

    The second strange thing, strangest, is that I use SelectObject twice before the one that produces the ASSERT, which is produced only when I select a font, class member of my CScrollView derived class, initialized and created in its constructor.

    Any idea on why this happens?

    PS don't know how to un-Resolve the thread
    Last edited by Buzzyous; May 17th, 2012 at 08:59 PM. Reason: Deepening of the issue
    - Buzzyous -

  8. #8
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917

    Re: [RESOLVED] Print preview ASSERT problem

    Can you compress and post your project?
    I was trying to follow your instructions but I am not able to duplicate the problem.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured