⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 colorchgctl.cpp

📁 Visual C++ From the Ground Up Second Edition 例程源代码
💻 CPP
字号:
// ColorChgCtl.cpp : Implementation of the CColorChgCtrl ActiveX Control class.

#include "stdafx.h"
#include "ColorChg.h"
#include "ColorChgCtl.h"
#include "ColorChgPpg.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


IMPLEMENT_DYNCREATE(CColorChgCtrl, COleControl)


/////////////////////////////////////////////////////////////////////////////
// Message map

BEGIN_MESSAGE_MAP(CColorChgCtrl, COleControl)
	//{{AFX_MSG_MAP(CColorChgCtrl)
	// NOTE - ClassWizard will add and remove message map entries
	//    DO NOT EDIT what you see in these blocks of generated code !
	//}}AFX_MSG_MAP
	ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// Dispatch map

BEGIN_DISPATCH_MAP(CColorChgCtrl, COleControl)
	//{{AFX_DISPATCH_MAP(CColorChgCtrl)
	DISP_PROPERTY_NOTIFY(CColorChgCtrl, "BackgroundColor", m_backgroundColor, OnBackgroundColorChanged, VT_BSTR)
	DISP_PROPERTY_NOTIFY(CColorChgCtrl, "ForegroundColor", m_foregroundColor, OnForegroundColorChanged, VT_BSTR)
	DISP_PROPERTY_NOTIFY(CColorChgCtrl, "VisitedColor", m_visitedColor, OnVisitedColorChanged, VT_BSTR)
	DISP_PROPERTY_NOTIFY(CColorChgCtrl, "LinkColor", m_linkColor, OnLinkColorChanged, VT_BSTR)
	DISP_PROPERTY_NOTIFY(CColorChgCtrl, "ColorAvailable", m_colorAvailable, OnColorAvailableChanged, VT_BOOL)
	DISP_FUNCTION(CColorChgCtrl, "GetColor", GetColor, VT_BOOL, VTS_NONE)
	DISP_FUNCTION(CColorChgCtrl, "SetColor", SetColor, VT_BOOL, VTS_NONE)
	//}}AFX_DISPATCH_MAP
	DISP_FUNCTION_ID(CColorChgCtrl, "AboutBox", DISPID_ABOUTBOX, AboutBox, VT_EMPTY, VTS_NONE)
END_DISPATCH_MAP()


/////////////////////////////////////////////////////////////////////////////
// Event map

BEGIN_EVENT_MAP(CColorChgCtrl, COleControl)
	//{{AFX_EVENT_MAP(CColorChgCtrl)
	EVENT_CUSTOM("OnColorLoad", FireOnColorLoad, VTS_NONE)
	//}}AFX_EVENT_MAP
END_EVENT_MAP()


/////////////////////////////////////////////////////////////////////////////
// Property pages

// TODO: Add more property pages as needed.  Remember to increase the count!
BEGIN_PROPPAGEIDS(CColorChgCtrl, 1)
	PROPPAGEID(CColorChgPropPage::guid)
END_PROPPAGEIDS(CColorChgCtrl)


/////////////////////////////////////////////////////////////////////////////
// Initialize class factory and guid

IMPLEMENT_OLECREATE_EX(CColorChgCtrl, "COLORCHG.ColorChgCtrl.1",
	0x428ca4a4, 0xa5c8, 0x11d0, 0x98, 0x87, 0, 0xa0, 0x24, 0xda, 0x5f, 0xb)


/////////////////////////////////////////////////////////////////////////////
// Type library ID and version

IMPLEMENT_OLETYPELIB(CColorChgCtrl, _tlid, _wVerMajor, _wVerMinor)


/////////////////////////////////////////////////////////////////////////////
// Interface IDs

const IID BASED_CODE IID_DColorChg =
		{ 0x428ca4a2, 0xa5c8, 0x11d0, { 0x98, 0x87, 0, 0xa0, 0x24, 0xda, 0x5f, 0xb } };
const IID BASED_CODE IID_DColorChgEvents =
		{ 0x428ca4a3, 0xa5c8, 0x11d0, { 0x98, 0x87, 0, 0xa0, 0x24, 0xda, 0x5f, 0xb } };


/////////////////////////////////////////////////////////////////////////////
// Control type information

static const DWORD BASED_CODE _dwColorChgOleMisc =
	OLEMISC_INVISIBLEATRUNTIME |
	OLEMISC_ACTIVATEWHENVISIBLE |
	OLEMISC_SETCLIENTSITEFIRST |
	OLEMISC_INSIDEOUT |
	OLEMISC_CANTLINKINSIDE |
	OLEMISC_RECOMPOSEONRESIZE;

IMPLEMENT_OLECTLTYPE(CColorChgCtrl, IDS_COLORCHG, _dwColorChgOleMisc)


/////////////////////////////////////////////////////////////////////////////
// CColorChgCtrl::CColorChgCtrlFactory::UpdateRegistry -
// Adds or removes system registry entries for CColorChgCtrl

BOOL CColorChgCtrl::CColorChgCtrlFactory::UpdateRegistry(BOOL bRegister)
{
	// TODO: Verify that your control follows apartment-model threading rules.
	// Refer to MFC TechNote 64 for more information.
	// If your control does not conform to the apartment-model rules, then
	// you must modify the code below, changing the 6th parameter from
	// afxRegApartmentThreading to 0.

	if (bRegister)
		return AfxOleRegisterControlClass(
			AfxGetInstanceHandle(),
			m_clsid,
			m_lpszProgID,
			IDS_COLORCHG,
			IDB_COLORCHG,
			afxRegApartmentThreading,
			_dwColorChgOleMisc,
			_tlid,
			_wVerMajor,
			_wVerMinor);
	else
		return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
}


/////////////////////////////////////////////////////////////////////////////
// CColorChgCtrl::CColorChgCtrl - Constructor

CColorChgCtrl::CColorChgCtrl()
{
   char*         szFileName;   // Name of the file to open.
   CFileStatus   oStatus;      // Status of file object.

   InitializeIIDs(&IID_DColorChg, &IID_DColorChgEvents);

   // Initialize our color properties to some default value.
   m_backgroundColor = "WHITE";
   m_foregroundColor = "BLACK";
   m_linkColor = "GREEN";
   m_visitedColor = "RED";

   // See if there's a color file on disk.
   szFileName = "C:\\COLORSET.TXT";
   m_colorAvailable = CFile::GetStatus(szFileName, oStatus);
}


/////////////////////////////////////////////////////////////////////////////
// CColorChgCtrl::~CColorChgCtrl - Destructor

CColorChgCtrl::~CColorChgCtrl()
{
	// TODO: Cleanup your control's instance data here.
}


/////////////////////////////////////////////////////////////////////////////
// CColorChgCtrl::OnDraw - Drawing function

void CColorChgCtrl::OnDraw(
			CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
{
	// TODO: Replace the following code with your own drawing code.
	pdc->FillRect(rcBounds, CBrush::FromHandle((HBRUSH)GetStockObject(WHITE_BRUSH)));
	pdc->Ellipse(rcBounds);
}


/////////////////////////////////////////////////////////////////////////////
// CColorChgCtrl::DoPropExchange - Persistence support

void CColorChgCtrl::DoPropExchange(CPropExchange* pPX)
{
	ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
	COleControl::DoPropExchange(pPX);

	// TODO: Call PX_ functions for each persistent custom property.

}


/////////////////////////////////////////////////////////////////////////////
// CColorChgCtrl::OnResetState - Reset control to default state

void CColorChgCtrl::OnResetState()
{
	COleControl::OnResetState();  // Resets defaults found in DoPropExchange

	// TODO: Reset any other control state here.
}


/////////////////////////////////////////////////////////////////////////////
// CColorChgCtrl::AboutBox - Display an "About" box to the user

void CColorChgCtrl::AboutBox()
{
	CDialog dlgAbout(IDD_ABOUTBOX_COLORCHG);
	dlgAbout.DoModal();
}


/////////////////////////////////////////////////////////////////////////////
// CColorChgCtrl message handlers

void CColorChgCtrl::OnBackgroundColorChanged() 
{
	// TODO: Add notification handler code

	SetModifiedFlag();
}

void CColorChgCtrl::OnForegroundColorChanged() 
{
	// TODO: Add notification handler code

	SetModifiedFlag();
}

void CColorChgCtrl::OnVisitedColorChanged() 
{
	// TODO: Add notification handler code

	SetModifiedFlag();
}

void CColorChgCtrl::OnLinkColorChanged() 
{
	// TODO: Add notification handler code

	SetModifiedFlag();
}

BOOL CColorChgCtrl::GetColor() 
{
   char*            szFileName;   // Name of the file to open.
   CFileStatus      oStatus;      // Status of file object.
   CFile            oFile;        // File object for sending data to the drive.
   CFileException   oException;   // File opening exception.
   char             szData[99];   // Data storage variable.

   // See if there's a color file on disk.
   szFileName = "C:\\COLORSET.TXT";
   if (! CFile::GetStatus(szFileName, oStatus))
   {
      // Make sure the color detection variable is set right.
      m_colorAvailable = FALSE;

      // Exit the routine.
      return FALSE;
   }

   // Open the file for reading.
   if (oFile.Open(szFileName, CFile::modeRead, &oException))
   {
      // Read the file.
      oFile.Read(szData, sizeof(szData));

     // Store the color settings in our properties.
     m_backgroundColor = strtok(szData, ",");
     m_foregroundColor = strtok(NULL, ",");
     m_linkColor = strtok(NULL, ",");
     m_visitedColor = strtok(NULL, ",");

     // Close the file.
     oFile.Close();
   }
   else
   {
      // We couldn't read the settings.
      MessageBox("File Exception: " + oException.m_cause, "Error", MB_OK | MB_ICONERROR);
      return FALSE;
   }

   return TRUE;
}

BOOL CColorChgCtrl::SetColor() 
{
   CFile            oFile;        // File object for sending data to the drive.
   CFileException   oException;   // File opening exception.
   char*            szFileName;   // Name of the file.
   CString          oData;        // Data storage variable.

   // Open the file for writing, don't save the old settings.
   szFileName = "C:\\COLORSET.TXT";
   if (oFile.Open(szFileName, CFile::modeCreate | CFile::modeWrite, &oException))
   {
      // Store the color settings in an easily parsed form.
      oData = m_backgroundColor + ",";
      oData = oData + m_foregroundColor + ",";
      oData = oData + m_linkColor + ",";
      oData = oData + m_visitedColor + ",";

      // Write the data to the file.
      oFile.Write(oData, oData.GetLength());

      // Close the file.
      oFile.Close();

      // Update the ColorAvailable status.
      m_colorAvailable = TRUE;

     // Tell the world that we've changed the colors.
     FireOnColorLoad();
   }
   else
   {
     // We couldn't save the settings.
     MessageBox("File Exception: " + oException.m_cause, "Error", MB_OK | MB_ICONERROR);
      return FALSE;
   }

   return TRUE;
}

void CColorChgCtrl::OnColorAvailableChanged() 
{
	// TODO: Add notification handler code

	SetModifiedFlag();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -