home *** CD-ROM | disk | FTP | other *** search
/ Mastering MFC Development / MMD.ISO / labs / c12 / lab01 / ex01 / palviewctl.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-20  |  6.5 KB  |  253 lines

  1. // PalViewCtl.cpp : Implementation of the CPalViewCtrl ActiveX Control class.
  2.  
  3. #include "stdafx.h"
  4. #include "PalView.h"
  5.  
  6. #include "dib.h"
  7. #include "dibpal.h"
  8.  
  9. #include "PalViewCtl.h"
  10. #include "PalViewPpg.h"
  11.  
  12.  
  13. #ifdef _DEBUG
  14. #define new DEBUG_NEW
  15. #undef THIS_FILE
  16. static char THIS_FILE[] = __FILE__;
  17. #endif
  18.  
  19.  
  20. IMPLEMENT_DYNCREATE(CPalViewCtrl, COleControl)
  21.  
  22.  
  23. /////////////////////////////////////////////////////////////////////////////
  24. // Message map
  25.  
  26. BEGIN_MESSAGE_MAP(CPalViewCtrl, COleControl)
  27.     //{{AFX_MSG_MAP(CPalViewCtrl)
  28.     // NOTE - ClassWizard will add and remove message map entries
  29.     //    DO NOT EDIT what you see in these blocks of generated code !
  30.     //}}AFX_MSG_MAP
  31.     ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
  32. END_MESSAGE_MAP()
  33.  
  34.  
  35. /////////////////////////////////////////////////////////////////////////////
  36. // Dispatch map
  37.  
  38. BEGIN_DISPATCH_MAP(CPalViewCtrl, COleControl)
  39.     //{{AFX_DISPATCH_MAP(CPalViewCtrl)
  40.     DISP_PROPERTY_NOTIFY(CPalViewCtrl, "DibFileName", m_dibFileName, OnDibFileNameChanged, VT_BSTR)
  41.     //}}AFX_DISPATCH_MAP
  42.     DISP_FUNCTION_ID(CPalViewCtrl, "AboutBox", DISPID_ABOUTBOX, AboutBox, VT_EMPTY, VTS_NONE)
  43. END_DISPATCH_MAP()
  44.  
  45.  
  46. /////////////////////////////////////////////////////////////////////////////
  47. // Event map
  48.  
  49. BEGIN_EVENT_MAP(CPalViewCtrl, COleControl)
  50.     //{{AFX_EVENT_MAP(CPalViewCtrl)
  51.     // NOTE - ClassWizard will add and remove event map entries
  52.     //    DO NOT EDIT what you see in these blocks of generated code !
  53.     //}}AFX_EVENT_MAP
  54. END_EVENT_MAP()
  55.  
  56.  
  57. /////////////////////////////////////////////////////////////////////////////
  58. // Property pages
  59.  
  60. // TODO: Add more property pages as needed.  Remember to increase the count!
  61. BEGIN_PROPPAGEIDS(CPalViewCtrl, 1)
  62.     PROPPAGEID(CPalViewPropPage::guid)
  63. END_PROPPAGEIDS(CPalViewCtrl)
  64.  
  65.  
  66. /////////////////////////////////////////////////////////////////////////////
  67. // Initialize class factory and guid
  68.  
  69. IMPLEMENT_OLECREATE_EX(CPalViewCtrl, "PALVIEW.PalViewCtrl.1",
  70.     0x7f7767c3, 0x365b, 0x11d0, 0x9c, 0x9d, 0, 0xa0, 0xd1, 0, 0xe3, 0xc8)
  71.  
  72.  
  73. /////////////////////////////////////////////////////////////////////////////
  74. // Type library ID and version
  75.  
  76. IMPLEMENT_OLETYPELIB(CPalViewCtrl, _tlid, _wVerMajor, _wVerMinor)
  77.  
  78.  
  79. /////////////////////////////////////////////////////////////////////////////
  80. // Interface IDs
  81.  
  82. const IID BASED_CODE IID_DPalView =
  83.         { 0x7f7767c1, 0x365b, 0x11d0,
  84.         { 0x9c, 0x9d, 0, 0xa0, 0xd1, 0, 0xe3, 0xc8 } };
  85. const IID BASED_CODE IID_DPalViewEvents =
  86.         { 0x7f7767c2, 0x365b, 0x11d0, 
  87.         { 0x9c, 0x9d, 0, 0xa0, 0xd1, 0, 0xe3, 0xc8 } };
  88.  
  89.  
  90. /////////////////////////////////////////////////////////////////////////////
  91. // Control type information
  92.  
  93. static const DWORD BASED_CODE _dwPalViewOleMisc =
  94.     OLEMISC_ACTIVATEWHENVISIBLE |
  95.     OLEMISC_SETCLIENTSITEFIRST |
  96.     OLEMISC_INSIDEOUT |
  97.     OLEMISC_CANTLINKINSIDE |
  98.     OLEMISC_RECOMPOSEONRESIZE;
  99.  
  100. IMPLEMENT_OLECTLTYPE(CPalViewCtrl, IDS_PALVIEW, _dwPalViewOleMisc)
  101.  
  102.  
  103. /////////////////////////////////////////////////////////////////////////////
  104. // CPalViewCtrl::CPalViewCtrlFactory::UpdateRegistry -
  105. // Adds or removes system registry entries for CPalViewCtrl
  106.  
  107. BOOL CPalViewCtrl::CPalViewCtrlFactory::UpdateRegistry(BOOL bRegister)
  108. {
  109.     // TODO: Verify that your control follows apartment-model threading rules.
  110.     // Refer to MFC TechNote 64 for more information.
  111.     // If your control does not conform to the apartment-model rules, then
  112.     // you must modify the code below, changing the 6th parameter from
  113.     // afxRegApartmentThreading to 0.
  114.  
  115.     if (bRegister)
  116.         return AfxOleRegisterControlClass(
  117.             AfxGetInstanceHandle(),
  118.             m_clsid,
  119.             m_lpszProgID,
  120.             IDS_PALVIEW,
  121.             IDB_PALVIEW,
  122.             afxRegApartmentThreading,
  123.             _dwPalViewOleMisc,
  124.             _tlid,
  125.             _wVerMajor,
  126.             _wVerMinor);
  127.     else
  128.         return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
  129. }
  130.  
  131.  
  132. /////////////////////////////////////////////////////////////////////////////
  133. // CPalViewCtrl::CPalViewCtrl - Constructor
  134.  
  135. CPalViewCtrl::CPalViewCtrl()
  136. {
  137.     InitializeIIDs(&IID_DPalView, &IID_DPalViewEvents);
  138.  
  139.     m_pDibPal = 0;
  140.     m_dibFileName = _T("");
  141. }
  142.  
  143.  
  144. /////////////////////////////////////////////////////////////////////////////
  145. // CPalViewCtrl::~CPalViewCtrl - Destructor
  146.  
  147. CPalViewCtrl::~CPalViewCtrl()
  148. {
  149.     if (0 != m_pDibPal)
  150.     {
  151.         delete m_pDibPal;
  152.         m_pDibPal = 0;
  153.     }
  154. }
  155.  
  156.  
  157. /////////////////////////////////////////////////////////////////////////////
  158. // CPalViewCtrl::OnDraw - Drawing function
  159.  
  160. void CPalViewCtrl::OnDraw(
  161.             CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
  162. {
  163.     if(0 != m_pDibPal)
  164.     {
  165.         m_pDibPal->Draw(pdc, rcBounds, TRUE); 
  166.     }
  167.     else
  168.     {
  169.         pdc->FillRect(rcBounds, 
  170.             CBrush::FromHandle((HBRUSH)GetStockObject(WHITE_BRUSH)));
  171.     }
  172. }
  173.  
  174.  
  175. /////////////////////////////////////////////////////////////////////////////
  176. // CPalViewCtrl::DoPropExchange - Persistence support
  177.  
  178. void CPalViewCtrl::DoPropExchange(CPropExchange* pPX)
  179. {
  180.     ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
  181.     COleControl::DoPropExchange(pPX);
  182.  
  183.     // TODO: Call PX_ functions for each persistent custom property.
  184. }
  185.  
  186.  
  187. /////////////////////////////////////////////////////////////////////////////
  188. // CPalViewCtrl::OnResetState - Reset control to default state
  189.  
  190. void CPalViewCtrl::OnResetState()
  191. {
  192.     COleControl::OnResetState();  // Resets defaults found in DoPropExchange
  193.  
  194.     // TODO: Reset any other control state here.
  195. }
  196.  
  197.  
  198. /////////////////////////////////////////////////////////////////////////////
  199. // CPalViewCtrl::AboutBox - Display an "About" box to the user
  200.  
  201. void CPalViewCtrl::AboutBox()
  202. {
  203.     CDialog dlgAbout(IDD_ABOUTBOX_PALVIEW);
  204.     dlgAbout.DoModal();
  205. }
  206.  
  207.  
  208. /////////////////////////////////////////////////////////////////////////////
  209. // CPalViewCtrl message handlers
  210. void CPalViewCtrl::ExtractPalFromDIBFile(LPCTSTR lpszFileSpec)
  211. {
  212.     CDIB    Dib;
  213.     CFile    DibFile;
  214.     
  215.     if(DibFile.Open(lpszFileSpec, CFile::modeRead))
  216.     {
  217.         // Load the bitmap file into a CDIB object
  218.         if(Dib.Load(&DibFile))
  219.         {
  220.             CDIBPal* pTemp = new CDIBPal;
  221.             ASSERT(pTemp != NULL);
  222.         
  223.             //    This will extract the color table fron the CDIB object
  224.             if(pTemp->Create(&Dib))
  225.             {
  226.                 //    Get rid of any existing CDIBPal object
  227.                 if(0 != m_pDibPal)
  228.                 {
  229.                     delete m_pDibPal;
  230.                     m_pDibPal = 0;
  231.                 }
  232.                 //    Ready to go
  233.                 m_pDibPal = pTemp;
  234.         
  235.                 //    Make sure the new palette reflects our current state
  236.                 m_pDibPal->SetDraw3D(TRUE);
  237.             }
  238.             else
  239.             {
  240.                 delete pTemp;
  241.             }
  242.         }
  243.     }
  244. }
  245.  
  246. void CPalViewCtrl::OnDibFileNameChanged() 
  247. {
  248.     ExtractPalFromDIBFile(m_dibFileName);
  249.     InvalidateControl();
  250.  
  251.     SetModifiedFlag();
  252. }
  253.