home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 23 / IOPROG_23.ISO / SOFT / VFORM.ZIP / Samples / vfTest / PalHook.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-10  |  3.9 KB  |  150 lines

  1. ////////////////////////////////////////////////////////////////
  2. // Copyright 1996 Microsoft Systems Journal. 
  3. // If this program works, it was written by Paul DiLascia.
  4. // If not, I don't know who wrote it.
  5. //
  6. #include "StdAfx.h"
  7. #include "PalHook.h"
  8. #include "Debug.h"
  9.  
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15.  
  16. // To turn on tracing for palette messages, you need
  17. // to define _TRACEPAL here or in your make file
  18. //#define _TRACEPAL
  19.  
  20. #ifndef _TRACEPAL
  21. #undef TRACEFN
  22. #undef TRACE
  23. #undef TRACE0
  24. #define TRACEFN CString().Format
  25. #define TRACE CString().Format
  26. #define TRACE0 CString().Format
  27. #endif
  28.  
  29. IMPLEMENT_DYNAMIC(CPalMsgHandler, CMsgHook);
  30.  
  31. CPalMsgHandler::CPalMsgHandler()
  32. {
  33.     m_pPalette = NULL;
  34. }
  35.  
  36. CPalMsgHandler::~CPalMsgHandler()
  37. {
  38. }
  39.  
  40. //////////////////
  41. // Message handler handles palette-related messages
  42. //
  43. LRESULT CPalMsgHandler::WindowProc(UINT msg, WPARAM wp, LPARAM lp)
  44. {
  45.     ASSERT_VALID(m_pWndHooked);
  46.  
  47.     switch (msg) {
  48.     case WM_PALETTECHANGED:
  49.         OnPaletteChanged(CWnd::FromHandle((HWND)wp));
  50.         return 0;
  51.     case WM_QUERYNEWPALETTE:
  52.         return OnQueryNewPalette();
  53.     case WM_SETFOCUS:
  54.         OnSetFocus(CWnd::FromHandle((HWND)wp));
  55.         return 0;
  56.     }
  57.     return CMsgHook::WindowProc(msg, wp, lp);
  58. }
  59.  
  60. //////////////////
  61. // Handle WM_PALETTECHANGED
  62. //
  63. void CPalMsgHandler::OnPaletteChanged(CWnd* pFocusWnd)
  64. {
  65.     ASSERT(m_pWndHooked);
  66.     CWnd& wnd = *m_pWndHooked;
  67.     TRACEFN(_T("CPalMsgHandler::OnPaletteChanged for %s [from %s]\n"), 
  68.         DbgName(&wnd), DbgName(pFocusWnd));
  69.  
  70.     if (pFocusWnd->GetSafeHwnd() != wnd.m_hWnd) {
  71.         if (DoRealizePalette(FALSE)==0) {
  72.             if (wnd.GetParent()==NULL) {
  73.                 // I'm the top-level frame: Broadcast to children
  74.                 // (only MFC permanent CWnd's!)
  75.                 //
  76.                 const MSG& curMsg = AfxGetThreadState()->m_lastSentMsg;
  77.                 wnd.SendMessageToDescendants(WM_PALETTECHANGED,
  78.                     curMsg.wParam, curMsg.lParam);
  79.             }
  80.         }
  81.     } else {
  82.         // I'm the window that triggered the WM_PALETTECHANGED
  83.         // in the first place: ignore it
  84.         //
  85.         TRACE(_T("[It's me, don't realize palette.]\n"));
  86.     }
  87. }
  88.  
  89. //////////////////
  90. // Handle WM_QUERYNEWPALETTE
  91. //
  92. BOOL CPalMsgHandler::OnQueryNewPalette()
  93. {
  94.     ASSERT(m_pWndHooked);
  95.     CWnd& wnd = *m_pWndHooked;
  96.     TRACEFN(_T("CPalMsgHandler::OnQueryNewPalette for %s\n"), DbgName(&wnd));
  97.  
  98.     if (DoRealizePalette(TRUE)==0) {    // realize in foreground
  99.         // No colors changed: if this is the top-level frame,
  100.         // give active view a chance to realize itself
  101.         //
  102.         if (wnd.GetParent()==NULL) {
  103.             ASSERT_KINDOF(CFrameWnd, &wnd);
  104.             CWnd* pView = ((CFrameWnd&)wnd).GetActiveFrame()->GetActiveView();
  105.             if (pView) 
  106.                 pView->SendMessage(WM_QUERYNEWPALETTE);
  107.         }
  108.     }
  109.     return TRUE;
  110. }
  111.  
  112. //////////////////
  113. // Handle WM_SETFOCUS
  114. //
  115. void CPalMsgHandler::OnSetFocus(CWnd* pOldWnd) 
  116. {
  117.     ASSERT(m_pWndHooked);
  118.     CWnd& wnd = *m_pWndHooked;
  119.     TRACEFN(_T("CPalMsgHandler::OnSetFocus for %s\n"), DbgName(&wnd));
  120.     wnd.SetForegroundWindow();        // Windows likes this
  121.     DoRealizePalette(TRUE);            // realize in foreground
  122.     Default();                            // let app handle focus message too
  123. }
  124.  
  125. /////////////////
  126. // Function to actually realize the palette.
  127. // Override this to do different kind of palette realization; e.g.,
  128. // DrawDib instead of setting the CPalette.
  129. //
  130. int CPalMsgHandler::DoRealizePalette(BOOL bForeground)
  131. {
  132.     if (!m_pPalette || !m_pPalette->m_hObject)
  133.         return 0;
  134.  
  135.     ASSERT(m_pWndHooked);
  136.     CWnd& wnd = *m_pWndHooked;
  137.     TRACEFN(_T("CPalMsgHandler::DoRealizePalette(%s) for %s\n"),
  138.         bForeground ? _T("foreground") : _T("background"), DbgName(&wnd));
  139.  
  140.     CClientDC dc(&wnd);
  141.     CPalette* pOldPal = dc.SelectPalette(m_pPalette, !bForeground);
  142.     int nColorsChanged = dc.RealizePalette();
  143.     if (pOldPal)
  144.         dc.SelectPalette(pOldPal, TRUE);
  145.     if (nColorsChanged > 0)
  146.         wnd.Invalidate(FALSE); // repaint
  147.     TRACE(_T("[%d colors changed]\n"), nColorsChanged);
  148.     return nColorsChanged;
  149. }
  150.