マルチシャットダウンタイマー

概要

指定した時間に、パソコンをシャットダウンします。シャットダウンする時間は複数指定できます。WakeOnLANでパソコンを起動できるようにすれば、任意の時間に起動して終了することが可能となります。下図は、6:00と10:00にシャットダウンするように設定した例です。パソコンが起動時に自動的に本ソフトが起動するように、プログラムメニューのスタートアップに本ソフトのショートカットを登録しておきます。私はTV録画に使用しています。

Unicode版
Windows 2000以上、Visual C++ 2005 再配布パッケージが必要。
ダウンロード

clock.lzh プログラムおよびソースリスト

 

プログラムリスト

clock.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"

void DrawCircle(HDC, int, int, int, int);
void DrawLine(HDC hdc,int x1,int y1,int x2,int y2);
void pointer(HDC hdc,int x,int y,int r,double qh);
void pointer2(HDC hdc,int x,int y,int r,double qh);
void GetWinRect(HWND hWnd, int *x, int *y);
int  GetTimeStr(struct tm* now_time);
void ShutDown(void);
void time_save(HWND hlistbox,FILE* fp);
void time_read(HWND hlistbox,FILE* fp);

#define ID_MYTIMER  100
DWORD Tid1;
bool time_chk(struct tm now,int h,int m);
LRESULT CALLBACK time_DlgProc(HWND hDlg, UINT msg, WPARAM wp, LPARAM lp);
TCHAR FILE_NAME[MAX_PATH];

class   METER{
        int     start;          //メータ目盛開始角度
        int     end;            //メータ目盛終了角度
        int     x,y;            //メータ中心位置
        int r2;                 //メータ半径
        int     val;            //指針位置
        int     length;         //val最大値
        int peti;               //目盛数
        int max;                //目盛数字最大
public:
        METER(int px,int py,int pr,int s,int e,int v,int l,int p,int m){
                x=px;
                y=py;
                r2=pr;
                start=s;
                end=e;
                val=v;
                length=l;
                peti=p;
                max=m;
        }
        METER(){}
        void set(int px,int py,int pr,int s,int e,int v,int l,int p,int m){
                x=px;
                y=py;
                r2=pr;
                start=s;
                end=e;
                val=v;
                length=l;
                peti=p;
                max=m;
        }
        int operator=(int p){
                return val=p;
        }
        void draw(HWND hWnd,HDC hdc,int h,int m,int s,int ss){
                double q;
                int r=r2;
            HPEN hPen, hOldPen;
                HBRUSH hBrush, hOldBrush;
            hPen = CreatePen(PS_SOLID, 1, RGB(0, 255, 0));
            hBrush = (HBRUSH)GetStockObject(NULL_BRUSH);
            hOldPen = (HPEN)SelectObject(hdc, hPen);
            hOldBrush= (HBRUSH)SelectObject(hdc, hBrush);

                DrawCircle(hdc, x-r,y-r,x+r, y+r);
                int n=3;
                for(q=0;q<=2*3.14;q+=2*3.14/12){
                        DrawLine(hdc,int(x+(r-10)*cos(q)),int(y+(r-10)*sin(q)),int(x+(r)*cos(q)),int(y+(r)*sin(q)));
                }
                double qh,qm,qs;
                qh=double(h)/12*2*3.14;
                qm=double(m)/60*2*3.14;
                qs=double(s)/60*2*3.14;
                qh=(qh+qm/12)-3.14/2;
                qm=(qm+qs/60)-3.14/2;
                qs=qs-3.14/2;


                pointer(hdc,x,y,r-15,qh);
                pointer(hdc,x,y,r,qm);
                DrawLine( hdc,int(x+(r+10)*cos(qs)),int(y+(r+10)*sin(qs)),x,y);

                SelectObject(hdc, hOldPen);
                SelectObject(hdc, hOldBrush);
                DeleteObject(hPen);


        }
        void draw2(HWND hWnd,HDC hdc,int h,int m,int s,int ss){
                HPEN hPen, hOldPen;
        
                hPen = CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
                hOldPen =(HPEN) SelectObject(hdc, hPen);

                int r=r2;
                int n=3;
                double qh,qm,qs;
                qh=double(h)/12*2*3.14;
                qm=double(m)/60*2*3.14;
                qs=double(s)/60*2*3.14;
                qh=(qh+qm/12)-3.14/2;
                qm=(qm+qs/60)-3.14/2;
                qs=qs-3.14/2;

                pointer2(hdc,x,y,r-15,qh);

                SelectObject(hdc, hOldPen);
                DeleteObject(hPen);

        }
};

METER rpm;


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

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 hlistbox;
        static HWND hclock;
        static int wx,wy;
    struct tm now;
        static int sd=0;
        FILE* fp;
        int n;
        TCHAR buf[32];
        int h,m;

    switch (msg) {
        case WM_INITDIALOG:
                        hlistbox=GetDlgItem(hDlg, IDC_LISTBOX);
                        if(!_tfopen_s(&fp,FILE_NAME,TEXT("r"))){
                                time_read(hlistbox,fp);
                                fclose(fp);
                        }
                        hclock=GetDlgItem(hDlg,IDC_STATIC111);
                        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);
            GetWinRect(hDlg, &wx, &wy);
            if(SetTimer(hDlg, ID_MYTIMER, 1000, NULL) == 0) {
                    MessageBox(hDlg,
                    TEXT("タイマー失敗!"),
                    TEXT("失敗"),
                    MB_OK | MB_ICONEXCLAMATION);
            }
            GetWinRect(hclock, &wx, &wy);
                        rpm.set(wx/2,wy/2,(wx<wy ? wx : wy )*40/100,45,315,0,10000,10,10);
                        return TRUE;

                case WM_TIMER:{
            GetTimeStr(&now);
                        InvalidateRect(hclock,NULL,TRUE);
                        UpdateWindow(hclock);

                        HDC hclock_hdc=GetDC(hclock);
                
                        rpm.draw(hclock,hclock_hdc,now.tm_hour,now.tm_min,now.tm_sec,0);

                        int i=SendMessage(hlistbox,LB_GETCOUNT,0,0);
                        for(n=0;n<i;n++){
                                SendMessage(hlistbox,LB_GETTEXT,n,(LPARAM)buf);

                                TCHAR* p=buf;
                                TCHAR* mp=buf;
                                while(*p){
                                        if(*p == _TEXT(':')){
                                                *p=_TEXT('\0');
                                                h = _ttoi(buf);
                                                mp=p+1;
                                        }
                                        ++p;
                                }
                                m=_ttoi(mp);
                                rpm.draw2(hDlg,hclock_hdc,h,m,0,0);
                                if(time_chk(now,h,m)==true && sd==0){
                                        sd=1;
                                        _tfopen_s(&fp,FILE_NAME,TEXT("w"));
                                        time_save(hlistbox,fp);
                                        fclose(fp);
                                        ShutDown();
                                }
                        }
                        ReleaseDC(hclock,hclock_hdc);
                        break; }
        case WM_PAINT:{
            GetTimeStr(&now);
                        InvalidateRect(hclock,NULL,TRUE);
                        UpdateWindow(hclock);
                        HDC hclock_hdc=GetDC(hclock);
                
                        rpm.draw(hclock,hclock_hdc,now.tm_hour,now.tm_min,now.tm_sec,0);
                        ReleaseDC(hclock,hclock_hdc);
            break; 
                }
        case WM_COMMAND:
            switch (LOWORD(wp)) {
                                case IDC_LISTBOX:
                                        switch(HIWORD(wp)){
                                        case LBN_SELCHANGE:
                                                int i=SendMessage(hlistbox,LB_GETCURSEL,0,0);
                                                SendMessage(hlistbox,LB_GETTEXT,i,(LPARAM)buf);
                                                TCHAR* p=buf;
                                                TCHAR* mp=buf;
                                                while(*p){
                                                        if(*p == _TEXT(':')){
                                                                *p=_TEXT('\0');
                                                                h = _ttoi(buf);
                                                                mp=p+1;
                                                        }
                                                        ++p;
                                                }
                                                m=_ttoi(mp);
                                                SendMessage(hUpdown_h, UDM_SETPOS, 0, (LPARAM)h);
                                                SendMessage(hUpdown_m, UDM_SETPOS, 0, (LPARAM)m);
                                        }
                                        break;

                case IDC_EXIT:
                                        _tfopen_s(&fp,FILE_NAME,TEXT("w"));
                                        time_save(hlistbox,fp);
                                        fclose(fp);
                    EndDialog(hDlg, IDC_EXIT);
                    return TRUE;

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

                                case IDOK:
                                        _tfopen_s(&fp,FILE_NAME,TEXT("w"));
                                        time_save(hlistbox,fp);
                                        fclose(fp);
                    EndDialog(hDlg, IDOK);
                    return TRUE;

                                case IDC_SET: {
                                        n=SendMessage(hlistbox,LB_GETCURSEL,0,0);
                                        if(0 <= n){
                                        h = (int)SendMessage(hUpdown_h, UDM_GETPOS, 0, 0);
                                        m = (int)SendMessage(hUpdown_m, UDM_GETPOS, 0, 0);
                                        _stprintf_s(buf,sizeof(buf)/sizeof(TCHAR),TEXT("%i:%i"),h,m);
                                        SendMessage(hlistbox, LB_DELETESTRING, (WPARAM)n, 0);
                                        SendMessage(hlistbox, LB_INSERTSTRING, (WPARAM)n, (LPARAM)buf);
                                        }
                                        break;
                                }

                                case IDC_DEL:
                                        n=SendMessage(hlistbox,LB_GETCURSEL,0,0);
                                        SendMessage(hlistbox, LB_DELETESTRING, (WPARAM)n, 0);
                                        break;

                                case IDC_NEW:
                                        h = (int)SendMessage(hUpdown_h, UDM_GETPOS, 0, 0);
                                        m = (int)SendMessage(hUpdown_m, UDM_GETPOS, 0, 0);
                                        _stprintf_s(buf,sizeof(buf)/sizeof(TCHAR),TEXT("%i:%i"),h,m);
                                        SendMessage(hlistbox, LB_INSERTSTRING, (WPARAM)-1, (LPARAM)buf);
                                        break;

            }
    }
    return FALSE;
}

bool time_chk(struct tm now,int h,int m){
        int src1=h*60+m;
        int src2=src1+1;
        int dtc=now.tm_hour*60+now.tm_min;
        if(src1<=dtc && dtc<=src2)
                return true;
        else
                return false;
}

void pointer(HDC hdc,int x,int y,int r,double qh){
        int x1,y1,x2,y2;
        DrawLine(hdc,x1=int(x+6*cos(qh+90*3.14/180)),y1=int(y+6*sin(qh+90*3.14/180)),x,y);
        DrawLine(hdc,x2=int(x+6*cos(qh-90*3.14/180)),y2=int(y+6*sin(qh-90*3.14/180)),x,y);
        DrawLine(hdc,x+int((r)*cos(qh)),int(y+(r)*sin(qh)),x1,y1);
        DrawLine(hdc,int(x+(r)*cos(qh)),int(y+(r)*sin(qh)),x2,y2);
        DrawLine(hdc,int(x+10*cos(qh+3.14)),int(y+10*sin(qh+3.14)),x1,y1);
        DrawLine(hdc,int(x+10*cos(qh+3.14)),int(y+10*sin(qh+3.14)),x2,y2);
}

void pointer2(HDC hdc,int x,int y,int r,double qh){
        int x1,y1,x2,y2;
        DrawLine(hdc,x1=int(x+6*cos(qh+90*3.14/180)),y1=int(y+6*sin(qh+90*3.14/180)),x,y);
        DrawLine(hdc,x2=int(x+6*cos(qh-90*3.14/180)),y2=int(y+6*sin(qh-90*3.14/180)),x,y);
        DrawLine(hdc,x+int((r)*cos(qh)),int(y+(r)*sin(qh)),x1,y1);
        DrawLine(hdc,int(x+(r)*cos(qh)),int(y+(r)*sin(qh)),x2,y2);
        DrawLine(hdc,int(x+10*cos(qh+3.14)),int(y+10*sin(qh+3.14)),x1,y1);
        DrawLine(hdc,int(x+10*cos(qh+3.14)),int(y+10*sin(qh+3.14)),x2,y2);
}

void DrawLine(HDC hdc,int x1,int y1,int x2,int y2){
    MoveToEx(hdc, x1, y1, NULL);
    LineTo(hdc, x2, y2);
}

void DrawCircle(HDC hdc, int x1, int y1, int x2, int y2){
        Ellipse(hdc, x1, y1, x2, y2);
}

void GetWinRect(HWND hWnd, int *x, int *y){
    RECT rc;
    GetClientRect(hWnd, &rc);
    *x = rc.right - rc.left;
    *y = rc.bottom - rc.top;
    return;
}

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){
    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);
        ExitWindowsEx(EWX_POWEROFF, 0);
}

void time_save(HWND hlistbox,FILE* fp){
        TCHAR buf[32];
        int i=SendMessage(hlistbox,LB_GETCOUNT,0,0);
        for(int n=0;n<i;n++){
                SendMessage(hlistbox,LB_GETTEXT,n,(LPARAM)buf);
                _ftprintf(fp,TEXT("%s\n"),buf);
        }
}

void time_read(HWND hlistbox,FILE* fp){
        TCHAR buf[32];
        while(_fgetts(buf,sizeof(buf)/sizeof(TCHAR),fp)!=NULL){
                int i=_tcslen(buf);
                if(buf[i-1]==_TEXT('\n'))
                        buf[i-1]=_TEXT('\0');
                if(1<i)
                        SendMessage(hlistbox, LB_INSERTSTRING, (WPARAM)-1, (LPARAM)buf);
        }
}

resource.h

#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_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_STATIC111   119

clock.rc

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

TIME_SET DIALOG DISCARDABLE 0, 0, 254, 116
EXSTYLE WS_EX_DLGMODALFRAME
STYLE WS_POPUP | WS_CAPTION | DS_SETFONT
CAPTION "シャットダウン時刻設定"
FONT 9, "MS UI Gothic"
{
 CONTROL "シャットダウン時間一覧", -1, "STATIC", WS_CHILD | WS_VISIBLE | SS_NOTIFY, 7, 7, 133, 12
 CONTROL "", IDC_LISTBOX, "LISTBOX", WS_CHILD | WS_VISIBLE | WS_VSCROLL | LBS_STANDARD, 7, 20, 133, 40

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

 CONTROL "", IDC_EDIT_M, "EDIT", ES_AUTOHSCROLL , 47, 67, 20, 12
 CONTROL "", IDC_UDCTRL_M, "MSCTLS_UPDOWN32",UDS_WRAP | UDS_SETBUDDYINT | UDS_ALIGNRIGHT | UDS_ARROWKEYS | UDS_NOTHOUSANDS , 72, 67, 16, 12
 CONTROL "分", IDC_LABEL109, "STATIC", WS_CHILD | WS_VISIBLE | SS_NOTIFY, 70, 69, 16, 8

 CONTROL "適用", IDC_SET, "BUTTON", WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON, 80, 67, 20, 14
 CONTROL "新規", IDC_NEW, "BUTTON", WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,100, 67, 20, 14
 CONTROL "削除", IDC_DEL, "BUTTON", WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,120, 67, 20, 14

 CONTROL "", IDC_STATIC111, "STATIC", WS_CHILD | WS_VISIBLE | SS_SUNKEN | SS_NOTIFY, 147, 7, 100, 100


 CONTROL "終了", IDC_EXIT, "BUTTON", WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,7, 95, 133, 14
}

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

VS_VERSION_INFO VERSIONINFO
 FILEVERSION 1,0,0,0
 PRODUCTVERSION 1,0,0,0
 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) 2007/09/30\0"
            VALUE "OriginalFilename", "clock.exe\0"
            VALUE "ProductName", "マルチシャットダウンタイマー"
            VALUE "ProductVersion",  "1,0,0,0"
        END
    END
    BLOCK "VarFileInfo"
    BEGIN
        VALUE "Translation",0x0411, 0x03A4 
    END
END