zoukankan      html  css  js  c++  java
  • 智能关机系统

    近期在网上看到了一个智能关机系统,看起来感觉功能不错,就想自己写一个,说干就干,使用如鹏网的VC6.0开发向导能够非常轻松的搭建好一个界面

    我的界面例如以下


    程序模块:


    控件名和控件的ID(因为表格的限制仅仅给出了部分的控件名和控件的ID其他的请參考代码)

    控件名

    控件ID

    当前日期(static控件)

    IDC_STATIC

    (static控件)

    IDC_STATIC

    (static控件)

    IDC_STATIC

    (static控件)

    IDC_STATIC

    星期(static控件)

    IDC_STATIC

    (edit控件)

    IDC_EDIT3

    (edit控件)

    IDC_EDIT4

    (edit控件)

    IDC_EDIT5

    星期(edit控件)

    IDC_EDIT9

    当前时间(static控件)

    IDC_STATIC

    (static控件)

    IDC_STATIC

    (static控件)

    IDC_STATIC

    (static控件)

    IDC_STATIC

    (edit控件)

    IDC_EDIT6

    (edit控件)

    IDC_EDIT7

    (edit控件)

    IDC_EDIT8

    预设时间(static控件)

    IDC_STATIC

    (edit控件)

    IDC_EDIT10

    (edit控件)

    IDC_EDIT11

    (edit控件)

    IDC_EDIT12

    (edit控件)

    IDC_EDIT13


    函数模块:

    函数名

    函数功能

    is_strcmp

    推断星期

    TimerProc

    时间回调函数(显示时间)

    TimerProc1

    回调函数(用于定时关机)

    TimerProc2

    回调函数(用于定时重新启动)

    TimerProc3

    定时关闭显示器


    程序代码:

    #include "stdafx.h"
    #include <windows.h>
    #include <windowsx.h>
    #include "resource.h"
    #include "MainDlg.h"
    
    BOOL WINAPI Main_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
        switch(uMsg)
        {
            HANDLE_MSG(hWnd, WM_INITDIALOG, Main_OnInitDialog);
            HANDLE_MSG(hWnd, WM_COMMAND, Main_OnCommand);
    		HANDLE_MSG(hWnd,WM_CLOSE, Main_OnClose);
        }
    
        return FALSE;
    }
    
    /*定义一个函数,用于推断当前的星期*/
    void is_strcmp(HWND hwnd,TCHAR*str)
    {
    	if(0 == lstrcmp(str ,"1"))
    	{
    		SetDlgItemText(hwnd,IDC_EDIT9,"一");
    	}
    
    	if(0 == lstrcmp(str,"2"))
    	{
    		SetDlgItemText(hwnd,IDC_EDIT9,"二");
    	}
    
    	if(0 == lstrcmp(str,"3"))
    	{
    		SetDlgItemText(hwnd,IDC_EDIT9,"三");
    	}
    
    	if(0 == lstrcmp(str,"4"))
    	{
    		SetDlgItemText(hwnd,IDC_EDIT9,"四");
    	}
    
    	if(0 == lstrcmp(str,"5"))
    	{
    		SetDlgItemText(hwnd,IDC_EDIT9,"五");
    	}
    
    	if(0 == lstrcmp(str,"6"))
    	{
    		SetDlgItemText(hwnd,IDC_EDIT9,"六");
    	}
    
    	if(0 == lstrcmp(str,"0"))
    	{
    		SetDlgItemText(hwnd,IDC_EDIT9,"日");
    	}
    }
    
    /*定时器;显示时间*/
    void CALLBACK TimerProc(HWND hwnd, UINT message,UINT iTimerID, DWORD dwTime)
    {
    	TCHAR strYear[256];  /*用于保存当前时间:年*/
    	TCHAR strMonth[256]; /*用于保存当前时间:月*/
    	TCHAR strDay[256];   /*用于保存当前时间:日*/
    	TCHAR strHour[256];  /*用于保存当前时间:时*/
    	TCHAR strMinute[256];/*用于保存当前时间:分*/
    	TCHAR strScond[256]; /*用于保存当前时间:秒*/
    	TCHAR strWeek[256];/*用于保存当前时间:星期*/
    
    	SYSTEMTIME stLock; /*时间结构*/
    	GetLocalTime(&stLock);/*得到系统时间*/
    
    	wsprintf(strYear, "%d", stLock.wYear     );/*年*/
        SetDlgItemText(hwnd, IDC_EDIT3, strYear );/*年*/
        SetDlgItemText(hwnd, IDC_EDIT10, strYear );/*在预设时间中显示当前时间*/
    
    	wsprintf(strMonth, "%d", stLock.wMonth   );/*月*/
    	SetDlgItemText(hwnd, IDC_EDIT4, strMonth );/*月*/
    	SetDlgItemText(hwnd, IDC_EDIT11, strMonth );/*在预设时间中显示当前时间*/
    
    	wsprintf(strDay,   "%d", stLock.wDay   ); /*日*/
    	SetDlgItemText(hwnd, IDC_EDIT5, strDay );/*日*/
    	SetDlgItemText(hwnd, IDC_EDIT12, strDay );/*在预设时间中显示当前时间*/
    
    	wsprintf(strHour,  "%d", stLock.wHour   );/*时*/
    	SetDlgItemText(hwnd, IDC_EDIT6, strHour );/*时*/
    
    	wsprintf(strMinute, "%d", stLock.wMinute   );/*分*/
    	SetDlgItemText(hwnd, IDC_EDIT7, strMinute );/*分*/
    
    	wsprintf(strScond, "%d", stLock.wSecond);  /*秒*/
    	SetDlgItemText(hwnd, IDC_EDIT8, strScond );/*秒*/
    
    	wsprintf(strWeek, "%d", stLock.wDayOfWeek);/*星期*/
    	is_strcmp(hwnd,strWeek);/*调用is_strcmp函数改动星期*/
    }
    
    /*回掉函数:定时关机*/
    void CALLBACK TimerProc1(HWND hwnd, UINT message, UINT iTimerID, DWORD dwTime)
    {
    	KillTimer(hwnd,2);
    	KillTimer(hwnd,3);
    
    	TCHAR strHour[256];  /*用于保存当前时间:时*/
    	TCHAR strMinute[256];/*用于保存当前时间:分*/
    	TCHAR strScond[256]; /*用于保存当前时间:秒*/
    
    	SYSTEMTIME stLock; /*时间结构*/
    	GetLocalTime(&stLock);/*得到系统时间*/
    
    	wsprintf(strHour,  "%d", stLock.wHour   );/*时*/
    	SetDlgItemText(hwnd, IDC_EDIT6, strHour );/*时*/
    
    	wsprintf(strMinute, "%d", stLock.wMinute   );/*分*/
    	SetDlgItemText(hwnd, IDC_EDIT7, strMinute );/*分*/
    
    	wsprintf(strScond, "%d", stLock.wSecond);  /*秒*/
    	SetDlgItemText(hwnd, IDC_EDIT8, strScond );/*秒*/
    
    	TCHAR Hour[256]; /*保存用户定义的关机时间:时*/
    	TCHAR Minute[256];/*保存用户定义的关机时间:分*/
    	TCHAR Second[256];/*保存用户定义的关机时间:秒*/
    
    	GetDlgItemText(hwnd,IDC_EDIT13,Hour,sizeof(Hour));/*时*/
    	GetDlgItemText(hwnd,IDC_EDIT14,Minute,sizeof(Minute));/*分*/
    	GetDlgItemText(hwnd,IDC_EDIT15,Second,sizeof(Second));/*秒*/
    
    	if(0 == lstrcmp(strHour,Hour))
    	{
    		if(0 == lstrcmp(strMinute,Minute))
    		{
    			if(0 == lstrcmp(strScond,Second))
    			{
    				/*Dos命令中的关机命令*/
    				system("shutdown -s -t 0");
    			}
    		}
    	}
    
    	int total1;/*系统时间总和*/
    	int total2;/*预设时间总和*/
    	int total3;/*剩余时间总和*/
    
    	total1 = stLock.wHour * 3600 + stLock.wMinute * 60 + stLock.wSecond;/*系统时间的总和(秒)*/
    	total2 = atoi(Hour) * 3600 + atoi(Minute) * 60 + atoi(Second);/*预设时间总和(秒)*/
    	total3 = total2 - total1;/*剩余时间总和(秒)*/
    
    	int HOUR;/*时*/
    	int MINUTE;/*分*/
    	int SECOND;/*秒*/
    
    	HOUR = total3 / 3600;/*剩余时间(时)*/
    
    	MINUTE = (total3 % 3600) / 60;/*剩余时间(分)*/
    
    	SECOND = (total3 % 3600) % 60;/*剩余时间(秒)*/
    
    
    
    	TCHAR hour[256];/*存放剩余时间:时*/
    	TCHAR minute[256];/*存放剩余时间:分*/
    	TCHAR second[256];/*存放剩余时间:秒*/
    
    
    	wsprintf(hour,   "%d",   HOUR);
    	wsprintf(minute, "%d", MINUTE);
    	wsprintf(second, "%d", SECOND);
    
    	SetDlgItemText(hwnd,IDC_EDIT16,hour);/*显示剩余时间:(时)*/
    	SetDlgItemText(hwnd,IDC_EDIT17,minute);/*显示剩余时间:(分)*/
    	SetDlgItemText(hwnd,IDC_EDIT18,second);/*显示剩余时间;(秒)*/
    }
    
    /*回掉函数:定时又一次启动*/
    void CALLBACK TimerProc2(HWND hwnd, UINT message, UINT iTimerID, DWORD dwTime)
    {
    	KillTimer(hwnd,1);
    	KillTimer(hwnd,3);
    
    	TCHAR strHour[256];  /*用于保存当前时间:时*/
    	TCHAR strMinute[256];/*用于保存当前时间:分*/
    	TCHAR strScond[256]; /*用于保存当前时间:秒*/
    
    	SYSTEMTIME stLock; /*时间结构*/
    	GetLocalTime(&stLock);/*得到系统时间*/
    
    	wsprintf(strHour,  "%d", stLock.wHour   );/*时*/
    	SetDlgItemText(hwnd, IDC_EDIT6, strHour );/*时*/
    
    	wsprintf(strMinute, "%d", stLock.wMinute   );/*分*/
    	SetDlgItemText(hwnd, IDC_EDIT7, strMinute );/*分*/
    
    	wsprintf(strScond, "%d", stLock.wSecond);  /*秒*/
    	SetDlgItemText(hwnd, IDC_EDIT8, strScond );/*秒*/
    
    	TCHAR Hour[256]; /*保存用户定义的关机时间:时*/
    	TCHAR Minute[256];/*保存用户定义的关机时间:分*/
    	TCHAR Second[256];/*保存用户定义的关机时间:秒*/
    
    	GetDlgItemText(hwnd,IDC_EDIT13,Hour,sizeof(Hour));/*时*/
    	GetDlgItemText(hwnd,IDC_EDIT14,Minute,sizeof(Minute));/*分*/
    	GetDlgItemText(hwnd,IDC_EDIT15,Second,sizeof(Second));/*秒*/
    
    	if(0 == lstrcmp(strHour,Hour))
    	{
    		if(0 == lstrcmp(strMinute,Minute))
    		{
    			if(0 == lstrcmp(strScond,Second))
    			{
    				/*Dos命令中的关机命令*/
    				system("shutdown -r -t 0");
    			}
    		}
    	}
    
    	int total1;/*系统时间总和*/
    	int total2;/*预设时间总和*/
    	int total3;/*剩余时间总和*/
    
    	total1 = stLock.wHour * 3600 + stLock.wMinute * 60 + stLock.wSecond;/*系统时间的总和(秒)*/
    	total2 = atoi(Hour) * 3600 + atoi(Minute) * 60 + atoi(Second);/*预设时间总和(秒)*/
    	total3 = total2 - total1;/*剩余时间总和(秒)*/
    
    	int HOUR;/*时*/
    	int MINUTE;/*分*/
    	int SECOND;/*秒*/
    
    	HOUR = total3 / 3600;/*剩余时间(时)*/
    
    	MINUTE = (total3 % 3600) / 60;/*剩余时间(分)*/
    
    	SECOND = (total3 % 3600) % 60;/*剩余时间(秒)*/
    
    
    
    	TCHAR hour[256];/*存放剩余时间:时*/
    	TCHAR minute[256];/*存放剩余时间:分*/
    	TCHAR second[256];/*存放剩余时间:秒*/
    
    
    	wsprintf(hour,   "%d",   HOUR);
    	wsprintf(minute, "%d", MINUTE);
    	wsprintf(second, "%d", SECOND);
    
    	SetDlgItemText(hwnd,IDC_EDIT16,hour);/*显示剩余时间:(时)*/
    	SetDlgItemText(hwnd,IDC_EDIT17,minute);/*显示剩余时间:(分)*/
    	SetDlgItemText(hwnd,IDC_EDIT18,second);/*显示剩余时间;(秒)*/
    }
    
    /*回掉函数:定时关闭显示器*/
    void CALLBACK TimerProc3(HWND hwnd, UINT message, UINT iTimerID, DWORD dwTime)
    {
    	KillTimer(hwnd,1);
    	KillTimer(hwnd,2);
    
    	TCHAR strHour[256];  /*用于保存当前时间:时*/
    	TCHAR strMinute[256];/*用于保存当前时间:分*/
    	TCHAR strScond[256]; /*用于保存当前时间:秒*/
    
    	SYSTEMTIME stLock; /*时间结构*/
    	GetLocalTime(&stLock);/*得到系统时间*/
    
    	wsprintf(strHour,  "%d", stLock.wHour   );/*时*/
    	SetDlgItemText(hwnd, IDC_EDIT6, strHour );/*时*/
    
    	wsprintf(strMinute, "%d", stLock.wMinute   );/*分*/
    	SetDlgItemText(hwnd, IDC_EDIT7, strMinute );/*分*/
    
    	wsprintf(strScond, "%d", stLock.wSecond);  /*秒*/
    	SetDlgItemText(hwnd, IDC_EDIT8, strScond );/*秒*/
    
    	TCHAR Hour[256]; /*保存用户定义的关机时间:时*/
    	TCHAR Minute[256];/*保存用户定义的关机时间:分*/
    	TCHAR Second[256];/*保存用户定义的关机时间:秒*/
    
    	GetDlgItemText(hwnd,IDC_EDIT13,Hour,sizeof(Hour));/*时*/
    	GetDlgItemText(hwnd,IDC_EDIT14,Minute,sizeof(Minute));/*分*/
    	GetDlgItemText(hwnd,IDC_EDIT15,Second,sizeof(Second));/*秒*/
    
    	if(0 == lstrcmp(strHour,Hour))
    	{
    		if(0 == lstrcmp(strMinute,Minute))
    		{
    			if(0 == lstrcmp(strScond,Second))
    			{
    				/*关闭显示器*/
    				SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM)2);
    			}
    		}
    	}
    
    	int total1;/*系统时间总和*/
    	int total2;/*预设时间总和*/
    	int total3;/*剩余时间总和*/
    
    	total1 = stLock.wHour * 3600 + stLock.wMinute * 60 + stLock.wSecond;/*系统时间的总和(秒)*/
    	total2 = atoi(Hour) * 3600 + atoi(Minute) * 60 + atoi(Second);/*预设时间总和(秒)*/
    	total3 = total2 - total1;/*剩余时间总和(秒)*/
    
    	int HOUR;/*时*/
    	int MINUTE;/*分*/
    	int SECOND;/*秒*/
    
    	HOUR = total3 / 3600;/*剩余时间(时)*/
    
    	MINUTE = (total3 % 3600) / 60;/*剩余时间(分)*/
    
    	SECOND = (total3 % 3600) % 60;/*剩余时间(秒)*/
    
    
    
    	TCHAR hour[256];/*存放剩余时间:时*/
    	TCHAR minute[256];/*存放剩余时间:分*/
    	TCHAR second[256];/*存放剩余时间:秒*/
    
    
    	wsprintf(hour,   "%d",   HOUR);
    	wsprintf(minute, "%d", MINUTE);
    	wsprintf(second, "%d", SECOND);
    
    	SetDlgItemText(hwnd,IDC_EDIT16,hour);/*显示剩余时间:(时)*/
    	SetDlgItemText(hwnd,IDC_EDIT17,minute);/*显示剩余时间:(分)*/
    	SetDlgItemText(hwnd,IDC_EDIT18,second);/*显示剩余时间;(秒)*/
    }
    
    
    /*程序初始化*/
    BOOL Main_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam)
    	{
    		/*调用计时器,1秒钟调用一次TimerProc函数*/
    		SetTimer(hwnd,0,1000,TimerProc);/*用于显示时间*/
    
    		return TRUE;
    }
    
    void Main_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
    {
    	HWND hwndCheck1 = GetDlgItem(hwnd,IDC_CHECK);
    	HWND hwndCheck2 = GetDlgItem(hwnd, IDC_CHECKRESET);
    	HWND hwndCheck3 = GetDlgItem(hwnd,IDC_CHECKCLOSE);
    
    	switch(id)
        {
            case IDC_CHECK:/*关机*/
    		{
    			/*定时关机*/
    			SetTimer(hwnd,0,1000,TimerProc1);
    		}
    		break;
    
    		case IDC_CHECKRESET:/*又一次启动*/
    		{
    			SetTimer(hwnd,0,1000,TimerProc2);
    		}
    		break;
    
    		case IDC_CHECKCLOSE:/*关闭显示器*/
    		{
    			SetTimer(hwnd,0,1000,TimerProc3);
    		}
    		break;
    
    	     case IDC_START:
    		{
    			int index1 = Button_GetCheck(hwndCheck1);
    			int index2 = Button_GetCheck(hwndCheck2);
    			int index3 = Button_GetCheck(hwndCheck3);
    
    		    	if(BST_CHECKED == index1)
    				{
                        /*回掉函数:定时关机*/
    		        	MessageBox(hwnd,TEXT("计算机将关闭"),TEXT("提示"),MB_OK);
    				}
    
    		    	if(BST_CHECKED == index2)
    				{
    	                /*回掉函数:定时又一次启动*/
    	               MessageBox(hwnd,TEXT("计算机将又一次启动"),TEXT("提示"),MB_OK);
    				}
    
    			if(BST_CHECKED == index3)
    				{
                        /*回掉函数:定时关闭显示器*/
                        MessageBox(hwnd,TEXT("显示器将关闭"),TEXT("提示"),MB_OK);
    				}
    		}
    		break;
    
    		 case IDC_CANCEL:
    			 {
    				 MessageBox(hwnd,TEXT("取消计时"),TEXT("提示"),MB_OK);
    				 exit(-1);
    			 }
    			 break;
    
            case IDC_CLOSE:/*关机*/
    		{
                /*Dos命令中的关机命令*/
    			system("shutdown -s -t 0");
    		}
            break;
    
    		case IDC_RESET:/*又一次启动*/
    		{
                /*Dos命令中的又一次启动命令*/
    			system("shutdown -r -t 0");
    		}
    		break;
    
    		case IDC_CLOSE1:/*关闭显示器*/
    		{
    			/*关闭显示器*/
    			SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM)2);
    		}
    		break;
    
            default:
    		break;
        }
    }
    
    void Main_OnClose(HWND hwnd)
    {
        EndDialog(hwnd, 0);
    }
    


    程序运行结果:


    设置2014年6月21日20:45:0关机


    程序还能够关闭显示器 、又一次启动、在此我不再一一演示


    单击此处下载程序

    
  • 相关阅读:
    vc 定义返回值为字符串函数方法
    typedef用法(二)
    新版.Net开发必备十大工具【转自www.bitsCN.com】
    大公司面试题
    NET(C#)连接各类数据库集锦
    对对象类型和调用方法属性进行存储以提升反射性能
    数据库连接字符串大全
    C#操作注册表的方法
    上班族解除疲劳
    在Microsoft Visual Studio 2005上安装.net3.0开发环境(含开发环境下
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/4355231.html
Copyright © 2011-2022 走看看