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

📄 diskstatusctl.cpp

📁 利用VC++开发的OCX控件
💻 CPP
字号:
// DiskStatusCtl.cpp : Implementation of the CDiskStatusCtrl ActiveX Control class.

#include "stdafx.h"
#include "DiskStatus.h"
#include "Graph.h"
#include "GraphSeries.h"
#include "DiskStatusCtl.h"
#include "DiskStatusPpg.h"
#include <math.h>

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


IMPLEMENT_DYNCREATE(CDiskStatusCtrl, COleControl)


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

BEGIN_MESSAGE_MAP(CDiskStatusCtrl, COleControl)
	//{{AFX_MSG_MAP(CDiskStatusCtrl)
	// 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(CDiskStatusCtrl, COleControl)
	//{{AFX_DISPATCH_MAP(CDiskStatusCtrl)
	DISP_PROPERTY_EX(CDiskStatusCtrl, "If3DPie", GetIf3DPie, SetIf3DPie, VT_BOOL)
	DISP_FUNCTION(CDiskStatusCtrl, "ShowDisk", ShowDisk, VT_EMPTY, VTS_BSTR)
	//}}AFX_DISPATCH_MAP
	DISP_FUNCTION_ID(CDiskStatusCtrl, "AboutBox", DISPID_ABOUTBOX, AboutBox, VT_EMPTY, VTS_NONE)
END_DISPATCH_MAP()


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

BEGIN_EVENT_MAP(CDiskStatusCtrl, COleControl)
	//{{AFX_EVENT_MAP(CDiskStatusCtrl)
	// NOTE - ClassWizard will add and remove event map entries
	//    DO NOT EDIT what you see in these blocks of generated code !
	//}}AFX_EVENT_MAP
END_EVENT_MAP()


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

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


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

IMPLEMENT_OLECREATE_EX(CDiskStatusCtrl, "DISKSTATUS.DiskStatusCtrl.1",
	0xc55a0296, 0x5b76, 0x448f, 0x8b, 0x2e, 0x24, 0x5e, 0x81, 0xb6, 0x74, 0x4)


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

IMPLEMENT_OLETYPELIB(CDiskStatusCtrl, _tlid, _wVerMajor, _wVerMinor)


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

const IID BASED_CODE IID_DDiskStatus =
		{ 0x67c55543, 0xa7a3, 0x4a76, { 0xb9, 0xa4, 0x76, 0xf, 0x81, 0x37, 0x93, 0x8f } };
const IID BASED_CODE IID_DDiskStatusEvents =
		{ 0x977c6325, 0xf438, 0x47f3, { 0xae, 0xaf, 0xf1, 0x7b, 0xbe, 0xe6, 0x77, 0xa7 } };


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

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

IMPLEMENT_OLECTLTYPE(CDiskStatusCtrl, IDS_DISKSTATUS, _dwDiskStatusOleMisc)


/////////////////////////////////////////////////////////////////////////////
// CDiskStatusCtrl::CDiskStatusCtrlFactory::UpdateRegistry -
// Adds or removes system registry entries for CDiskStatusCtrl

BOOL CDiskStatusCtrl::CDiskStatusCtrlFactory::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_DISKSTATUS,
			IDB_DISKSTATUS,
			afxRegApartmentThreading,
			_dwDiskStatusOleMisc,
			_tlid,
			_wVerMajor,
			_wVerMinor);
	else
		return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
}


/////////////////////////////////////////////////////////////////////////////
// CDiskStatusCtrl::CDiskStatusCtrl - Constructor

CDiskStatusCtrl::CDiskStatusCtrl()
{
	InitializeIIDs(&IID_DDiskStatus, &IID_DDiskStatusEvents);

	// TODO: Initialize your control's instance data here.
	m_pGraph = NULL;
	m_bIs3DPie = FALSE;

	ShowDisk("e:");
}


/////////////////////////////////////////////////////////////////////////////
// CDiskStatusCtrl::~CDiskStatusCtrl - Destructor

CDiskStatusCtrl::~CDiskStatusCtrl()
{
	// TODO: Cleanup your control's instance data here.
	if (NULL != m_pGraph)
	{
		delete m_pGraph;
	}
}


/////////////////////////////////////////////////////////////////////////////
// CDiskStatusCtrl::OnDraw - Drawing function

void CDiskStatusCtrl::OnDraw(
			CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
{
	// TODO: Replace the following code with your own drawing code.
	if (NULL != m_pGraph)
	{
		m_pGraph->DrawGraph(pdc);
	}
}


/////////////////////////////////////////////////////////////////////////////
// CDiskStatusCtrl::DoPropExchange - Persistence support

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

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

}


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

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

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

BOOL CDiskStatusCtrl::GetDiskStatus(LPCTSTR lpszDisk, long &totalMB, long &freeMB, CString &strType)
{
	int DType = DRIVE_FIXED;
	if(DType == DRIVE_FIXED)
	{
		strType = "本地磁盘";
	}
	else if(DType == DRIVE_CDROM)
	{
		strType = "光驱";
	}
	else if(DType == DRIVE_REMOVABLE)
	{
		strType = "可移动式磁盘";
	}
	else if(DType == DRIVE_REMOTE)
	{
		strType = "网络磁盘";
	}
	else if(DType == DRIVE_RAMDISK)
	{
		strType = "虚拟RAM磁盘";
	}
	else if (DType == DRIVE_UNKNOWN)
	{
		strType = "未知设备";
	}
	
	
	_int64 FreeBytes, TotalBytes;
	int nResult = ::GetDiskFreeSpaceEx(
					lpszDisk,
					(PULARGE_INTEGER)&FreeBytes,
					(PULARGE_INTEGER)&TotalBytes,
					NULL);
	if (nResult == 0)
	{
		CString strErrorMsg;
		strErrorMsg.Format("Can not Get the free space of %s (%d)", lpszDisk, GetLastError());
		MessageBox(strErrorMsg, "DiskStatusCtrl", MB_OK | MB_ICONERROR);
		return FALSE;
	}

	freeMB = (long)(FreeBytes / (1024 * 1024));
	totalMB = (long)(TotalBytes / (1024 * 1024));

	return TRUE;
}

void CDiskStatusCtrl::CreateGraphShow(LPCTSTR lpszDisk)
{
	long totalMB = -1;
	long freeMB = -1;
	CString strType;
	BOOL bResult = GetDiskStatus(lpszDisk, totalMB, freeMB, strType);
	if (!bResult)
	{
		return;
	}

	if (m_pGraph)
	{
		delete m_pGraph;
	}

	if (m_bIs3DPie)
	{
		m_pGraph = new CGraph(PIE_GRAPH_3D);
		m_pGraph->Set3DDepthRatio(.1);
	}
	else
	{
		m_pGraph = new CGraph(PIE_GRAPH);
	}

	//CString strTitle;
	//strTitle.Format("容量%ldMB", totalMB);
	//m_pGraph->SetGraphTitle(strTitle);

	//set up legend
	CString strLegend1;
	CString strLegend2;
	if (freeMB > 0)
	{
		CString strTemp;
		div_t tResult = div(freeMB, 1000);
		while (tResult.quot > 0)
		{
			strTemp = strLegend1;
			strLegend1.Format("%03d,%s", tResult.rem, strTemp);
			tResult = div(tResult.quot, 1000);
		}
		strTemp = strLegend1;
		strLegend1.Format("%d,%s", tResult.rem, strTemp);
		strLegend1.Delete(strLegend1.GetLength() - 1, 1);

		strLegend1.TrimLeft('0');

		strLegend1 = "Free  " + strLegend1 + " MB";
	}
	else
	{
		strLegend1 = "0";
	}

	if ((totalMB - freeMB) > 0)
	{
		CString strTemp;
		div_t tResult = div((totalMB - freeMB), 1000);
		while (tResult.quot > 0)
		{
			strTemp = strLegend2;
			strLegend2.Format("%03d,%s", tResult.rem, strTemp);
			tResult = div(tResult.quot, 1000);
		}
		strTemp = strLegend2;
		strLegend2.Format("%d,%s", tResult.rem, strTemp);
		strLegend2.Delete(strLegend2.GetLength() - 1, 1);

		strLegend1.TrimLeft('0');
		
		strLegend2 = "Used " + strLegend2 + " MB";
	}
	else
	{
		strLegend1 = "0";
	}
	//strLegend1.Format("可用%ldMB", freeMB);
	//strLegend2.Format("已用%ldMB", totalMB - freeMB);
	m_pGraph->SetLegend(0, strLegend1);
	m_pGraph->SetLegend(1, strLegend2);
	m_pGraph->SetLegendFontSize(14);
	
	//set up some series
	CGraphSeries* series1 = new CGraphSeries();
	//CString strLable;
	//strLable.Format("%s%s", strType, lpszDisk);
	//series1->SetLabel(strLable);

	series1->SetData(0, freeMB);
	series1->SetData(1, totalMB - freeMB);
	
	m_pGraph->AddSeries(series1);
	
	//set the colors of my bars
	m_pGraph->SetColor(0, RGB(255, 0, 255));
	m_pGraph->SetColor(1, BLUE);
}

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

void CDiskStatusCtrl::AboutBox()
{
	CDialog dlgAbout(IDD_ABOUTBOX_DISKSTATUS);
	dlgAbout.DoModal();
}


/////////////////////////////////////////////////////////////////////////////
// CDiskStatusCtrl message handlers

BOOL CDiskStatusCtrl::GetIf3DPie() 
{
	// TODO: Add your property handler here

	return m_bIs3DPie;
}

void CDiskStatusCtrl::SetIf3DPie(BOOL bNewValue) 
{
	// TODO: Add your property handler here
	m_bIs3DPie = bNewValue;

	SetModifiedFlag();
}

void CDiskStatusCtrl::ShowDisk(LPCTSTR diskName) 
{
	// TODO: Add your dispatch handler code here
	CreateGraphShow(diskName);
}

⌨️ 快捷键说明

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