?? settimeshutdown.cpp
字號:
// SetTimeShutdown.cpp : implementation file
//
//功能:Windows 9x/NT下定時、立即關機,重啟計算機,開機程序自啟動
// 作者:徐景周
//日期:2001年7月20日
//
#include "stdafx.h"
#include "scrgenius.h"
#include "SetTimeShutdown.h"
#include "AnimEffect.h" //對話框動畫效果顯示
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//設置開機程序自啟動鍵值位置和其下新建子鍵值(可設為程序名)
const TCHAR gcszAutoRunKey[]= _T( "Software\\Microsoft\\windows\\CurrentVersion\\Run" );
const TCHAR gcszWindowClass[] = _T("lovePet");
/////////////////////////////////////////////////////////////////////////////
// CSetTimeShutdown dialog
CSetTimeShutdown::CSetTimeShutdown(CWnd* pParent /*=NULL*/)
: CDialog(CSetTimeShutdown::IDD, pParent)
{
//{{AFX_DATA_INIT(CSetTimeShutdown)
m_sNowTime = _T("");
m_Time = CTime(2001,7,21,20,06,0);
//}}AFX_DATA_INIT
}
void CSetTimeShutdown::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSetTimeShutdown)
DDX_Control(pDX, IDC_REBOOT, m_ReBoot);
DDX_Control(pDX, IDC_SHUTDOWN, m_ShutDownOnce);
DDX_Control(pDX, IDCANCEL, m_Cancel);
DDX_Control(pDX, IDC_BUTTON_SHUTDOWN, m_ShutDown);
DDX_Control(pDX, IDC_DATETIMEPICKER1, m_Timer);
DDX_Text(pDX, IDC_STATIC_NOW, m_sNowTime);
DDX_DateTimeCtrl(pDX, IDC_DATETIMEPICKER1, m_Time);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSetTimeShutdown, CDialog)
//{{AFX_MSG_MAP(CSetTimeShutdown)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_BUTTON_SHUTDOWN, OnButtonShutdown)
ON_NOTIFY(DTN_DATETIMECHANGE, IDC_DATETIMEPICKER1, OnDatetimechangeDatetimepicker1)
ON_BN_CLICKED(IDC_SHUTDOWN, OnShutdown)
ON_WM_SHOWWINDOW()
ON_BN_CLICKED(IDC_REBOOT, OnReboot)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSetTimeShutdown message handlers
BOOL CSetTimeShutdown::OnInitDialog()
{
CDialog::OnInitDialog();
//啟動計時器,一秒鐘更新一次
SetTimer(0,1000,NULL);
bSetTime=false;
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
//開機時程序自動運行
BOOL StartUpAutoRun(BOOL bIsAdd)
{
HKEY hKey;
LONG lRet, lRet2;
DWORD dwLength, dwDataType;
TCHAR szItemValue[MAX_PATH], szPrevValue[MAX_PATH];
TCHAR szBuffer[MAX_PATH];
// 得到程序全路徑名
GetModuleFileName( NULL, szItemValue, MAX_PATH );
// 打開注冊表鍵
lRet = RegOpenKeyEx( HKEY_LOCAL_MACHINE, gcszAutoRunKey,
0, KEY_READ | KEY_WRITE, &hKey );
if( lRet != ERROR_SUCCESS )
return FALSE;
// 查詢自動運行項目是否存在
dwLength = sizeof( szBuffer );
lRet = RegQueryValueEx( hKey, gcszWindowClass,
NULL, &dwDataType, (LPBYTE)szBuffer, &dwLength );
// 添加
if( bIsAdd == TRUE )
{
// 自動運行項目不存在
if( lRet != ERROR_SUCCESS )
lRet2 = RegSetValueEx( hKey, gcszWindowClass,
0, REG_SZ, (LPBYTE)szItemValue, strlen( szItemValue ) );
else
{
// 存在, 比較二者是否相同
dwLength = sizeof( szPrevValue );
lRet2 = RegQueryValueEx( hKey, gcszWindowClass,
0, &dwDataType,(LPBYTE)szPrevValue, &dwLength );
// 不相同則替換
if( lstrcmp( szItemValue, szPrevValue ) )
{
lRet2 = RegDeleteValue( hKey, gcszWindowClass );
lRet2 = RegSetValueEx( hKey, gcszWindowClass,
0, REG_SZ,(LPBYTE)szItemValue, strlen( szItemValue ) );
}
}
}
// 刪除
else
// 自動運行項目已存在則刪除
if( lRet == ERROR_SUCCESS )
lRet2 = RegDeleteValue( hKey, gcszWindowClass );
// 關閉注冊表鍵
RegCloseKey( hKey );
if( lRet2 != ERROR_SUCCESS )
return FALSE;
return TRUE;
}
//WINDOWS NT/98下安全關機或注銷的調用涵數
void WinShutdown(UINT ShutdownFlag)
{
OSVERSIONINFO oi;
oi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&oi);
//如果是NT/2000下需先設置相關權限
if (oi.dwPlatformId == VER_PLATFORM_WIN32_NT)
{
HANDLE handle;
TOKEN_PRIVILEGES tkp;
OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &handle);
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(handle, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
}
//WIN98下關機或注銷時可直接調用下面涵數既可
::ExitWindowsEx(ShutdownFlag,0);
}
void CSetTimeShutdown::OnTimer(UINT nIDEvent)
{
int hour,min,sec;
int yhour,ymin,ysec;
CTime nowtime = CTime::GetCurrentTime();
m_sNowTime = nowtime.Format(_T("%Y-%m-%d %H:%M:%S"));
UpdateData(FALSE);
if(bSetTime)
{
hour = nowtime.GetHour();
min = nowtime.GetMinute();
sec = nowtime.GetSecond();
yhour = m_Time.GetHour();
ymin = m_Time.GetMinute();
ysec = m_Time.GetSecond();
//如果時間到,則關機
if(hour == yhour && min == ymin )
WinShutdown(EWX_SHUTDOWN);
}
CDialog::OnTimer(nIDEvent);
}
//定時關機按鈕按下時
void CSetTimeShutdown::OnButtonShutdown()
{
if(m_sNowTime.IsEmpty())
return;
bSetTime=true;
ShowWindow(SW_HIDE);//隱藏該窗體
//判斷是否選中開機自啟動項,并做相應處理
int Status;
CButton * check1 = ( CButton * )GetDlgItem( IDC_CHECK1 );
Status=check1->GetCheck();
if (Status==1) //添加子鍵
StartUpAutoRun(true);
else //刪除子鍵
StartUpAutoRun(false);
}
void CSetTimeShutdown::OnDatetimechangeDatetimepicker1(NMHDR* pNMHDR, LRESULT* pResult)
{
UpdateData(true); //更新控件顯示
*pResult = 0;
}
//當按下立即關機按鈕時,則馬上關機
void CSetTimeShutdown::OnShutdown()
{
//判斷是否選中開機自啟動項,并做相應處理
int Status;
CButton * check1 = ( CButton * )GetDlgItem( IDC_CHECK1 );
Status=check1->GetCheck();
if (Status==1) //添加子鍵
StartUpAutoRun(true);
else //刪除子鍵
StartUpAutoRun(false);
//關機
WinShutdown(EWX_SHUTDOWN);
}
BOOL CSetTimeShutdown::DestroyWindow()
{
CRect rect3; //對話框動畫顯示效果
AnimEffect animation3;
//在此加入對話框動畫關閉效果
GetWindowRect(rect3);
rect3.DeflateRect(2,2);//動態方框顯示
animation3.DrawWireRects(rect3, CRect(rect3.CenterPoint().x-10,rect3.CenterPoint().y-10,rect3.CenterPoint().x+10,rect3.CenterPoint().y+10 ), 10);
// animation3.Close(rect3); ////對話框動畫效果顯示關閉
// Sleep(50); //延緩50毫秒后關閉
return CDialog::DestroyWindow();
}
void CSetTimeShutdown::OnShowWindow(BOOL bShow, UINT nStatus)
{
CDialog::OnShowWindow(bShow, nStatus);
CRect rect3; //對話框動畫顯示效果
AnimEffect animation3;
//在此加入對話框動畫打開效果
GetWindowRect(rect3 );
animation3.Open( rect3 ); //對話框動畫效果顯示打開
}
//關閉該對話框
void CSetTimeShutdown::OnCancel()
{
//判斷是否選中開機自啟動項,并做相應處理
int Status;
CButton * check1 = ( CButton * )GetDlgItem( IDC_CHECK1 );
Status=check1->GetCheck();
if (Status==1) //添加子鍵
StartUpAutoRun(true);
else //刪除子鍵
StartUpAutoRun(false);
CDialog::OnCancel();
}
//重新啟動計算機
void CSetTimeShutdown::OnReboot()
{
//判斷是否選中開機自啟動項,并做相應處理
int Status;
CButton * check1 = ( CButton * )GetDlgItem( IDC_CHECK1 );
Status=check1->GetCheck();
if (Status==1) //添加子鍵
StartUpAutoRun(true);
else //刪除子鍵
StartUpAutoRun(false);
//重新啟動
WinShutdown(EWX_REBOOT);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -