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

📄 a9410dlg.cpp

📁 主要用于宿舍上网计时
💻 CPP
字号:
// A9410Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "A9410.h"
#include "A9410Dlg.h"
#include "PayDlg.h"
#include "Registry.h"
#include "PowerOffDlg.h"

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

#define WM_NOTIFYICON	WM_USER+200
NOTIFYICONDATA ntd;

/////////////////////////////////////////////////////////////////////////////
// CA9410Dlg dialog

CA9410Dlg::CA9410Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CA9410Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CA9410Dlg)
	m_Warning = _T("");
	m_TimeNow = _T("");
	m_TimeTotal = _T("");
	m_TimeOne = _T("");
	m_Remain = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDI_ICON3);
}

void CA9410Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CA9410Dlg)
	DDX_Control(pDX, IDC_BUTTON_OFF, m_PowerOff);
	DDX_Text(pDX, IDC_STATIC1, m_Warning);
	DDX_Text(pDX, IDC_STATIC2, m_TimeNow);
	DDX_Text(pDX, IDC_STATIC3, m_TimeTotal);
	DDX_Text(pDX, IDC_STATIC4, m_TimeOne);
	DDX_Text(pDX, IDC_STATIC5, m_Remain);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CA9410Dlg, CDialog)
	//{{AFX_MSG_MAP(CA9410Dlg)
	ON_WM_PAINT()
	ON_WM_SYSCOMMAND()
	ON_WM_QUERYDRAGICON()
	ON_WM_TIMER()
	ON_WM_CLOSE()
	ON_MESSAGE(WM_NOTIFYICON,OnNotifyIcon)
	ON_COMMAND(ID_TIMER_SHOW, OnTimerShow)
	ON_BN_CLICKED(IDC_BUTTON_PAY, OnButtonPay)
	ON_BN_CLICKED(IDC_BUTTON_OFF, OnButtonOff)
	ON_COMMAND(ID_TIMER_PAY, OnTimerPay)
	ON_COMMAND(ID_TIMER_SHOWTIME, OnTimerShowtime)
	ON_COMMAND(ID_TIMER_POWEROFF, OnTimerPoweroff)
	ON_COMMAND(ID_EXIT, OnExit)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CA9410Dlg message handlers

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

	// 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

	//检测是否已经加载启动项

	//获取文件路径
	CString str;
	::GetModuleFileName(NULL,strPath.GetBuffer(50),50);
	strPath.ReleaseBuffer();
	strPath.TrimLeft();
	CRegistry reg;
	reg.Open("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\0");
	if(!reg.Read("A9-410计时器",&str))
	{
		MessageBox("启动项已经被删除!");
		if(!reg.Write("A9-410计时器",strPath))
		{
			MessageBox("无法写入注册表!请不要阻止修改注册表!");
		}
	}
	else
	if((str!=strPath))
	{
		MessageBox("或是程序被移动过!");
		reg.DeleteValue("A9-410计时器");
		if(!reg.Write("A9-410计时器",strPath))
		{
			MessageBox("无法写入注册表!请不要阻止修改注册表!");
		}
	}
	reg.Close();
	
	//获取文件路径,去除文件名
	int nCount=strPath.ReverseFind('\\');
	int nLength=strPath.GetLength();
	strPath.Delete(nCount,nLength-nCount);//去除文件名
//	::GetCurrentDirectory();


	//初始化对话框显示内容
	SetTimer(0,1000,NULL);
	SetTimer(1,20,NULL);
	time_now=time_now.GetCurrentTime();
	m_TimeNow=time_now.Format("%Y年%m月%d日 %A %H:%M:%S");
	time_one=0;
	m_TimeOne=time_one.Format("%H:%M:%S");
	time_total=GetTotaltime();
	m_TimeTotal=time_total.Format("%D天%H小时%M分");	//读取总时间
	UpdateData(false);	
	RecordBeginTime();//记录开始上机时间
	
	//初始化托盘图标
	ntd.cbSize				= sizeof(NOTIFYICONDATA);
	ntd.hIcon				= m_hIcon;
	ntd.hWnd				= m_hWnd;
	strcpy(ntd.szTip,"A9-410宿舍上机计时器");
	ntd.uCallbackMessage	= WM_NOTIFYICON;
	ntd.uFlags				= NIF_ICON | NIF_MESSAGE | NIF_TIP ;
	ntd.uID					= IDR_MAINFRAME;
	Shell_NotifyIcon(NIM_ADD, &ntd);
//	ShowWindow(SW_HIDE);
	offFlag=false;//定时关机没有启动

	return TRUE;  // return TRUE  unless you set the focus to a control
}

// 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 CA9410Dlg::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 CA9410Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}
void CA9410Dlg::OnTimer( UINT nIDEvent )
{
	//更新显示,保存时间 
	if(0==nIDEvent)
	{
		time_now=time_now.GetCurrentTime();
		m_TimeNow=time_now.Format("%Y年%m月%d日 %A %H:%M:%S");
		time_one=time_one+1;
		m_TimeOne=time_one.Format("%H:%M:%S");
		SaveTotalTime();
		UpdateData(false);
	}

	//隐藏窗口
	if(1==nIDEvent)
	{
		ShowWindow(SW_HIDE);
		KillTimer(1);
	}
		
	if(2==nIDEvent)
	{
		//关机检测
		time_off=time_off-1;
		m_Remain="剩余关机时间:"+time_off.Format("%D天%H小时%M分%S秒");
		unsigned long i=time_off.GetTotalSeconds();
		if(i==300)
		{
			MessageBox("还有5分钟就要关机了!\r\n有什么要保存的尽快保存!");
		}
		if(i==0)
		{
			PowerOff();
		}
	}
	CDialog::OnTimer(nIDEvent );
}

void CA9410Dlg::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
	CString str;
	str="[/Stop]\r\n";
	str=str+m_TimeNow+"\r\n";
	CTimeSpan time;
	time=time_total+time_one;
	str=str+"本次上机时间:"+time_one.Format("%D天%H小时%M分%S秒")+"\r\n";
	str=str+"总的上机时间:"+time.Format("%D天%H小时%M分%S秒")+"\r\n";
	str=str+"[Stop\\]\r\n\r\n";
	SaveTime(str,"\\time.txt");

	//保存总时间
	SaveTotalTime();

	//删除图标
	Shell_NotifyIcon(NIM_DELETE ,&ntd);
	//删除所有定时器
	KillTimer(0);
	KillTimer(1);
	KillTimer(2);
	CDialog::OnClose();
}
CTimeSpan CA9410Dlg::GetTotaltime()
{
	CFile file;
	char buf[50];
	CString str;
	if(!file.Open((strPath+"\\Time.ini"),CFile::modeRead))
	{
		time_total=0;
		return time_total; 
	}
	file.Read(buf,50);
	file.Close();
	str.Format("%s",buf);
	str.TrimLeft();
	int nStart,nCount=0;
	int nD,nH,nM,nS;
	nStart=str.Find('=')+1;
	nCount=str.Find(':')-nStart;
	sscanf(str.Mid(nStart,nCount),"%d",&nD);
	sscanf(str.Mid(nStart+2,2),"%d",&nH);
	sscanf(str.Mid(nStart+5,2),"%d",&nM);
	sscanf(str.Mid(nStart+8,2),"%d",&nS);

	CTimeSpan time_temp(nD,nH,nM,nS);
	return time_temp;

}
void CA9410Dlg::RecordBeginTime()
{
	CString str;
	str="[Begin]\r\n";
	str=str+m_TimeNow+"\r\n\r\n";
	SaveTime(str,"\\Time.txt");
}
void CA9410Dlg::SaveTime(CString strContent,CString strFileName,bool flag)
{
	CFile file;
	char buf[200];
	sprintf(buf,"%s",strContent);
	if(!file.Open((strPath+strFileName),CFile::modeWrite))
	{
		if(!file.Open((strPath+strFileName),CFile::modeCreate|CFile::modeWrite))
		{
			AfxMessageBox("无法创建"+strPath+strFileName+"!");
			return ; 
		}
	}
	if(flag)
	{
		file.SeekToEnd();
	}
	file.Write(buf,strContent.GetLength());
	file.Close();
}
LONG CA9410Dlg::OnNotifyIcon(WPARAM wParam, LPARAM lParam)
{
	switch(lParam)
	{
	case WM_RBUTTONDOWN:
	{
		CMenu pMenu;
		if(pMenu.LoadMenu(IDR_MENU1))
		{
			CMenu*pPopup=pMenu.GetSubMenu(0);
			ASSERT(pPopup!=NULL);
			CPoint Point;
			if(IsWindowVisible())
				pMenu.ModifyMenu(ID_TIMER_SHOW,MF_STRING,ID_TIMER_SHOW,"隐藏(&H)");
			else
				pMenu.ModifyMenu(ID_TIMER_SHOW,MF_STRING,ID_TIMER_SHOW,"显示(&S)");
			GetCursorPos(&Point);
			SetForegroundWindow();
			pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON,
				Point.x,Point.y,this);
		}
	}
	break;
	case WM_LBUTTONDBLCLK:
			OnTimerShow();
			break;
	}
	return 0;
}


void CA9410Dlg::OnTimerShow() 
{
	// TODO: Add your command handler code here
	if(IsWindowVisible())
		ShowWindow(SW_HIDE);
	else
		ShowWindow(SW_SHOW);
}
void CA9410Dlg::SaveTotalTime()
{
	CString str;
	CTimeSpan time;
	time=time_total+time_one;
	str="[Total]\r\n";
	str=str+"Total Time="+time.Format("%D:%H:%M:%S");
	SaveTime(str,"\\Time.ini",false);
}

void CA9410Dlg::OnButtonPay() 
{
	// TODO: Add your control notification handler code here
	CPayDlg PayDlg;
	CTimeSpan time_sum;
	time_sum=time_total+time_one;
	PayDlg.m_Sum=(float )(time_sum.GetTotalHours()+time_sum.GetMinutes()/60.00);	
	PayDlg.DoModal();
	if(PayDlg.m_PayS)
	{
		time_total=0;
		time_one=0;
		m_TimeTotal=time_total.Format("%D天%H小时%M分");
		UpdateData(false);
		m_TimeOne=time_one.Format("%H:%M:%S");
		UpdateData(false);
		SaveTotalTime();
	}
}


BOOL CA9410Dlg::OnCommand(WPARAM wParam, LPARAM lParam) 
{
	// TODO: Add your specialized code here and/or call the base class
	if((wParam & 0xFFF0)==SC_CLOSE)
	{
		ShowWindow(SW_HIDE);
		return true;
	}
	return CDialog::OnCommand(wParam, lParam);
}
void CA9410Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == SC_CLOSE)
	{
		ShowWindow(SW_HIDE);
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}
void CA9410Dlg::PowerOff()
{
   
	OnClose();
    TOKEN_PRIVILEGES tp;
    HANDLE hToken; 
    LUID luid; 
    LPTSTR MachineName=NULL; 
    if(!OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES,&hToken ))
	{
       // PERR("OpenProcessToken",GetLastError());
        return ;
	}
    if(!LookupPrivilegeValue(MachineName, SE_SHUTDOWN_NAME, &luid))
	{
      //  PERR("LookupPrivilegeValue", GetLastError());
        return ; 
	}
    tp.PrivilegeCount = 1; 
    tp.Privileges[0].Luid = luid; 
    tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; 
    AdjustTokenPrivileges(hToken,FALSE,&tp,sizeof(TOKEN_PRIVILEGES),NULL, NULL ); //到这里,是取得权限///
    ExitWindowsEx(EWX_POWEROFF,EWX_FORCE);	
}

void CA9410Dlg::OnButtonOff() 
{
	// TODO: Add your control notification handler code here
	if(!offFlag)
	{
		CPowerOffDlg OffDlg;
		OffDlg.DoModal();
		if(OffDlg.n_ok)
		{
			time_off=OffDlg.timeoff;
			SetTimer(2,1000,NULL);
			offFlag=true;//定时关机启动
			m_PowerOff.SetWindowText("取消定时关机");
			m_Remain="剩余关机时间:"+time_off.Format("%D天%H小时%M分%S秒");
		}
	}
	else
	{
		KillTimer(2);
		m_PowerOff.SetWindowText("启动定时关机");
		m_Remain="定时关机已经取消!";
		offFlag=false;
	}
}

void CA9410Dlg::OnTimerPay() 
{
	// TODO: Add your command handler code here
	OnButtonPay();
}

void CA9410Dlg::OnTimerShowtime() 
{
	// TODO: Add your command handler code here
	CString str;
	str="已上机"+time_one.Format("%D天%H时%M分%S秒");
	MessageBox(str);
}

void CA9410Dlg::OnTimerPoweroff() 
{
	// TODO: Add your command handler code here
	OnButtonOff();
}

void CA9410Dlg::OnExit() 
{
	// TODO: Add your command handler code here
	::PostQuitMessage(0);
	OnClose();
}

⌨️ 快捷键说明

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