当前位置: 源码素材网 » VC++教程 » 详情页

VC++ timeShutdown 定时关机功能代码分享

  •   时间:2018-10-09
  • 概述:定时关机

一个简单的VC++ 定时关机功能,自定义关机时间,保持本程序在windows托盘处显示图标,时间到达后会强制关闭计算机,具体实现代码如下:

void CTimeShutDownPCDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	// 屏蔽最大化,将最小化重定向至隐藏窗口
    if ((nID & 0xFFF0) == SC_MINIMIZE)
	{
		// 最小化时隐藏窗口
		ShowWindow(0);
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}
BOOL CTimeShutDownPCDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	// 将图标放入系统托盘
    TrayNotifyMsg(NIM_ADD, IDR_MAINFRAME, "定时关机");
	// 初始化关机时间为23点.
	SYSTEMTIME systime;
	((CDateTimeCtrl*)GetDlgItem(IDC_DATETIMEPICKER_TIME))->GetTime(&systime);
	systime.wHour = 23;
	systime.wMinute = 0;
	systime.wSecond = 0;
	((CDateTimeCtrl*)GetDlgItem(IDC_DATETIMEPICKER_TIME))->SetTime(&systime);
	// 设定定时器
	SetTimer(1, 1000, NULL);
	// 显示当前的系统时间
	ShowCurrentTime();
	return TRUE;
}
void CTimeShutDownPCDlg::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();
	}
}
HCURSOR CTimeShutDownPCDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}
BOOL CTimeShutDownPCDlg::TrayNotifyMsg(DWORD Msg, UINT Icon, CString strTip)
{
	// 向系统发送系统栏图标信息, 先填充一个 NOTIFYICONDATA 结构
	// 向系统发送系统栏图标信息Msg: NIM_DELETE, NIM_ADD, NIM_MODIFY.
	if (Icon == NULL)
	{
		Icon = IDR_MAINFRAME;
	}
	NOTIFYICONDATA data;
	data.cbSize           = sizeof(NOTIFYICONDATA);
	data.hIcon            = (HICON) LoadImage(AfxGetApp()->m_hInstance ,
		                                      MAKEINTRESOURCE(Icon),
											  IMAGE_ICON,
											  16,
											  16,
											  0);
	data.hWnd             = this->GetSafeHwnd();
	data.uCallbackMessage = WM_TRAYICON;
	data.uFlags           = NIF_MESSAGE | NIF_ICON | NIF_TIP;
	data.uID              = IDR_MAINFRAME;
	switch(Msg)
	{
		case NIM_DELETE:
		{
		   	data.hIcon    = NULL;
		   	data.szTip[0] = '\0';
		   	break;
		}
		case NIM_ADD:
		{
			strcpy(data.szTip, strTip);
		   	break;
		}
		case NIM_MODIFY:
		{
		   	strcpy(data.szTip, strTip);
		   	break;
		}
	}
	return Shell_NotifyIcon(Msg, &data);
}
void CTimeShutDownPCDlg::OnTrayIcon(WPARAM wParam, LPARAM lParam)
{
	switch(lParam)
	{
	case WM_LBUTTONDBLCLK:
		SetForegroundWindow();
        ShowWindow(1);
		break;
	case WM_LBUTTONUP:
		SetForegroundWindow();
        ShowWindow(1);
		break;
	case WM_RBUTTONDOWN:
		CPoint point(0, 0);
		GetCursorPos(&point);
		SetForegroundWindow();
		CMenu menu;
		// 装入菜单
		menu.LoadMenu(IDR_POP_MENU);
		// 显示菜单
		menu.GetSubMenu(0)->TrackPopupMenu(TPM_LEFTALIGN|
			                               TPM_LEFTBUTTON|
			                               TPM_RIGHTBUTTON,
										   point.x,
										   point.y,
										   this);
		break;
    //default:
        //break;
	}
}
void CTimeShutDownPCDlg::OnPopMenuShowWindow()
{
	// TODO: Add your command handler code here
	SetForegroundWindow();
	ShowWindow(1);
}
void CTimeShutDownPCDlg::OnPopMenuHideWindow()
{
	ShowWindow(0);
}
void CTimeShutDownPCDlg::OnPopMenuQuit()
{
	TrayNotifyMsg(NIM_DELETE);
	// 关闭程序
	DestroyWindow();
}
void CTimeShutDownPCDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
	PostMessage( WM_NCLBUTTONDOWN,
		         HTCAPTION,
				 MAKELPARAM( point.x, point.y));
	CDialog::OnLButtonDown(nFlags, point);
}
void CTimeShutDownPCDlg::OnButtonConfirm()
{
	// 获得日期和时间
	CString csDate = _T("");
	CString csTime = _T("");
	GetDlgItem(IDC_DATETIMEPICKER_DATE)->GetWindowText(csDate);
	GetDlgItem(IDC_DATETIMEPICKER_TIME)->GetWindowText(csTime);
	// 隐藏窗口
	OnPopMenuHideWindow();
}
void CTimeShutDownPCDlg::ShowCurrentTime()
{
	CString csCurrentTime = CTime::GetCurrentTime().Format("%Y-%m-%d %H:%M:%S");
	GetDlgItem(IDC_STATIC_CURRENT_SYSTEM_TIME)->SetWindowText(csCurrentTime);
}
void CTimeShutDownPCDlg::OnTimer(UINT nIDEvent)
{
	if ( nIDEvent == 1)
	{
		// 显示当前的系统时间
		ShowCurrentTime();
		if (CmpDateTime())
		{
			m_nTimes = 60;
			SetTimer(2, 1000, NULL);
		}
	}
	if ( nIDEvent == 2)
	{
		CString csTip = _T("");
		if ((--m_nTimes) == 0)
		{
			KillTimer(2);
			ShowDown();
		}
		else
		{
			SetForegroundWindow();
			csTip.Format("离关机还有 %d 秒", m_nTimes);
			TrayNotifyMsg(NIM_MODIFY, IDR_MAINFRAME, csTip);
			SetWindowText(csTip);
		}
	}
	CDialog::OnTimer(nIDEvent);
}
void CTimeShutDownPCDlg::ShowDown()
{
	// 关机函数, 适用9X和NT
	if (IsWin2000_Xp())
	{
		WinExec("ShutDown -s -f -t 0 -c \" WINDOWS即将关机,请做好保存工作!\"", SW_SHOW);
	}
	else
	{
		ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE | EWX_POWEROFF,NULL);
	}
}
BOOL CTimeShutDownPCDlg::IsWin2000_Xp()
{
	OSVERSIONINFO winInfo;
	winInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
	GetVersionEx(&winInfo);
	if (winInfo.dwMajorVersion == 5)	//win2000 or winXP
	{
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}
// 时间比较函数, 判断时间是否已到
BOOL CTimeShutDownPCDlg::CmpDateTime()
{
	CTime CurrentTime = CTime::GetCurrentTime();
	CString csTime = CurrentTime.Format("%H:%M:%S");
	CString csDate = _T("");
	csDate.Format("%d-%d-%d", CurrentTime.GetYear(), CurrentTime.GetMonth(),
		          CurrentTime.GetDay());
	CString csTempDate = _T("");
	CString csTempTime = _T("");
	GetDlgItem(IDC_DATETIMEPICKER_DATE)->GetWindowText(csTempDate);
	if (csTempDate.CompareNoCase(csDate) == 0)
	{
		GetDlgItem(IDC_DATETIMEPICKER_TIME)->GetWindowText(csTempTime);
		if (csTempTime.CompareNoCase(csTime) == 0)
		{
			return TRUE;
		}
	}
	return FALSE;
}

    相关声明:

      若“VC++ timeShutdown 定时关机功能代码分享”有损您的权益,请告之我们删除内容。
      部分文章来源于网络,版权归原作者所有。