commit d2a10300a54df4ba214f684a6c3a11d3e2d75012 Author: 千嶂夹城 Date: Mon May 26 14:17:14 2025 +0800 修正了跨天计时在一些情况下不归零的bug diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9d830a9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.vscode/ +*.exe +*.txt \ No newline at end of file diff --git a/selfMonitor.cpp b/selfMonitor.cpp new file mode 100644 index 0000000..54f4e9a --- /dev/null +++ b/selfMonitor.cpp @@ -0,0 +1,140 @@ +#pragma comment(linker, "/subsystem:\"Windows\" /entry:\"mainCRTStartup\"") +#include +#include +#include +#include +#include +using namespace std; +// +const int BLACK_LIST_SIZE = 3; +string blackList[]={"","MuMuģ","֪"}; +int blackListTimeCount = 0; +const int MAX_BLACKLIST_TIME_COUNT = 1800; +const int TOLENT_MORE_TIME = 1200; +void saveData(){ + // ʱ䵽save.txt + FILE *fp = fopen("save.txt", "w"); + if(fp!=NULL){ + time_t timeNow = time(0); + tm *curTime; + if(curTime->tm_hour<4) curTime = localtime(&timeNow-86400); + else curTime = localtime(&timeNow); + fprintf(fp, "%d %d %d %d", blackListTimeCount,curTime->tm_year,curTime->tm_mon,curTime->tm_mday); + fclose(fp); + } +} +bool beginWithMuteFlag=false; +void muteAll(){ + keybd_event(VK_SPACE, 0, 0, 0); + Sleep(1); + keybd_event(VK_SPACE, 0, KEYEVENTF_KEYUP, 0); +} +void unmuteAll(){ + keybd_event(VK_SPACE, 0, 0, 0); + Sleep(1); + keybd_event(VK_SPACE, 0, KEYEVENTF_KEYUP, 0); +} +int main(){ + ShowWindow(GetConsoleWindow(), SW_HIDE); + system("chcp 936 >>nul"); + FreeConsole(); + srand((unsigned)time(0)); + char title[1024]; + // save.txtжȡʱ + FILE *fp = fopen("save.txt", "r"); + if(fp!=NULL){ + int curYear,curMonth,curDay; + fscanf(fp, "%d%d%d%d", &blackListTimeCount,&curYear,&curMonth,&curDay); + time_t timeNow = time(0); + tm *curTime; + if(curTime->tm_hour<4) curTime = localtime(&timeNow-86400); + else curTime = localtime(&timeNow); + if(curTime->tm_year!=curYear||curTime->tm_mon!=curMonth||curTime->tm_mday!=curDay){ + blackListTimeCount = 0; + } + fclose(fp); + } + cout<<"Hello, Self Monitor!"; + while(true){ + // жʱĵ + time_t now = time(0); + tm *ltm = localtime(&now); + if(ltm->tm_hour==4&<m->tm_min==0){ + blackListTimeCount = 0; + } + // ȡ㴰 + HWND hwnd = GetForegroundWindow(); + if (hwnd == NULL) { + continue; + } + if(GetWindowTextA(hwnd, title, 1024) == 0){ + cout<<"GetWindowTextW failed"<=MAX_BLACKLIST_TIME_COUNT){ + muteAll(); + bool isContinue = true; + isContinue = MessageBoxA(hwnd, "СʱˣȷҪ", "Сʱ", MB_YESNO) == IDYES; + if(isContinue){ + MessageBoxA(hwnd, "ȷĻҵָʮΣ", "û취", MB_OK); + for(int i=0;i<10;i++){ + int randNum = rand()&1; + if(randNum){ + isContinue = MessageBoxA(hwnd, "˼һ£õԵıҪ\nӦʲô\n˰ȷ", "˼˼", MB_YESNO) == IDYES; + }else{ + isContinue = MessageBoxA(hwnd, "˼һ£õԵıҪ\nӦʲô\n˰ȡ", "˼˼", MB_YESNO) == IDNO; + } + if(!isContinue){ + MessageBoxA(hwnd, "㲢û¶ء", "̾", MB_OK); + break; + } + } + } + unmuteAll(); + if(isContinue){ + MessageBoxA(hwnd, "ðɣȷʵм£ȥæɣöʮӺһģ", "æ", MB_OK); + blackListTimeCount = MAX_BLACKLIST_TIME_COUNT - TOLENT_MORE_TIME; + }else{ + MessageBoxA(hwnd, "ǧּгƶķ첻׼ˣ\nӱҪļǿƹص","True Ending",MB_OK); + Sleep(5*1000); + // ǿƹرhwnd + PostMessageA(hwnd, WM_CLOSE, 0, 0); + } + }else if(blackListTimeCount%600==0){ + muteAll(); + MessageBoxA(hwnd, "Ѿʮˣͣ£", "ʮ", MB_OK); + unmuteAll(); + }else if(blackListTimeCount%300==0){ + muteAll(); + MessageBoxA(hwnd, "ˣȷϢ", "", MB_OK); + unmuteAll(); + } + } + // ʱ + Sleep(1000); + static int clockCount = 0; + clockCount++; + if(clockCount%60==0){ + saveData(); + } + // cout<