用win32实现 提醒喝水小助手

mac2024-07-04  57

#include <windows.h> #include <cstdio> int g_isQuit = 6; template <typename T> void MyPrintf(char* str,T data){ AllocConsole(); HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE); //HANDLE hInput = GetStdHandle(STD_INPUT_HANDLE); //HANDLE hError = GetStdHandle(STD_ERROR_HANDLE); char buff[1024]={0}; sprintf(buff,str,data); WriteConsole(hOutput,buff,strlen(buff),NULL,NULL); return; } void PrintwParam(WPARAM wParam){ MyPrintf("HIWORD(wParam):%d ",HIWORD(wParam)); MyPrintf("LOWORD(wParam):%d\n",LOWORD(wParam)); return; } void PrintlParam(LPARAM lParam){ POINT pos; pos.x = LOWORD(lParam); pos.y = HIWORD(lParam); MyPrintf("HIWORD(lParam):%d ",pos.x); MyPrintf("LOWORD(lParam):%d\n",pos.y); return; } void OnCreate(HWND hwnd){ char buff[] = "喝水好! \n清晨喝水好!\n有情饮水饱,\n省却牛奶和面包。\n\n喝水好!\n清晨喝水好!\n神仙只喝水一瓢,\n人间炊烟太火燥。\n\n喝水好!\n清晨喝水好!\n不必将茶泡,\n别弄烧卖和虾饺。\n\n喝水好!\n清晨喝水好!\n心肠洗净了,\n身心健康自然好。\n"; MessageBox(hwnd,buff,"喝水小助手提醒你:",MB_OK); int ret = MessageBox(hwnd,"本程序将在后台运行,每两个小时提醒您喝一次水。小逼崽子听懂了吗?","Read Me!",MB_YESNO); //MyPrintf("%d",ret); if(ret == 6){//是 MessageBox(hwnd,"真乖~快去忙吧!","叉会腰~",MB_OK); SetTimer(hwnd,007,7200*1000,NULL); }else{ MessageBox(hwnd,"大爷/姑奶奶,我错了!宁放心的去工作吧!","卑微",MB_OK); SetTimer(hwnd,8,7200*1000,NULL); } } /* This is where all the input to the window goes to */ LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) { switch(Message) { case WM_CREATE: OnCreate(hwnd); break; /* trap the WM_CLOSE (clicking X) message, and actually tell the window to close */ case WM_CLOSE: { DestroyWindow(hwnd); break; } /* Upon destruction, tell the main thread to stop */ case WM_DESTROY: { PostQuitMessage(0); break; } case WM_TIMER: switch(LOWORD(wParam)){ case 7: g_isQuit = MessageBox(hwnd,"小乖乖~该喝水了~放下手头工作,休息休息吧!(退出请选否)","老子提醒您:",MB_YESNO|MB_TOPMOST); break; case 8: g_isQuit = MessageBox(hwnd,"大爷/姑奶奶,宁喝口水吧,别累坏了身子。(退出请选否)","卑微小程序提醒宁:",MB_YESNO|MB_TOPMOST); break; } if(g_isQuit != 6) PostQuitMessage(0); //PrintlParam(lParam); //PrintwParam(wParam); break; /* All other messages (a lot of them) are processed using default procedures */ default: return DefWindowProc(hwnd, Message, wParam, lParam); } return 0; } /* The 'main' function of Win32 GUI programs: this is where execution starts */ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WNDCLASSEX wc; /* A properties struct of our window */ HWND hwnd; /* A 'HANDLE', hence the H, or a pointer to our window */ MSG Msg; /* A temporary location for all messages */ /* zero out the struct and set the stuff we want to modify */ memset(&wc,0,sizeof(wc)); wc.cbSize = sizeof(WNDCLASSEX); wc.lpfnWndProc = WndProc; /* This is where we will send messages to */ wc.hInstance = hInstance; wc.hCursor = LoadCursor(NULL, IDC_ARROW); /* White, COLOR_WINDOW is just a #define for a system color, try Ctrl+Clicking it */ wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wc.lpszClassName = "WindowClass"; wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); /* Load a standard icon */ wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); if(!RegisterClassEx(&wc)) { MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK); return 0; } hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","Caption",WS_VISIBLE|WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, /* x */ CW_USEDEFAULT, /* y */ 0, /* width */ 0, /* height */ NULL,NULL,hInstance,NULL); if(hwnd == NULL) { MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK); return 0; } ShowWindow(hwnd,SW_HIDE); UpdateWindow(hwnd); /* This is the heart of our program where all input is processed and sent to WndProc. Note that GetMessage blocks code flow until it receives something, so this loop will not produre unreasonably CPU usage */ while(GetMessage(&Msg, NULL, 0, 0) > 0) { /* If no error is received... */ TranslateMessage(&Msg); /* Translate keycodes to chars if present */ DispatchMessage(&Msg); /* Send it to WndProc */ } return Msg.wParam; }

说明:提醒喝水小程序2.0

该程序每两个小时,提醒用户喝一次水。当小程序挑衅你的时候,选择是与否的不同,小程序对你说话的语气将不一样。

最新回复(0)