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

📄 testtmdlg.cpp

📁 日期计算,天数,日期差,准确计算,文件操作
💻 CPP
字号:
// testtmDlg.cpp : implementation file
//

#include "stdafx.h"
#include "testtm.h"
#include "testtmDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTesttmDlg dialog

CTesttmDlg::CTesttmDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTesttmDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTesttmDlg)
	m_nValue = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTesttmDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTesttmDlg)
	DDX_Text(pDX, IDC_EDIT1, m_nValue);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTesttmDlg, CDialog)
	//{{AFX_MSG_MAP(CTesttmDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTesttmDlg message handlers

BOOL CTesttmDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CTesttmDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CTesttmDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CTesttmDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CTesttmDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	UpdateData(FALSE);
	long nvalue=m_nValue;	
}

long GetMonthDay(long nMonthIndex,long nYear)
{
	long nValidDays=30;
	switch (nMonthIndex)
	{
	case 1:
	case 3:
	case 5:
	case 7:
	case 8:
	case 10:
	case 12:
		nValidDays=31;
		break;
	case 2:
		if(nYear%4==0)
		{
			nValidDays=29;
		}
		else
		{
			nValidDays=28;

		}
		break;
	case 4:
	case 6:
	case 9:
	case 11:
		nValidDays=30;
		break;
	default :
		nValidDays=30;

		break;



	}

	return nValidDays;

}

long GetTotalDayAtMonth(long nMonthIndex,long nYear)
{
	long nDays=0;
	for(long i=1;i<=nMonthIndex;i++)
	{
		nDays+=GetMonthDay(i,nYear);
	}
	return nDays;
}

long GetTotalDay(SYSTEMTIME tm)
{
	long nTotalDay=tm.wYear*365+tm.wYear/4;
	long nDays=0;
	if(tm.wMonth>1)
	{
		for(long i=1;i<=tm.wMonth-1;i++)
		{
			nDays+=GetMonthDay(i,tm.wYear);
		}
	}


	nTotalDay+=nDays;
	nTotalDay+=tm.wDay;

	return nTotalDay;
}

SYSTEMTIME GetDate(long nTotalDay)
{
	//有多少个润年周期
	long nCount=nTotalDay/(365*4+1);
	long nSubDays=nTotalDay%(365*4+1);
	long nSubYears=nSubDays/365;
	nSubDays=nSubDays%365;
	long nYears=nCount*4+nSubYears;
	long nMonths=1;
	long nDays=31;
	if(nSubDays<=31)
	{
		nMonths=1;
		nDays=nSubDays;
	}
	else
	{
		for(long i=1;i<=11;i++)
		{
			if(nSubDays>GetTotalDayAtMonth(i,nYears)&&nSubDays<=GetTotalDayAtMonth(i+1,nYears))
			{
				nMonths=i+1;
				break;
			}
		}
		nDays=nSubDays-GetTotalDayAtMonth(nMonths-1,nYears);
	}

	SYSTEMTIME tm;
	tm.wYear=nYears;
	tm.wMonth=nMonths;
	tm.wDay=nDays;
	return tm;
    	
}


//删除之前天数的文件
long DeleteBefore(SYSTEMTIME tmCur,long nDays,CString strPath)
{
	WIN32_FIND_DATA fdData;
	HANDLE hFirst=::FindFirstFile(strPath+"\\*.*",&fdData);
	bool bFind=true;
	if(hFirst==INVALID_HANDLE_VALUE)
	{
		return 0;
	}

	//简单计算倒退的天数
	long nCurTotalDay=GetTotalDay(tmCur);

	//计算之前的日期
	nCurTotalDay-=nDays;



	while(bFind)
	{
		FILETIME ftLocal;
		if(::FileTimeToLocalFileTime(&fdData.ftCreationTime,&ftLocal))
		{

			SYSTEMTIME sysTime;
			if(::FileTimeToSystemTime(&ftLocal,&sysTime))
			{
				//简单计算天数
				long nFileDays=GetTotalDay(sysTime);
				if(nFileDays<nCurTotalDay)
				{
					::DeleteFile(strPath+CString(_T("\\"))+fdData.cFileName); 
				}
			
			}
		}

		bFind=::FindNextFile(hFirst,&fdData);
	}
	::FindClose(hFirst);

	return 0;
}


void CTesttmDlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
	SYSTEMTIME tmCur;
	GetLocalTime(&tmCur);
	DeleteBefore(tmCur,30,"d:\\tst");
}

⌨️ 快捷键说明

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