Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My app is so compute bound that it can run for on the order of a minute. During that time, it is drawing to the screen, but you cannot stop it. The message pumps have stopped, I presume. I have solved the painting problem but not the printing problem, so I'll give you the gist of painting. I start the worker thread in OnPaint and pass the thread parms the "this" pointer and HWND hwnd. The hwnd comes from GetSafeHwnd(). OnPaint is now done and returns.

I've learned not to use CDC In the worker (painter) thread, I do this:

C++
HDC hdc = GetDC(ps->hwnd);   // ps = pointer to thread parm structure
DrawAll(hdc);     // heavy duty computing here
ReleaseDC(ps->hwnd);
return;

OnPrint() is similar to OnPaint() but is a tad more complicated. The starter thread looks like this:

C++
void CMyView::StartPrinterThread(CDC* pdc, CPrintInfo* pInfo)
{
    PrintParam.pv = (CMyView*)this;
    CMyDoc* pDoc = GetDocument();
    PrintParam.pInfo = pInfo;

    pDoc->m_bStopThread = FALSE; // so can stop with a mouse click
    ResetEvent(pDoc->m_hevPrinter);
    AfxBeginThread(PrinterThread,&PrintParam);
    WaitForSingleObject(pDoc->m_hevPrinter, INFINITE);
}

static UINT PrinterThread(LPVOID pParam)
{
    CPrintInfo* pInfo;
    HDC hdc;
    RECT rcl;
    POINT Org;
    PRINTPARMSTRUCT* ps = (PRINTPARMSTRUCT*)pParam;
    // some app specific stuff
    CMyDoc* pDoc = ps->pv->GetDocument();
    pInfo = ps->pInfo;
    hdc = pInfo->m_pPD->m_pd.hDC;
    SetMapMode(hdc, MM_LOENGLISH);
    rcl = pInfo->m_rectDraw;
    ...
    SetEvent(pDoc->m_hevPrinter);  // starter thread going
    DrawAll(hdc);     // heavy duty computing here
    return 0;
}


My app actually runs as expected but for a small detail. The print preview page and the printed page are blank. I hope it is something small that I am missing.

Tom Stokes
Posted
Comments
Richard MacCutchan 1-Jan-15 5:45am    
It appears (although the code is not clear) that you are starting a separate thread to do your painting/printing, while the main thread goes into a wait state. What benefit do you expect from this?
Tom Stokes 1-Jan-15 17:05pm    
OnPaint() and OnPrint() start threads but they do not run concurrently of course. The wait event in the main thread lasts only long enough for the printer thread to start and get ahold of the hDC. The printer thread then does a SetEvent(). The main thread is now able to respond to messages.

1 solution

You may like to look at http://msdn.microsoft.com/en-us/library/3dy7kd92.aspx[^] for a description of the best way to handle compute bound situations.
 
Share this answer
 
Comments
Tom Stokes 1-Jan-15 17:05pm    
Perhaps I should have mentioned that the app is for entertainment purposes. It creates abstract art and draws it to the screen at the same time. You just sit and watch. In that sense, you could say being compute bound is a good thing! The printed output is the final result and can be saved as a *.jpg.

As I said, the program works except printed output is blank. Do I need to do something else with the hDC?
Richard MacCutchan 2-Jan-15 4:18am    
I can only assume that your printer DC is not being set up correctly.
Tom Stokes 3-Jan-15 12:40pm    
This is about the only thing I do:

HDC hdc = pInfo->m_pPD->m_pd.hDC;
Richard MacCutchan 3-Jan-15 12:48pm    
Well I am afraid no one can guess whether that is correct or not. You need to use your debugger to collect more useful information.
Tom Stokes 4-Jan-15 20:33pm    
I used the debugger to step through CView::DoPreparePrinting(pInfo). That is where I found HDC hdc = pInfo->m_pPD->m_pd.hDC in the first place. It looks to be valid for both print preview or a printer selected from the print dialog box.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900