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?

[Webinar] Streamline your web hosting managementRegister Today

x
 
chensuConnect With a Mentor Commented:
Thank you.
0
 
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
All Courses

From novice to tech pro — start learning today.