www.pudn.com > Shutdown.rar > ShutdownDlg.cpp
// ShutdownDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Shutdown.h"
#include "ShutdownDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
#define MYWM_MESSAGE WM_USER+100
CUIntArray aryRun;
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()
/////////////////////////////////////////////////////////////////////////////
// CShutdownDlg dialog
CShutdownDlg::CShutdownDlg(CWnd* pParent /*=NULL*/)
: CDialog(CShutdownDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CShutdownDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CShutdownDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CShutdownDlg)
DDX_Control(pDX, IDC_SPIN2, m_Spin2);
DDX_Control(pDX, IDC_SPIN1, m_Spin1);
DDX_Control(pDX, IDC_CHECK1, m_Check);
DDX_Control(pDX, IDC_LIST_Progress, m_List_Progress);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CShutdownDlg, CDialog)
//{{AFX_MSG_MAP(CShutdownDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_CLOSE()
ON_WM_DESTROY()
ON_COMMAND(ID_MENU_EXIT, OnMenuExit)
ON_WM_SHOWWINDOW()
ON_WM_TIMER()
ON_BN_CLICKED(IDC_BUTTON2, OnAddProgress)
ON_BN_CLICKED(IDC_BUT_APPLY, OnButApply)
ON_BN_CLICKED(IDC_CHECK1, OnCheckShutdown)
ON_BN_CLICKED(IDC_BUTTON3, OnDelItem)
//}}AFX_MSG_MAP
ON_MESSAGE(MYWM_MESSAGE,Do_MyMessage)
ON_MESSAGE(MSG_CLICK_BUTTON,ClickButton)
ON_MESSAGE(MSG_EDITFINISH,EditFinish)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CShutdownDlg message handlers
BOOL CShutdownDlg::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
InitAll();
//SetWindowText("");
return TRUE; // return TRUE unless you set the focus to a control
}
void CShutdownDlg::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 CShutdownDlg::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 CShutdownDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CShutdownDlg::Do_MyMessage(WPARAM wparam,LPARAM lparam)
{
if(lparam== WM_LBUTTONDBLCLK)//双击左键;
{}
else if(lparam== WM_RBUTTONDOWN)//单击右键
{
CMenu menu;
menu.LoadMenu(IDR_MENU_POP);
//载入事先定义的菜单
CMenu* pMenu=menu.GetSubMenu(0);
CPoint pos;
GetCursorPos(&pos);
pMenu->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,
pos.x,pos.y,AfxGetMainWnd());
}
else if(lparam== WM_LBUTTONDOWN)//单击左键
{
CRect rect,Rect;
this->GetWindowRect(&Rect);
this->ScreenToClient(&Rect);
this->GetClientRect(&rect);
rect.bottom = rect.top + Rect.Height();
// this->SetWindowPos(&wndTop,rect.left,rect.top,rect.right,rect.bottom,SWP_NOMOVE);
this->ShowWindow(SW_SHOW);
this->SetFocus();
}
}
void CShutdownDlg::InitFaceplate()
{
m_tnid.cbSize = sizeof(NOTIFYICONDATA);
m_tnid.hWnd = this->m_hWnd;
m_tnid.uFlags = NIF_MESSAGE|NIF_ICON|NIF_TIP;
m_tnid.uCallbackMessage = MYWM_MESSAGE;
//
CString szToolTip;
szToolTip = _T("自动关机程序");
_tcscpy(m_tnid.szTip,szToolTip);
m_tnid.uID = IDR_MAINFRAME;
HICON hIcon;
hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_tnid.hIcon = hIcon;
::Shell_NotifyIcon(NIM_ADD,&m_tnid);
if(hIcon)::DestroyIcon(hIcon);
}
void CShutdownDlg::InitAll()
{
InitFaceplate();
ReadData();
OnCheckShutdown();
m_Spin1.SetRange(0,23);
m_Spin2.SetRange(0,59);
SetTimer(1,50000,NULL);
}
void CShutdownDlg::OnClose()
{
// TODO: Add your message handler code here and/or call default
ShowWindow(SW_HIDE);
}
void CShutdownDlg::OnDestroy()
{
CDialog::OnDestroy();
::Shell_NotifyIcon(NIM_DELETE,&m_tnid);
// TODO: Add your message handler code here
}
BOOL CShutdownDlg::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Add your specialized code here and/or call the base class
if(!CDialog::PreCreateWindow(cs))
return FALSE;
cs.style = WS_POPUP;
cs.dwExStyle |=WS_EX_TOOLWINDOW;
return TRUE;
}
void CShutdownDlg::OnMenuExit()
{
// TODO: Add your command handler code here
CDialog::OnOK();
}
void CShutdownDlg::OnShowWindow(BOOL bShow, UINT nStatus)
{
CDialog::OnShowWindow(bShow, nStatus);
// TODO: Add your message handler code here
static int ctrl =0;
if(ctrl==0)
{
this->PostMessage(WM_CLOSE);
ctrl =1;
}
}
void CShutdownDlg::ShutdownSoon()
{
DWORD dwVersion,dwWindowsMajorVersion,dwWindowsMinorVersion,dwBuild;
dwVersion = GetVersion();
// Get the Windows version.
dwWindowsMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion)));
dwWindowsMinorVersion = (DWORD)(HIBYTE(LOWORD(dwVersion)));
// Get the build number.
if (dwVersion < 0x80000000) // Windows NT/2000/XP
{
dwBuild = (DWORD)(HIWORD(dwVersion));
ShutdownWin2000NTXP();
}
//else if (dwWindowsMajorVersion < 4) // Win32s
//{
// dwBuild = (DWORD)(HIWORD(dwVersion) & ~0x8000);
//}
else // Windows 95/98/Me
{
dwBuild = 0;
ShutdownWin98();
}
}
void CShutdownDlg::ShutdownWin2000NTXP()
{
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
AfxMessageBox("OpenProcessToken");
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
&tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES)NULL, 0);
if (GetLastError() != ERROR_SUCCESS)
AfxMessageBox("AdjustTokenPrivileges");
if (!ExitWindowsEx(EWX_POWEROFF | EWX_FORCE, 0))
AfxMessageBox("ExitWindowsEx");
}
void CShutdownDlg::ShutdownWin98()
{
if (!ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0))
AfxMessageBox("ExitWindowsEx");
}
void CShutdownDlg::ReadData()
{
CString strSection,strEntry,strDefault,strVal;
InitListCtrl(m_List_Progress);
int nCheck,nCount ;
strSection = "CShutdownDlg";
//写关机时间
strEntry = "IF_SHUTDOWN";
strDefault = "0";
strVal = AfxGetApp()->GetProfileString(strSection,strEntry,strDefault);
nCheck = atoi(strVal); m_Check.SetCheck(nCheck);
//写小时
strEntry = "HOUR";
strDefault = "0";
strVal = AfxGetApp()->GetProfileString(strSection,strEntry,strDefault);
SetDlgItemText(IDC_EDIT1,strVal);
//写分钟
strEntry = "MINUTE";
strDefault = "0";
strVal = AfxGetApp()->GetProfileString(strSection,strEntry,strDefault);
SetDlgItemText(IDC_EDIT2,strVal);
//写任务表
strEntry = "ITEMCOUNT";
strDefault = "0";
strVal = AfxGetApp()->GetProfileString(strSection,strEntry,strDefault);
nCount = atoi(strVal);
for(int i = 0;iGetProfileString(strSection,strEntry);
if(j==0)
m_List_Progress.InsertItem(i,strVal);
else
m_List_Progress.SetItemText(i,j,strVal);
}
aryRun.Add(0);
}
//
}
void CShutdownDlg::WriteData()
{
CString strSection,strEntry,strVal;
int nCheck,nCount ;
strSection = "CShutdownDlg";
//写关机时间
strEntry = "IF_SHUTDOWN";
nCheck = m_Check.GetCheck();
strVal.Format("%d",nCheck);
AfxGetApp()->WriteProfileString(strSection,strEntry,strVal);
//写小时
strEntry = "HOUR";
GetDlgItemText(IDC_EDIT1,strVal);
AfxGetApp()->WriteProfileString(strSection,strEntry,strVal);
//写分钟
strEntry = "MINUTE";
GetDlgItemText(IDC_EDIT2,strVal);
AfxGetApp()->WriteProfileString(strSection,strEntry,strVal);
//写任务表
strEntry = "ITEMCOUNT";
nCount = m_List_Progress.GetItemCount();
strVal.Format("%d",nCount);
AfxGetApp()->WriteProfileString(strSection,strEntry,strVal);
for(int i = 0;iWriteProfileString(strSection,strEntry,strVal);
}
}
}
void CShutdownDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
SYSTEMTIME lpSystemTime ;
CString strVal;
int nCount,i;
int nHourSut,nMinuteSut;
GetSystemTime(&lpSystemTime );
GetLocalTime(&lpSystemTime);
int nHour = (int)lpSystemTime.wHour;
int nMinute = (int)lpSystemTime.wMinute;
if(m_Check.GetCheck())
{
GetDlgItemText(IDC_EDIT1,strVal);
nHourSut = atoi(strVal);
GetDlgItemText(IDC_EDIT2,strVal);
nMinuteSut = atoi(strVal);
if(nHourSut==nHour&&(WORD)nMinute>=(WORD)nMinuteSut)
ShutdownSoon();
}
nCount = m_List_Progress.GetItemCount();
for(i = 0;i=nMinuteSut&&aryRun.GetAt(i)==0)
{
strVal = m_List_Progress.GetItemText(i,0);
::WinExec(strVal,0);
aryRun.SetAtGrow(i,1);
}
}
CDialog::OnTimer(nIDEvent);
}
//------界面--------------------
void CShutdownDlg::InitListCtrl(Y_Cls_ListCtrl_ &m_List)
{
CString str;
int nItemColumnW;
int L,l;
int nAllW;
CRect rect,rcList;
int cxVScroll = GetSystemMetrics(SM_CXVSCROLL);
CBitmap bm;
CWnd *pW=GetDlgItem(IDC_EDIT1);
pW->GetClientRect(&rect);
rect.InflateRect(0,-1);
bm.LoadBitmap(IDB_BIT);
CImageList m_HeadIcon;
m_HeadIcon.Create(1,rect.Height()+2,TRUE,2,2);
m_HeadIcon.Add(&bm, RGB(255, 255, 255));
m_List.SetImageList(&m_HeadIcon,LVSIL_SMALL);
m_List.GetClientRect(&rcList);
nAllW = rcList.Width() - cxVScroll;
str = "程序名称 小时分钟";
L = str.GetLength();
m_List.SetExtendedStyle(m_List.GetExtendedStyle()|LVS_EX_GRIDLINES|LVS_EX_FULLROWSELECT);
m_List.GetClientRect(&rect);
str.Format("%s","程序名称 ");
l = str.GetLength();
nItemColumnW = nAllW*l/L;
m_List.InsertColumn(0,str,LVCFMT_LEFT,nItemColumnW,-1);
str.Format("%s","小时");
l = str.GetLength();
nItemColumnW = nAllW*l/L;
m_List.InsertColumn(1,str,LVCFMT_LEFT,nItemColumnW,-1);
str.Format("%s","分钟");
l = str.GetLength();
nItemColumnW = nAllW*l/L;
m_List.InsertColumn(2,str,LVCFMT_LEFT,nItemColumnW,-1);
}
LRESULT CShutdownDlg::ClickButton(WPARAM wraram,LPARAM lparam)
{
int nCtrl = (int)wraram;
CFileDialog dlg(TRUE, "exe", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
"工程文件 (*.exe) |*.exe||", NULL);
if(dlg.DoModal()==IDCANCEL)
return 0;
CString str = dlg.GetPathName();
CString strOldRun;
if(nCtrl==1)
{
m_List_Progress.InsertItem(m_List_Progress.GetItemCount(),str);
aryRun.Add(0);
}
else if(nCtrl==0)
{
strOldRun = m_List_Progress.GetItemText(m_List_Progress.Old_iItem,m_List_Progress.Old_iSubItem);
if(strOldRun!=str) aryRun.SetAtGrow(m_List_Progress.Old_iItem,0);
m_List_Progress.SetItemText(m_List_Progress.Old_iItem,m_List_Progress.Old_iSubItem,str);
}
else
AfxMessageBox("Error define by cyp : Errorl NO.1");
return 1;
}
void CShutdownDlg::OnAddProgress()
{
// TODO: Add your control notification handler code here
ClickButton(1,0);
}
void CShutdownDlg::OnButApply()
{
// TODO: Add your control notification handler code here
if(verify()) WriteData();
}
void CShutdownDlg::OnCheckShutdown()
{
// TODO: Add your control notification handler code here
bool bShow;
if(m_Check.GetCheck()) bShow = true;
else bShow = false;
GetDlgItem(IDC_STATICHour)->EnableWindow(bShow);
GetDlgItem(IDC_EDIT1)->EnableWindow(bShow);
GetDlgItem(IDC_SPIN2)->EnableWindow(bShow);
GetDlgItem(IDC_STATICMinu)->EnableWindow(bShow);
GetDlgItem(IDC_EDIT2)->EnableWindow(bShow);
GetDlgItem(IDC_SPIN1)->EnableWindow(bShow);
}
int CShutdownDlg::verify()
{
CString strTmp;
int nTmp;
if(m_Check.GetCheck())
{
GetDlgItemText(IDC_EDIT1,strTmp);
nTmp = atoi(strTmp);
if(nTmp>=24)
{
AfxMessageBox("请输入0 ~ 23 之间的数!");
CEdit *pEdit = (CEdit *)GetDlgItem(IDC_EDIT1);
pEdit->SetSel(-1,2);
return 0;
}
GetDlgItemText(IDC_EDIT2,strTmp);
nTmp = atoi(strTmp);
if(nTmp>=60)
{
AfxMessageBox("请输入0 ~ 60 之间的数!");
CEdit *pEdit = (CEdit *)GetDlgItem(IDC_EDIT1);
pEdit->SetSel(-1,2);
return 0;
}
}
for(int i = 0;i23)
{
AfxMessageBox("请输入0 ~ 23 之间的数!");
return 0;
}
strTmp = m_List_Progress.GetItemText(i,2);
nTmp = atoi(strTmp);
if(nTmp<0||nTmp>59)
{
AfxMessageBox("请输入0 ~ 59 之间的数!");
return 0;
}
}
return 1;
}
LRESULT CShutdownDlg::EditFinish(WPARAM wraram,LPARAM lparam)
{
verify();
return 1;
}
void CShutdownDlg::OnDelItem()
{
// TODO: Add your control notification handler code here
aryRun.RemoveAt(m_List_Progress.Old_iItem);
m_List_Progress.DeleteItem(m_List_Progress.Old_iItem);
}