home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / Basic / Visual Basic.60 / COMMON / TOOLS / VCM / VCM.MDB / VcmComponentContainer / 23_Cabinet / IMAGECTL.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1998-05-18  |  8.0 KB  |  285 lines

  1. // ImageCtl.cpp : Implementation of the CImageCtrl OLE control class.
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "stdafx.h"
  14. #include "image.h"
  15. #include "BmpProp.h"
  16. #include "ImageCtl.h"
  17. #include "ImagePpg.h"
  18.  
  19.  
  20. #ifdef _DEBUG
  21. #define new DEBUG_NEW
  22. #undef THIS_FILE
  23. static char THIS_FILE[] = __FILE__;
  24. #endif
  25.  
  26.  
  27. IMPLEMENT_DYNCREATE(CImageCtrl, COleControl)
  28.  
  29.  
  30. /////////////////////////////////////////////////////////////////////////////
  31. // Message map
  32.  
  33. BEGIN_MESSAGE_MAP(CImageCtrl, COleControl)
  34.     //{{AFX_MSG_MAP(CImageCtrl)
  35.     // NOTE - ClassWizard will add and remove message map entries
  36.     //    DO NOT EDIT what you see in these blocks of generated code !
  37.     //}}AFX_MSG_MAP
  38.     ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
  39. END_MESSAGE_MAP()
  40.  
  41.  
  42. /////////////////////////////////////////////////////////////////////////////
  43. // Dispatch map
  44.  
  45. BEGIN_DISPATCH_MAP(CImageCtrl, COleControl)
  46.     //{{AFX_DISPATCH_MAP(CImageCtrl)
  47.     DISP_PROPERTY_NOTIFY(CImageCtrl, "AutoSize", m_bAutoSize, OnAutoSizeChanged, VT_BOOL)
  48.     DISP_PROPERTY_EX(CImageCtrl, "ImagePath", GetImagePath, SetImagePath, VT_BSTR)
  49.     DISP_STOCKPROP_READYSTATE()
  50.     //}}AFX_DISPATCH_MAP
  51.     DISP_FUNCTION_ID(CImageCtrl, "AboutBox", DISPID_ABOUTBOX, AboutBox, VT_EMPTY, VTS_NONE)
  52. END_DISPATCH_MAP()
  53.  
  54.  
  55. /////////////////////////////////////////////////////////////////////////////
  56. // Event map
  57.  
  58. BEGIN_EVENT_MAP(CImageCtrl, COleControl)
  59.     //{{AFX_EVENT_MAP(CImageCtrl)
  60.     // NOTE - ClassWizard will add and remove event map entries
  61.     //    DO NOT EDIT what you see in these blocks of generated code !
  62.     EVENT_STOCK_READYSTATECHANGE()
  63.     //}}AFX_EVENT_MAP
  64. END_EVENT_MAP()
  65.  
  66.  
  67. /////////////////////////////////////////////////////////////////////////////
  68. // Property pages
  69.  
  70. BEGIN_PROPPAGEIDS(CImageCtrl, 1)
  71.     PROPPAGEID(CImagePropPage::guid)
  72. END_PROPPAGEIDS(CImageCtrl)
  73.  
  74. /////////////////////////////////////////////////////////////////////////////
  75. // Initialize class factory and guid
  76.  
  77. IMPLEMENT_OLECREATE_EX(CImageCtrl, "IMAGE.ImageCtrl.1",
  78.     0x346685e3, 0xc383, 0x11cf, 0xa5, 0xa4, 0, 0xaa, 0, 0xa4, 0x57, 0x5)
  79.  
  80. /////////////////////////////////////////////////////////////////////////////
  81. // Type library ID and version
  82.  
  83. IMPLEMENT_OLETYPELIB(CImageCtrl, _tlid, _wVerMajor, _wVerMinor)
  84.  
  85. /////////////////////////////////////////////////////////////////////////////
  86. // Interface IDs
  87.  
  88. const IID BASED_CODE IID_DImage =
  89.         { 0x346685e1, 0xc383, 0x11cf, { 0xa5, 0xa4, 0, 0xaa, 0, 0xa4, 0x57, 0x5 } };
  90. const IID BASED_CODE IID_DImageEvents =
  91.         { 0x346685e2, 0xc383, 0x11cf, { 0xa5, 0xa4, 0, 0xaa, 0, 0xa4, 0x57, 0x5 } };
  92.  
  93. /////////////////////////////////////////////////////////////////////////////
  94. // Control type information
  95.  
  96. static const DWORD BASED_CODE _dwImageOleMisc =
  97.     OLEMISC_ACTIVATEWHENVISIBLE |
  98.     OLEMISC_SETCLIENTSITEFIRST |
  99.     OLEMISC_INSIDEOUT |
  100.     OLEMISC_CANTLINKINSIDE |
  101.     OLEMISC_RECOMPOSEONRESIZE;
  102.  
  103. IMPLEMENT_OLECTLTYPE(CImageCtrl, IDS_IMAGE, _dwImageOleMisc)
  104.  
  105. /////////////////////////////////////////////////////////////////////////////
  106. // CImageCtrl::CImageCtrlFactory::UpdateRegistry -
  107. // Adds or removes system registry entries for CImageCtrl
  108.  
  109. BOOL CImageCtrl::CImageCtrlFactory::UpdateRegistry(BOOL bRegister)
  110. {
  111.     if (bRegister)
  112.         return AfxOleRegisterControlClass(
  113.             AfxGetInstanceHandle(),
  114.             m_clsid,
  115.             m_lpszProgID,
  116.             IDS_IMAGE,
  117.             IDB_IMAGE,
  118.             afxRegInsertable | afxRegApartmentThreading,
  119.             _dwImageOleMisc,
  120.             _tlid,
  121.             _wVerMajor,
  122.             _wVerMinor);
  123.     else
  124.         return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
  125. }
  126.  
  127.  
  128. /////////////////////////////////////////////////////////////////////////////
  129. // CImageCtrl::CImageCtrl - Constructor
  130.  
  131. #pragma warning(disable : 4355)
  132.  
  133. CImageCtrl::CImageCtrl() : m_bitprop(this), m_bAutoSize(TRUE)
  134. {
  135.     InitializeIIDs(&IID_DImage, &IID_DImageEvents);
  136.  
  137.     m_lReadyState = READYSTATE_UNINITIALIZED;
  138. }
  139.  
  140. #pragma warning(default : 4355)
  141.  
  142. /////////////////////////////////////////////////////////////////////////////
  143. // CImageCtrl::~CImageCtrl - Destructor
  144.  
  145. CImageCtrl::~CImageCtrl()
  146. {
  147. }
  148.  
  149.  
  150. /////////////////////////////////////////////////////////////////////////////
  151. // CImageCtrl::OnDraw - Drawing function
  152.  
  153. void CImageCtrl::OnDraw(
  154.             CDC* pDC, const CRect& rcBounds, const CRect& rcInvalid)
  155. {
  156.     if (m_bitprop.m_Bitmap.m_hObject != NULL)
  157.     {
  158.         CWindowDC screenDC(NULL);
  159.         CDC dc;
  160.         dc.CreateCompatibleDC(&screenDC);
  161.         CBitmap* pBitmap = dc.SelectObject(&m_bitprop.m_Bitmap);
  162.  
  163.         int cx(min(rcBounds.Width(), m_bitprop.m_BitmapSize.cx));
  164.         int cy(min(rcBounds.Height(), m_bitprop.m_BitmapSize.cy));
  165.  
  166.         pDC->BitBlt(rcBounds.left, rcBounds.top, cx, cy, &dc, 0, 0, SRCCOPY);
  167.         if (cx < rcBounds.Width())
  168.             pDC->PatBlt(cx, rcBounds.top, rcBounds.Width() - cx, rcBounds.Height(), BLACKNESS);
  169.         if (cy < rcBounds.Height())
  170.             pDC->PatBlt(rcBounds.left, cy, rcBounds.Width(), rcBounds.Height() - cy, BLACKNESS);
  171.  
  172.         if (!IsOptimizedDraw())
  173.         {
  174.             // The container does not support optimized drawing.
  175.             // So, we'll have to clean up what we did to the DC
  176.  
  177.             dc.SelectObject(pBitmap);
  178.         }
  179.     }
  180.     else
  181.     {
  182.         pDC->PatBlt(rcBounds.left, rcBounds.top,
  183.                     rcBounds.Width(), rcBounds.Height(), BLACKNESS);
  184.     }
  185.  
  186. }
  187.  
  188. /////////////////////////////////////////////////////////////////////////////
  189. // CImageCtrl::DoPropExchange - Persistence support
  190.  
  191. void CImageCtrl::DoPropExchange(CPropExchange* pPX)
  192. {
  193.     ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
  194.     COleControl::DoPropExchange(pPX);
  195.  
  196.     if (pPX->IsLoading())
  197.         InternalSetReadyState(READYSTATE_LOADING);
  198.  
  199.     PX_DataPath(pPX, _T("ImagePath"), m_bitprop);
  200.     PX_Bool(pPX, _T("AutoSize"), m_bAutoSize, TRUE);
  201. }
  202.  
  203.  
  204. /////////////////////////////////////////////////////////////////////////////
  205. // CImageCtrl::GetControlFlags -
  206. // Flags to customize MFC's implementation of ActiveX controls.
  207. //
  208. // For information on using these flags, please see MFC technical note
  209. // #nnn, "Optimizing an ActiveX Control".
  210. DWORD CImageCtrl::GetControlFlags()
  211. {
  212.     DWORD dwFlags = COleControl::GetControlFlags();
  213.  
  214.     // The control can activate without creating a window.
  215.     dwFlags |= windowlessActivate;
  216.  
  217.     // The control can optimize its OnDraw method, by not restoring
  218.     // the original GDI objects in the device context.
  219.     dwFlags |= canOptimizeDraw;
  220.     return dwFlags;
  221. }
  222.  
  223. /////////////////////////////////////////////////////////////////////////////
  224. // CImageCtrl::OnResetState - Reset control to default state
  225.  
  226. void CImageCtrl::OnResetState()
  227. {
  228.     m_bitprop.ResetData();
  229.     COleControl::OnResetState();  // Resets defaults found in DoPropExchange
  230. }
  231.  
  232.  
  233. /////////////////////////////////////////////////////////////////////////////
  234. // CImageCtrl::AboutBox - Display an "About" box to the user
  235.  
  236. void CImageCtrl::AboutBox()
  237. {
  238.     CDialog dlgAbout(IDD_ABOUTBOX_IMAGE);
  239.     dlgAbout.DoModal();
  240. }
  241.  
  242. /////////////////////////////////////////////////////////////////////////////
  243. // CImageCtrl message handlers
  244.  
  245. void CImageCtrl::InformDlStatus(CBitmapProperty::DLState dlState)
  246. {
  247.     Invalidate(FALSE);
  248.     if (dlState == CBitmapProperty::DLState::dlDone)
  249.         InternalSetReadyState(READYSTATE_COMPLETE);
  250. }
  251.  
  252. void CImageCtrl::AutoSize()
  253. {
  254.     ASSERT(m_bAutoSize);
  255.  
  256.     SetControlSize(m_bitprop.m_BitmapSize.cx, m_bitprop.m_BitmapSize.cy);
  257. }
  258.  
  259. void CImageCtrl::OnAutoSizeChanged()
  260. {
  261.     if (m_bAutoSize)
  262.         AutoSize();
  263.  
  264.     SetModifiedFlag();
  265. }
  266.  
  267. void CImageCtrl::InformSize()
  268. {
  269.     if (m_bAutoSize)
  270.         AutoSize();
  271.     InternalSetReadyState(READYSTATE_INTERACTIVE);
  272. }
  273.  
  274. BSTR CImageCtrl::GetImagePath()
  275. {
  276.     return m_bitprop.GetPath().AllocSysString();
  277. }
  278.  
  279. void CImageCtrl::SetImagePath(LPCTSTR lpszNewValue)
  280. {
  281.     Load(lpszNewValue, m_bitprop);
  282.     Invalidate(FALSE);
  283.     SetModifiedFlag();
  284. }
  285.