How to get DC from callback

Hi,
  I need to get the CDC during my callback so that I can draw a graph. The code below doesnt work. CDC is NULL;

Thanks,
  Jer

VOID CALLBACK TimerProc(
  HWND hwnd,     // handle of window for timer messages
  UINT uMsg,     // WM_TIMER message
  UINT idEvent,  // timer identifier
  DWORD dwTime   // current system time
  ){


      g_pDC = (CDC*) GetDC( AfxGetApp()->GetMainWnd()->GetSafeHwnd() );

      theApp.GetGraph()->m_pThis->Draw( g_pDC );

      ReleaseDC( AfxGetApp()->GetMainWnd()->GetSafeHwnd(), (HDC)g_pDC );

}
pipeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chensuCommented:
The way to do it should be to post a user-defined message to the window responsible for drawing the graph. When the window receives the user-defined message, get the DC and draw the graph.

#define    WM_USER_DRAWGRAPH    (WM_USER + 1)

CWnd *pWnd = ::AfxGetApp()->GetMainWnd();
if (pWnd != NULL)
    pWnd->PostMessage(WM_USER_DRAWGRAPH);


ON_MESSAGE(WM_USER_DRAWGRAPH, OnDrawGraph)

....
0
pipeAuthor Commented:

in my timer callback:


VOID CALLBACK TimerProc(
  HWND hwnd,     // handle of window for timer messages
  UINT uMsg,     // WM_TIMER message
  UINT idEvent,  // timer identifier
  DWORD dwTime   // current system time
  ){

      CWnd *pWnd = ::AfxGetApp()->GetMainWnd();
      
      if (pWnd != NULL){
            pWnd->PostMessage(WM_USER_DRAWGRAPH);
      }

}

the message gets sent off correctly however the function itself never fires.


void CMyView::OnDrawGraph(){

      static CDC* pDC;

      pDC = GetDC();
      theApp.GetGraph()->Draw( pDC );
      ReleaseDC( pDC );

}


I added this to the message map:

      ON_MESSAGE(WM_USER_DRAWGRAPH, OnDrawGraph)

i dont understand why it isnt getting called.
0
pipeAuthor Commented:
i think i kno why but am not sure how to fix it. im in the CMyView class of my mfc app and the message is getting posted to the main window and so this function will never get the message. how can i get around this?
0
pipeAuthor Commented:
never mind i got it now. answer it again and ill give you your points.

VOID CALLBACK TimerProc(
  HWND hwnd,     // handle of window for timer messages
  UINT uMsg,     // WM_TIMER message
  UINT idEvent,  // timer identifier
  DWORD dwTime   // current system time
  ){


      ::PostMessage( hwnd, WM_USER_DRAWGRAPH, 0, 0 );

}

Thanks!
0
chensuCommented:
Thank you.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.