概要

指定した時間に、パソコンをスリープします。スリープする時間は複数指定できます。設定されたスリープ時間は実行順にソートされて表示されます。

Unicode版 Windows XP以上、Visual C++ 2008 32bit/64bit

timer3.zip プログラムおよびソースリスト

 

プログラムリスト

timer.cpp


#include <windows.h>
#include <commctrl.h>
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <mmsystem.h>
#include <shlobj.h>
#include <direct.h>
#include <TCHAR.H>
#include "resource.h"


//#define SLEEP_TEST 1

int  GetTimeStr(struct tm* now_time);
void ShutDown(void);
void time_save(HWND hlistview,FILE* fp);
void time_read(HWND hlistview,FILE* fp);
void set_listview(HWND hlistview,int u,int w,struct tm now,struct tm* tmp,bool f=true);
#define ID_MYTIMER  100
DWORD Tid1;
bool time_chk(struct tm now,int h,int m,int w);
LRESULT CALLBACK time_DlgProc(HWND hDlg, UINT msg, WPARAM wp, LPARAM lp);
TCHAR SHUT_TIME_FILE[MAX_PATH];
TCHAR LOG_FILE[MAX_PATH];

TCHAR* week_str[]={ TEXT("日"),
                                        TEXT("月"),
                                        TEXT("火"),
                                        TEXT("水"),
                                        TEXT("木"),
                                        TEXT("金"),
                                        TEXT("土"),
                                        0 };


HWND hlistview=0;
int sortsubno[3]={ 0 ,0,0 };

int WINAPI _tWinMain(HINSTANCE hCurInst, HINSTANCE hPrevInst,
                   TCHAR* lpsCmdLine, int nCmdShow){
        SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, 0, (TCHAR*)SHUT_TIME_FILE);
        _tcscat_s(SHUT_TIME_FILE,MAX_PATH,TEXT( "\\yamatyuusoft") );
        _tmkdir(SHUT_TIME_FILE);
        _tcscat_s(SHUT_TIME_FILE,MAX_PATH,TEXT( "\\shout_down_clock") );
        _tmkdir(SHUT_TIME_FILE);
        _tcscat_s(SHUT_TIME_FILE,MAX_PATH,TEXT("\\shuttime.csv"));
                
        SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, 0, (TCHAR*)LOG_FILE);
        _tcscat_s(LOG_FILE,MAX_PATH,TEXT( "\\yamatyuusoft") );
        _tmkdir(LOG_FILE);
        _tcscat_s(LOG_FILE,MAX_PATH,TEXT( "\\shout_down_clock") );
        _tmkdir(LOG_FILE);
        _tcscat_s(LOG_FILE,MAX_PATH,TEXT("\\log.csv"));
                
        DialogBox(0 , TEXT("TIME_SET"), 0, (DLGPROC)time_DlgProc);
    return 0;
}



void str_to_time(TCHAR* buf,int& h,int& m,int& w){
        TCHAR* p=buf;
        TCHAR* mp=buf;
        TCHAR* wp=buf;
        while(*p){
                if(*p == _TEXT(':')){
                        *p=_TEXT('\0');
                        h = _ttoi(buf);
                        mp=p+1;
                }else if(*p == TEXT(' ') || *p == TEXT(' ')){
                        wp=p+1;
                        break;
                }
                ++p;
        }
        w=0;
        m=_ttoi(mp);
        int c;
        while( c = *wp ){
                switch(*wp){
                        case _T('日'):   w |= 0x40; break;
                        case _T('月'):   w |= 0x20; break;
                        case _T('火'):   w |= 0x10; break;
                        case _T('水'):   w |= 0x08; break;
                        case _T('木'):   w |= 0x04; break;
                        case _T('金'):   w |= 0x02; break;
                        case _T('土'):   w |= 0x01; break;
                }
                ++wp;
        }
}

void str_to_time2(TCHAR* buf,TCHAR* week,int& h,int& m,int& w){
        TCHAR* p=buf;
        TCHAR* mp=buf;
        TCHAR* wp=week;
        while(*p){
                if(*p == _TEXT(':')){
                        *p=_TEXT('\0');
                        h = _ttoi(buf);
                        mp=p+1;
                }else if(*p == TEXT(' ')){
                        wp=p+1;                                                         
                }
                ++p;
        }
        w=0;
        m=_ttoi(mp);
        while(*wp){
                switch(*wp){
                        case _T('日'):   w |= 0x40; break;
                        case _T('月'):   w |= 0x20; break;
                        case _T('火'):   w |= 0x10; break;
                        case _T('水'):   w |= 0x08; break;
                        case _T('木'):   w |= 0x04; break;
                        case _T('金'):   w |= 0x02; break;
                        case _T('土'):   w |= 0x01; break;
                }
                ++wp;
        }
}


void time_to_str(TCHAR* buf,int s,int h,int m,int w){
        _stprintf_s(buf,s,TEXT("%i:%i "),h,m);
        if( w & 0x40 )
                _tcscat_s(buf,s,TEXT("日"));
        if( w & 0x20 )
                _tcscat_s(buf,s,TEXT("月"));
        if( w & 0x10 )
                _tcscat_s(buf,s,TEXT("火"));
        if( w & 0x08 )
                _tcscat_s(buf,s,TEXT("水"));
        if( w & 0x04 )
                _tcscat_s(buf,s,TEXT("木"));
        if( w & 0x02 )
                _tcscat_s(buf,s,TEXT("金"));
        if( w & 0x01 )
                _tcscat_s(buf,s,TEXT("土"));
}

void time_to_str2(TCHAR* str,struct tm* t){
                        if(t->tm_mon+1<10){
                                str[0]=_T('0');
                                str[1]=_T('0')+t->tm_mon+1;
                        }else{
                                str[0]=_T('0')+(t->tm_mon+1)/10;
                                str[1]=_T('0')+(t->tm_mon+1)%10;
                        }
                        str[2]=_T('/');
        
                        if(t->tm_mday<10){
                                str[3]=_T('0');
                                str[4]=_T('0')+t->tm_mday;
                        }else{
                                str[3]=_T('0')+t->tm_mday/10;
                                str[4]=_T('0')+t->tm_mday%10;
                        }
                        str[5]=_T(' ');

        
                        if(t->tm_hour<10){
                                str[6]=_T('0');
                                str[7]=_T('0')+t->tm_hour;
                        }else{
                                str[6]=_T('0')+t->tm_hour/10;
                                str[7]=_T('0')+t->tm_hour%10;
                        }
                        str[8]=_T(':');
                        if(t->tm_min<10){
                                str[9]=_T('0');
                                str[10]=_T('0')+t->tm_min;
                        }else{
                                str[9]=_T('0')+t->tm_min/10;
                                str[10]=_T('0')+t->tm_min%10;
                        }
                        str[11]=_T('\0');
}


//      現在より一番近い日時を検索 aw:結果

void next_sleep_time(struct tm now,struct tm** tmp,int h,int m,int w){
                        int f=0;        // 1の場合は次の日から検索

                        if( now.tm_hour*3600+now.tm_min*60+now.tm_sec > h*3600+m*60 ){
                                f=1;
                        }
                        now.tm_hour=h;
                        now.tm_min=m;
                        now.tm_sec=0;
                        int awx;

                        int aw=7;

                        int t;
                        if(w & 0x40){       //      日
                                awx=0;
                                if(now.tm_wday+f <= awx)     //      同じ週     
                                        t=awx-now.tm_wday;
                                else
                                        t=awx-now.tm_wday+7; // 来週
                                if(aw>t)
                                        aw=t;
                        }
                        if(w & 0x20){       //      月
                                awx=1;
                                if(now.tm_wday+f<=awx)       //      同じ週     
                                        t=awx-now.tm_wday;
                                else
                                        t=awx-now.tm_wday+7; // 来週
                                if(aw>t)
                                        aw=t;
                        }
                        if(w & 0x10){       //      火
                                awx=2;
                                if(now.tm_wday+f<=awx)       //      同じ週     
                                        t=awx-now.tm_wday;
                                else
                                        t=awx-now.tm_wday+7; // 来週
                                if(aw>t)
                                        aw=t;
                        }
                        if(w & 0x08){       //      水
                                awx=3;
                                if(now.tm_wday+f<=awx)       //      同じ週     
                                        t=awx-now.tm_wday;
                                else
                                        t=awx-now.tm_wday+7; // 来週
                                if(aw>t)
                                        aw=t;
                        }
                        if(w & 0x04){       //      木
                                awx=4;
                                if(now.tm_wday+f<=awx)       //      同じ週     
                                        t=awx-now.tm_wday;
                                else
                                        t=awx-now.tm_wday+7; // 来週
                                if(aw>t)
                                        aw=t;
                        }
                        if(w & 0x02){       //      金
                                awx=5;
                                if(now.tm_wday+f<=awx)       //      同じ週     
                                        t=awx-now.tm_wday;
                                else
                                        t=awx-now.tm_wday+7; // 来週
                                if(aw>t)
                                        aw=t;
                        }
                        if(w & 0x01){       //      土
                                awx=6;
                                if(now.tm_wday+f<=awx)       //      同じ週     
                                        t=awx-now.tm_wday;
                                else
                                        t=awx-now.tm_wday+7; // 来週
                                if(aw>t)
                                        aw=t;
                        }


                        time_t tt=mktime(&now);
                        tt += aw*24*3600;
                        localtime_s(*tmp,&tt);

}

INT64 time_diff(struct tm* dtc,struct tm* src){
                return mktime(dtc)-mktime(src);
}

void set_listview(HWND hlistview,int u,int w,struct tm now,struct tm* tmp,bool f){
                        TCHAR str[20];
                        time_to_str2(str,tmp);

                        LV_ITEM item;
                        item.mask = LVIF_TEXT  | LVIF_PARAM;
                        item.pszText = str;
                        item.iItem = u;
                        item.iSubItem = 0;
                        item.lParam = u;
                        if(f==true)
                                ListView_InsertItem(hlistview, &item);
                        else
                                ListView_SetItem(hlistview, &item);


                        item.mask = LVIF_TEXT;
                        item.pszText = str+6;
                        item.iItem = u;
                        item.iSubItem = 1;
                        ListView_SetItem(hlistview, &item);

                        size_t s=20;

                        if( w & 0x40 )
                                _tcscpy_s(str,s,TEXT("日"));
                        else
                                _tcscpy_s(str,s,TEXT("  "));

                        if( w & 0x20 )
                                _tcscat_s(str,s,TEXT("月"));
                        else
                                _tcscat_s(str,s,TEXT("  "));

                        if( w & 0x10 )
                                _tcscat_s(str,s,TEXT("火"));
                        else
                                _tcscat_s(str,s,TEXT("  "));

                        if( w & 0x08 )
                                _tcscat_s(str,s,TEXT("水"));
                        else
                                _tcscat_s(str,s,TEXT("  "));

                        if( w & 0x04 )
                                _tcscat_s(str,s,TEXT("木"));
                        else
                                _tcscat_s(str,s,TEXT("  "));

                        if( w & 0x02 )
                                _tcscat_s(str,s,TEXT("金"));
                        else
                                _tcscat_s(str,s,TEXT("  "));

                        if( w & 0x01 )
                                _tcscat_s(str,s,TEXT("土"));
                        else
                                _tcscat_s(str,s,TEXT("  "));

                        item.mask = LVIF_TEXT;
                        item.pszText = str;
                        item.iItem = u;
                        item.iSubItem = 2;
                        ListView_SetItem(hlistview, &item);

}

/*
void listbox_to_listview(HWND hlistbox,HWND hlistview){
        TCHAR buf[32];
        int h,m,w;
        int u,e;
        struct tm now;
        GetTimeStr(&now);

        //アイテムを全て削除する 
        ListView_DeleteAllItems(hlistview);

                e=SendMessage(hlistbox,LB_GETCOUNT,0,0);
                for(u=0;u<e;u++){
                        int f=0;        // 1の場合は次の日から検索
                        SendMessage(hlistbox,LB_GETTEXT,u,(LPARAM)buf);

                        str_to_time(buf,h,m,w);
                        struct tm* tmp;
                        next_sleep_time(now,&tmp, h,m,w);

                        set_listview(hlistview,u,w,now,tmp);
#if 0
                        TCHAR str[20];
                        time_to_str2(str,tmp);

                        LV_ITEM item;
                        item.mask = LVIF_TEXT  | LVIF_PARAM;
                        item.pszText = str;
                        item.iItem = u;
                        item.iSubItem = 0;
                        item.lParam = u;

                        ListView_InsertItem(hlistview, &item);


                        item.mask = LVIF_TEXT;
                        item.pszText = str+6;
                        item.iItem = u;
                        item.iSubItem = 1;
                        ListView_SetItem(hlistview, &item);

                        size_t s=20;

                        if( w & 0x40 )
                                _tcscpy_s(str,s,TEXT("日"));
                        else
                                _tcscpy_s(str,s,TEXT("  "));

                        if( w & 0x20 )
                                _tcscat_s(str,s,TEXT("月"));
                        else
                                _tcscat_s(str,s,TEXT("  "));

                        if( w & 0x10 )
                                _tcscat_s(str,s,TEXT("火"));
                        else
                                _tcscat_s(str,s,TEXT("  "));

                        if( w & 0x08 )
                                _tcscat_s(str,s,TEXT("水"));
                        else
                                _tcscat_s(str,s,TEXT("  "));

                        if( w & 0x04 )
                                _tcscat_s(str,s,TEXT("木"));
                        else
                                _tcscat_s(str,s,TEXT("  "));

                        if( w & 0x02 )
                                _tcscat_s(str,s,TEXT("金"));
                        else
                                _tcscat_s(str,s,TEXT("  "));

                        if( w & 0x01 )
                                _tcscat_s(str,s,TEXT("土"));
                        else
                                _tcscat_s(str,s,TEXT("  "));

                        item.mask = LVIF_TEXT;
                        item.pszText = str;
                        item.iItem = u;
                        item.iSubItem = 2;
                        ListView_SetItem(hlistview, &item);
#endif
                }

}
*/
/*
int CALLBACK MyCompProc(LPARAM lp1, LPARAM lp2, LPARAM lp3){
    static LV_FINDINFO lvf;
    static int nItem1, nItem2;
    static TCHAR buf1[64], buf2[64];

    lvf.flags = LVFI_PARAM;
    lvf.lParam = lp1;
    nItem1 = ListView_FindItem(hlistview, -1, &lvf);

    lvf.lParam = lp2;
    nItem2 = ListView_FindItem(hlistview, -1, &lvf);
    
    ListView_GetItemText(hlistview, nItem1, (int)lp3, buf1, sizeof(buf1));
    ListView_GetItemText(hlistview, nItem2, (int)lp3, buf2, sizeof(buf2));
        int i=_tcscmp(buf1,buf2);
        if(sortsubno[(int)lp3] == 0)
                return i;
        else
                return -i;
        
}
*/
struct ITEM{
        INT64 diff;
        struct tm tmp;
        int w;
};


int cmp_func(const void* d,const void* s){
        ITEM* dtc=(ITEM*)d;
        ITEM* src=(ITEM*)s;
        return (int)(dtc->diff - src->diff);
}

void sort_item(HWND hlistview){
        struct tm now;
        GetTimeStr(&now);
        INT64 t=-1;     //      スリープまでの残り時間
        int i=(int)SendMessage(hlistview,LVM_GETITEMCOUNT,0,0);
        ITEM* item=new ITEM[i];
        TCHAR buf1[32];
        TCHAR buf2[32];
        int n;
        for(n=0;n<i;n++){
                TCHAR buf[32];
                int h,m;

                ListView_GetItemText(hlistview, n, (int)1, buf1,32);
                ListView_GetItemText(hlistview, n, (int)2, buf2,32);

                _stprintf_s(buf,sizeof(buf)/sizeof(TCHAR),TEXT("%s %s"),buf1 , buf2);

                str_to_time(buf,h,m,item[n].w);
                struct tm  tmp_t;
                struct tm* tmp=&tmp_t;
                next_sleep_time(now,&tmp,h,m,item[n].w);
                item[n].tmp=*tmp;
                INT64 tt=time_diff(&item[n].tmp,&now);

                item[n].diff=tt;
        }
        qsort(item,i,sizeof(ITEM),cmp_func);
        ListView_DeleteAllItems(hlistview);
        for(n=0;n<i;n++)
                set_listview(hlistview,n,item[n].w,now,&item[n].tmp,true);


        delete [] item;

}


LRESULT CALLBACK time_DlgProc(HWND hDlg, UINT msg, WPARAM wp, LPARAM lp){
    static HWND hUpdown_h, hEdit_h;
    static HWND hUpdown_m, hEdit_m;
        static HWND htime;
        static HWND hclock;
    struct tm now;
        static int sd=0;
        static int iClickNo=-1;
        FILE* fp;
        int n;
        TCHAR buf[32];
        int h,m,w;

    switch (msg) {
        case WM_INITDIALOG:
                        hlistview=GetDlgItem(hDlg,IDC_LISTVIEW);
                        htime=GetDlgItem(hDlg,IDC_LABEL110);
                        hclock=GetDlgItem(hDlg,IDC_LABEL111);

                        LV_COLUMN lvcol;

                        lvcol.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
            lvcol.fmt = LVCFMT_LEFT;
            lvcol.cx = 80;
            lvcol.pszText = TEXT("実行日時");
            lvcol.iSubItem = 0;
            ListView_InsertColumn(hlistview, 0, &lvcol);


                        lvcol.cx = 48;
            lvcol.pszText = TEXT("日時");
            lvcol.iSubItem = 1;
            ListView_InsertColumn(hlistview, 1, &lvcol);


                        lvcol.cx = 154;
            lvcol.pszText = TEXT("曜日");
            lvcol.iSubItem = 2;
            ListView_InsertColumn(hlistview, 2, &lvcol);

                        if(!_tfopen_s(&fp,SHUT_TIME_FILE,TEXT("r"))){
                                time_read(hlistview,fp);
                                fclose(fp);
                        }
                        if(!_tfopen_s(&fp,LOG_FILE,TEXT("a"))){
                                time_t tt;
                                time(&tt);
                                _ftprintf(fp,TEXT("start,%s\n"), _tctime(&tt));
                                fclose(fp);
                        }

                        hUpdown_h = GetDlgItem(hDlg, IDC_UDCTRL_H);
                        hEdit_h = GetDlgItem(hDlg, IDC_EDIT_H);
                        SendMessage(hUpdown_h, UDM_SETBUDDY, (WPARAM)hEdit_h, 0);
                        SendMessage(hUpdown_h, UDM_SETRANGE, (WPARAM)0, (LPARAM)MAKELONG(23,0));
                        SendMessage(hUpdown_h, UDM_SETPOS, 0, (LPARAM)0);

                        hUpdown_m = GetDlgItem(hDlg, IDC_UDCTRL_M);
                        hEdit_m = GetDlgItem(hDlg, IDC_EDIT_M);
                        SendMessage(hUpdown_m, UDM_SETBUDDY, (WPARAM)hEdit_m, 0);
                        SendMessage(hUpdown_m, UDM_SETRANGE, (WPARAM)0, (LPARAM)MAKELONG(59,0));
                        SendMessage(hUpdown_m, UDM_SETPOS, 0, (LPARAM)0);
            if(SetTimer(hDlg, ID_MYTIMER, 1000, NULL) == 0) {
                    MessageBox(hDlg,
                    TEXT("タイマー失敗!"),
                    TEXT("失敗"),
                    MB_OK | MB_ICONEXCLAMATION);
            }

                        return TRUE;

                case WM_TIMER:{
            GetTimeStr(&now);
                        INT64 t=-1;     //      スリープまでの残り時間
                        int i=(int)SendMessage(hlistview,LVM_GETITEMCOUNT,0,0);
                        for(n=0;n<i;n++){
                                TCHAR buf[32];
                                TCHAR ts[64];
                                TCHAR ws[64];
                                                           
                                ListView_GetItemText(hlistview, n, (int)1, ts, sizeof(ts)/sizeof(TCHAR));
                                ListView_GetItemText(hlistview, n, (int)2, ws, sizeof(ws)/sizeof(TCHAR));

                                _stprintf_s(buf,sizeof(buf)/sizeof(TCHAR),TEXT("%s %s"),ts,ws);


                                str_to_time(buf,h,m,w);
                                struct tm tmp_t;
                                struct tm* tmp=&tmp_t;
                                next_sleep_time(now,&tmp,h,m,w);
                                INT64 tt=time_diff(tmp,&now);
                                if(t==-1)
                                        t=tt;
                                else{
                                        if(tt < t)
                                                t=tt;
                                }
                        }
                        TCHAR buf[32];

                        _stprintf_s(buf,sizeof(buf)/sizeof(TCHAR),TEXT("%i/%i/%i %s %i:%i:%i"),now.tm_year+1900,now.tm_mon+1,now.tm_mday,week_str[now.tm_wday],now.tm_hour , now.tm_min , now.tm_sec);
                        SetWindowText(hclock,buf);
                        if(i==0)
                                break;
                        if( -1 != t ){
                                _stprintf_s(buf,sizeof(buf)/sizeof(TCHAR),TEXT("スリープまで%i:%i:%i"),int(t/3600),int((t/60)%60),int(t%60));
                                SetWindowText(htime,buf);
                                if(t==0){
                                        KillTimer(hDlg, ID_MYTIMER);
                                        if(!_tfopen_s(&fp,LOG_FILE,TEXT("a"))){
                                                _ftprintf(fp,TEXT("Sleep set ,%i %i %i\n"), h,m,w);
                                                fclose(fp);
                                        }
                                        SetWindowText(htime,TEXT("スリープ"));
                                        SendMessage(hDlg,WM_COMMAND,IDC_SLEEP,0);
                                        sort_item(hlistview);
                                        if(SetTimer(hDlg, ID_MYTIMER, 1000, NULL) == 0) {
                                                        MessageBox(hDlg,
                                                        TEXT("タイマー失敗!"),
                                                        TEXT("失敗"),
                                                        MB_OK | MB_ICONEXCLAMATION);
                                        }


                                }
                        }
                        break; }
        case WM_PAINT:{
            break; 
                }
                case WM_NOTIFY:
                        if((int)wp==IDC_LISTVIEW){

                                LPNMHDR lpnmhdr;
                                lpnmhdr = (LPNMHDR)lp;

                                switch(lpnmhdr->code){
                                case NM_CLICK:{         //      ユーザーがコントロールで左ボタンをクリック   時刻の編集
                                        LV_FINDINFO lvf;
                                            LPNMLISTVIEW lpnmlv;

                            lpnmlv = (LPNMLISTVIEW)lp;
                                        iClickNo = lpnmlv->iItem + 1;
                                        if (iClickNo == 0) {
                                                break;
                                        }else{
                                                lvf.flags = LVFI_PARAM;
                                                lvf.lParam = iClickNo;
                                            ListView_FindItem(hlistview, -1, &lvf);
                                                TCHAR ts[64];
                                                TCHAR ws[64];
                                           
                                            ListView_GetItemText(hlistview, iClickNo-1, (int)1, ts, sizeof(ts)/sizeof(TCHAR));
                                            ListView_GetItemText(hlistview, iClickNo-1, (int)2, ws, sizeof(ws)/sizeof(TCHAR));
                                                str_to_time2(ts,ws,h,m,w);
                                                SendMessage(GetDlgItem(hDlg, IDC_WEEKDAY), BM_SETCHECK, w & 0x40 ? BST_CHECKED : BST_UNCHECKED  , 0L);
                                                SendMessage(GetDlgItem(hDlg, IDC_WEEKDAY+1), BM_SETCHECK, w & 0x20 ? BST_CHECKED : BST_UNCHECKED  , 0L);
                                                SendMessage(GetDlgItem(hDlg, IDC_WEEKDAY+2), BM_SETCHECK, w & 0x10 ? BST_CHECKED : BST_UNCHECKED  , 0L);
                                                SendMessage(GetDlgItem(hDlg, IDC_WEEKDAY+3), BM_SETCHECK, w & 0x08 ? BST_CHECKED : BST_UNCHECKED  , 0L);
                                                SendMessage(GetDlgItem(hDlg, IDC_WEEKDAY+4), BM_SETCHECK, w & 0x04 ? BST_CHECKED : BST_UNCHECKED  , 0L);
                                                SendMessage(GetDlgItem(hDlg, IDC_WEEKDAY+5), BM_SETCHECK, w & 0x02 ? BST_CHECKED : BST_UNCHECKED  , 0L);
                                                SendMessage(GetDlgItem(hDlg, IDC_WEEKDAY+6), BM_SETCHECK, w & 0x01 ? BST_CHECKED : BST_UNCHECKED  , 0L);

                                                SendMessage(hUpdown_h, UDM_SETPOS, 0, (LPARAM)h);
                                                SendMessage(hUpdown_m, UDM_SETPOS, 0, (LPARAM)m);

                                        }
                                }break;

                                }
                        }
                        break;
        case WM_COMMAND:
            switch (LOWORD(wp)) {
                case IDC_EXIT:
                                        if(!_tfopen_s(&fp,SHUT_TIME_FILE,TEXT("w"))){
                                                time_save(hlistview,fp);
                                                fclose(fp);
                                        }
                    EndDialog(hDlg, IDC_EXIT);
                    return TRUE;

                                case IDCANCEL:
                    EndDialog(hDlg, IDCANCEL);
                    return TRUE;

                                case IDOK:
                                        if(!_tfopen_s(&fp,SHUT_TIME_FILE,TEXT("w"))){
                                                time_save(hlistview,fp);
                                                fclose(fp);
                                        }
                    EndDialog(hDlg, IDOK);
                    return TRUE;

                                case IDC_SET: {
                                        if(0<iClickNo){      
                                                n=iClickNo;
                                                if(0 <= n){
                                                        h = (int)SendMessage(hUpdown_h, UDM_GETPOS, 0, 0);
                                                        m = (int)SendMessage(hUpdown_m, UDM_GETPOS, 0, 0);
                                                        w = 0;
                                                        w |= (IsDlgButtonChecked(hDlg, IDC_WEEKDAY  ) == BST_CHECKED) ? 0x40 : 0;
                                                        w |= (IsDlgButtonChecked(hDlg, IDC_WEEKDAY+1) == BST_CHECKED) ? 0x20 : 0;
                                                        w |= (IsDlgButtonChecked(hDlg, IDC_WEEKDAY+2) == BST_CHECKED) ? 0x10 : 0;
                                                        w |= (IsDlgButtonChecked(hDlg, IDC_WEEKDAY+3) == BST_CHECKED) ? 0x08 : 0;
                                                        w |= (IsDlgButtonChecked(hDlg, IDC_WEEKDAY+4) == BST_CHECKED) ? 0x04 : 0;
                                                        w |= (IsDlgButtonChecked(hDlg, IDC_WEEKDAY+5) == BST_CHECKED) ? 0x02 : 0;
                                                        w |= (IsDlgButtonChecked(hDlg, IDC_WEEKDAY+6) == BST_CHECKED) ? 0x01 : 0;
                                                        time_to_str(buf,sizeof(buf)/sizeof(TCHAR),h,m,w);

                                                        if(!_tfopen_s(&fp,SHUT_TIME_FILE,TEXT("w"))){
                                                                time_save(hlistview,fp);
                                                                fclose(fp);
                                                        }
                                                }
                                                GetTimeStr(&now);
                                                struct tm tmp_t;
                                                struct tm* tmp=&tmp_t;
                                                next_sleep_time(now,&tmp, h,m,w);

                                                set_listview(hlistview,iClickNo-1,w,now,tmp,false);
                                                iClickNo=-1;
                                                sort_item(hlistview);
                                        }
                                
                                        break;
                                }

                                case IDC_DEL:
                                        if(iClickNo!=-1)
                                                ListView_DeleteItem(hlistview, iClickNo-1);

                                        iClickNo=-1;
                                        sort_item(hlistview);

                                        break;

                                case IDC_NEW:{
                                        h = (int)SendMessage(hUpdown_h, UDM_GETPOS, 0, 0);
                                        m = (int)SendMessage(hUpdown_m, UDM_GETPOS, 0, 0);
                                        w = 0;
                                        w |= (IsDlgButtonChecked(hDlg, IDC_WEEKDAY  ) == BST_CHECKED) ? 0x40 : 0;
                                        w |= (IsDlgButtonChecked(hDlg, IDC_WEEKDAY+1) == BST_CHECKED) ? 0x20 : 0;
                                        w |= (IsDlgButtonChecked(hDlg, IDC_WEEKDAY+2) == BST_CHECKED) ? 0x10 : 0;
                                        w |= (IsDlgButtonChecked(hDlg, IDC_WEEKDAY+3) == BST_CHECKED) ? 0x08 : 0;
                                        w |= (IsDlgButtonChecked(hDlg, IDC_WEEKDAY+4) == BST_CHECKED) ? 0x04 : 0;
                                        w |= (IsDlgButtonChecked(hDlg, IDC_WEEKDAY+5) == BST_CHECKED) ? 0x02 : 0;
                                        w |= (IsDlgButtonChecked(hDlg, IDC_WEEKDAY+6) == BST_CHECKED) ? 0x01 : 0;

                                        int i=(int)SendMessage(hlistview,LVM_GETITEMCOUNT,0,0);
                                        GetTimeStr(&now);
                                        struct tm tmp_t;
                                        struct tm* tmp=&tmp_t;
                                        next_sleep_time(now,&tmp, h,m,w);

                                        set_listview(hlistview,i,w,now,tmp,true);
                                        sort_item(hlistview);

                                        break;
                                        }
                                case IDC_LOG_VIEW:
                                        ShellExecute(NULL,TEXT("open"),LOG_FILE,TEXT(""),TEXT(""),SW_SHOW);
                                        break;
                                case IDC_LOG_DEL:
                                        DeleteFile(LOG_FILE);
                                        break;
                                case IDC_SLEEP:
                                        sd=1;
                                        _tfopen_s(&fp,SHUT_TIME_FILE,TEXT("w"));
                                        time_save(hlistview,fp);
                                        fclose(fp);
                                        if(!_tfopen_s(&fp,LOG_FILE,TEXT("a"))){
                                                time_t tt;
                                                time(&tt);
                                                _ftprintf(fp,TEXT("Sleep,%s\n"), _tctime(&tt));
                                                fclose(fp);
                                        }
                                        ShutDown();
                                        break;

            }
    }
    return FALSE;
}

bool time_chk(struct tm now,int h,int m,int w){
        int src1=h*60+m;
        int src2=src1+1;
        int dtc=now.tm_hour*60+now.tm_min;
        int tw=0;
        switch(now.tm_wday){
                case 0: tw=0x40;        break;
                case 1: tw=0x20;        break;
                case 2: tw=0x10;        break;
                case 3: tw=0x08;        break;
                case 4: tw=0x04;        break;
                case 5: tw=0x02;        break;
                case 6: tw=0x01;        break;
        }

        if( (tw & w) && src1<=dtc && dtc<=src2)
                return true;
        else
                return false;
}

int GetTimeStr(struct tm* now_time){
    
    time_t long_time;
    time(&long_time);
    localtime_s(now_time,&long_time);
    return 0;
}
//      Windowsをシャットダウンする。

void ShutDown(void){

#ifndef SLEEP_TEST

        DWORD ret;
    HANDLE hToken;
    LUID    Luid;
    TOKEN_PRIVILEGES    tNew,tPre;

        
        HANDLE hProcess = GetCurrentProcess();
    OpenProcessToken(hProcess, TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES, &hToken);
    LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &Luid);
    tNew.PrivilegeCount = 1;
    tNew.Privileges[0].Luid = Luid;
    tNew.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
    AdjustTokenPrivileges(hToken, FALSE, &tNew, sizeof(tPre), &tPre, &ret);
        SetSystemPowerState(FALSE, FALSE);
#else
        MessageBox(0,TEXT("メッセージ"),TEXT("スリープします"),MB_OK);

#endif

}

void time_save(HWND hlistbox,FILE* fp){
        TCHAR buf[32];
#ifdef UNICODE
        char sjis[32*2];
#else
        char* sjis=buf;
#endif
        int i=(int)SendMessage(hlistview,LVM_GETITEMCOUNT,0,0);
        for(int n=0;n<i;n++){
                TCHAR ts[64];
                TCHAR ws[64];
                                           
            ListView_GetItemText(hlistview, n, (int)1, ts, sizeof(ts)/sizeof(TCHAR));
            ListView_GetItemText(hlistview, n, (int)2, ws, sizeof(ws)/sizeof(TCHAR));

                _stprintf_s(buf,sizeof(buf)/sizeof(TCHAR),TEXT("%s %s"),ts,ws);
                
#ifdef UNICODE
                WideCharToMultiByte(932,0,buf,-1,sjis,sizeof(sjis),NULL,NULL);
#endif
                fprintf(fp,"%s\n",sjis);
        }
}

void time_read(HWND hlistview,FILE* fp){
        char sjis[32*2];
#ifdef UNICODE
        TCHAR buf[32];
#else
        char* buf;
#endif
        int u=0;
        struct tm now;
        GetTimeStr(&now);
        while(fgets(sjis,sizeof(sjis),fp)!=NULL){
                int i=(int)strlen(sjis);
                if(sjis[i-1]=='\n')
                        sjis[i-1]='\0';
                if(1<i){
#ifdef UNICODE
                        MultiByteToWideChar(932,0,sjis,-1,buf,sizeof(buf)/sizeof(TCHAR));
#else
                        buf=sjis;
#endif
                        int h,m,w;
                        str_to_time(buf,h,m,w);
                        struct tm tmp_t;
                        struct tm* tmp=&tmp_t;
                        next_sleep_time(now,&tmp, h,m,w);
                        set_listview(hlistview,u,w,now,tmp);
                }
        }
        sort_item(hlistview);

}
</pre>

<h3>resource.h</h3>

<pre>#define      IDC_EDIT_H              100
#define IDC_UDCTRL_H            101
#define IDC_EDIT_M              102
#define IDC_UDCTRL_M            103
#define IDC_EXIT                105
#define IDC_BUTTON107           107
#define IDC_LABEL108            108
#define IDC_LABEL109            109
#define IDC_LABEL110            110
#define IDC_LABEL111            111
#define IDC_LABEL112            112
#define IDC_LABEL113            113
#define IDC_LABEL114            114

//#define IDC_LISTBOX   115
#define IDC_SET 116
#define IDC_NEW 117
#define IDC_DEL 118
#define IDC_LOG_VIEW 120
#define IDC_LOG_DEL 121
#define IDC_LISTVIEW    119
#define IDC_WEEKDAY 130
#define IDC_SLEEP 140

timer.rc


#include <windows.h>
#include "resource.h"


MYICON                  ICON    DISCARDABLE     "timer3.ico"


TIME_SET DIALOG DISCARDABLE 0, 0, 249, 173
EXSTYLE WS_EX_DLGMODALFRAME
STYLE WS_POPUP | WS_CAPTION | DS_SETFONT | WS_SYSMENU | WS_MINIMIZEBOX

CAPTION "スリープ時刻設定"
FONT 9, "MS UI Gothic"
{
 CONTROL "スリープ時刻一覧", -1, "STATIC", WS_CHILD | WS_VISIBLE | SS_NOTIFY, 7, 7, 97, 12
  CONTROL "",IDC_LISTVIEW,"SYSLISTVIEW32", WS_CHILD | WS_VISIBLE |   LVS_REPORT , 7, 20, 150, 87


 CONTROL "日", IDC_WEEKDAY  , "BUTTON",  BS_AUTOCHECKBOX | WS_CHILD | WS_VISIBLE | WS_TABSTOP,   7, 114, 20, 12
 CONTROL "月", IDC_WEEKDAY+1, "BUTTON",  BS_AUTOCHECKBOX | WS_CHILD | WS_VISIBLE | WS_TABSTOP,  27, 114, 20, 12
 CONTROL "火", IDC_WEEKDAY+2, "BUTTON",  BS_AUTOCHECKBOX | WS_CHILD | WS_VISIBLE | WS_TABSTOP,  47, 114, 20, 12
 CONTROL "水", IDC_WEEKDAY+3, "BUTTON",  BS_AUTOCHECKBOX | WS_CHILD | WS_VISIBLE | WS_TABSTOP,  67, 114, 20, 12
 CONTROL "木", IDC_WEEKDAY+4, "BUTTON",  BS_AUTOCHECKBOX | WS_CHILD | WS_VISIBLE | WS_TABSTOP,  87, 114, 20, 12
 CONTROL "金", IDC_WEEKDAY+5, "BUTTON",  BS_AUTOCHECKBOX | WS_CHILD | WS_VISIBLE | WS_TABSTOP, 107, 114, 20, 12
 CONTROL "土", IDC_WEEKDAY+6, "BUTTON",  BS_AUTOCHECKBOX | WS_CHILD | WS_VISIBLE | WS_TABSTOP, 127, 114, 20, 12

 CONTROL "", IDC_EDIT_H, "EDIT",  ES_AUTOHSCROLL,   7, 133 20, 12
 CONTROL "", IDC_UDCTRL_H, "MSCTLS_UPDOWN32", UDS_WRAP | UDS_SETBUDDYINT | UDS_ALIGNRIGHT | UDS_ARROWKEYS | UDS_NOTHOUSANDS,  32, 133, 16, 12
 CONTROL "時", IDC_LABEL108, "STATIC", WS_CHILD | WS_VISIBLE | SS_NOTIFY, 30, 135, 16, 8

 CONTROL "", IDC_EDIT_M, "EDIT", ES_AUTOHSCROLL, 52, 133, 20, 12
 CONTROL "", IDC_UDCTRL_M, "MSCTLS_UPDOWN32",UDS_WRAP | UDS_SETBUDDYINT | UDS_ALIGNRIGHT | UDS_ARROWKEYS | UDS_NOTHOUSANDS ,  77, 133, 16, 12
 CONTROL "分", IDC_LABEL109, "STATIC", WS_CHILD | WS_VISIBLE | SS_NOTIFY,  75, 135, 16, 8

 CONTROL "適用", IDC_SET, "BUTTON", WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,  7, 152, 26, 14
 CONTROL "新規", IDC_NEW, "BUTTON", WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON, 33, 152, 26, 14
 CONTROL "削除", IDC_DEL, "BUTTON", WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON, 59, 152, 26, 14

 CONTROL "ログ閲覧", IDC_LOG_VIEW, "BUTTON", WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,  97, 152, 30, 14
 CONTROL "ログ削除", IDC_LOG_DEL, "BUTTON", WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON, 127, 152, 30, 14



 
 CONTROL "", IDC_LABEL110, "STATIC", WS_CHILD | WS_VISIBLE | SS_NOTIFY, 171, 14, 80, 8
CONTROL "", IDC_LABEL111, "STATIC", WS_CHILD | WS_VISIBLE | SS_NOTIFY, 171, 24, 80, 8

CONTROL "スリープ", IDC_SLEEP, "BUTTON", WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,192,114, 50, 14
CONTROL "終了", IDC_EXIT, "BUTTON", WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,192, 152, 50, 14
}

//--------------------
//  ヴァージョン情報
//--------------------

VS_VERSION_INFO VERSIONINFO
 FILEVERSION 1,0,0,3
 PRODUCTVERSION 1,0,0,3
 FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
 FILEFLAGS 0x1L
#else
 FILEFLAGS 0x0L
#endif
 FILEOS 0x4L
 FILETYPE 0x1L
 FILESUBTYPE 0x0L
BEGIN
    BLOCK "StringFileInfo"
    BEGIN
        BLOCK "040904b0"
        BEGIN
            VALUE "Comments", "マルチスリープタイマー\0"
            VALUE "CompanyName","キラー"
            VALUE "FileDescription", "\0"
            VALUE "FileVersion", "1,0,0,0"
            VALUE "InternalName", "1,0,0,0\0"
            VALUE "LegalCopyright", "Copyright (C) 2012/03/11\0"
            VALUE "OriginalFilename", "timer.exe\0"
            VALUE "ProductName", "マルチスリープタイマー"
            VALUE "ProductVersion",  "1,0,0,0"
        END
    END
    BLOCK "VarFileInfo"
    BEGIN
        VALUE "Translation",0x0411, 0x03A4 
    END
END