一:目的
累计电脑日使用时间,超过8小时候提醒:请注意休息!
二:要点
三:实现
四:源代码
#include <windows.h> LRESULT CALLBACK WinProc(HWND,UINT,WPARAM,LPARAM); void auto_run(); UINT second,minute,hour,day,month; int font_size,window_width; DWORD dw; //打杂的 int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd) { auto_run(); day=GetPrivateProfileInt("Section0","Day",0,"time.ini"); SYSTEMTIME st; GetSystemTime(&st); if(day != st.wDay) second,minute,hour=0; else{ hour=GetPrivateProfileInt("Section0","Hour",0,"time.ini"); minute=GetPrivateProfileInt("Section0","Minute",0,"time.ini"); } font_size = GetSystemMetrics(SM_CYSCREEN)/30; window_width=font_size*6; WNDCLASS wc; wc.style=CS_HREDRAW|CS_VREDRAW; wc.lpfnWndProc=(WNDPROC)WinProc; wc.cbClsExtra=0; wc.cbWndExtra=0; wc.hInstance=hInstance; wc.hIcon=LoadIcon(NULL,IDI_APPLICATION); wc.hCursor=LoadCursor(NULL,IDC_ARROW); wc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH); wc.lpszMenuName=NULL; wc.lpszClassName="MyWindow"; RegisterClass(&wc);//WS_EX_TOOLWINDOW风格可以使得不在任务栏上显示 HWND hwnd=CreateWindowEx(WS_EX_TOOLWINDOW,"MyWindow","My Window",WS_POPUP, GetSystemMetrics(SM_CXSCREEN)-window_width,0,window_width,100, NULL,NULL,hInstance,NULL); if(!hwnd) return false; /*增加窗口透明属性*/ /*动态调用SetLayeredWindowAttributes,使窗口透明*/ LONG l = GetWindowLong(hwnd,GWL_EXSTYLE); SetWindowLong(hwnd,GWL_EXSTYLE,l^0x00080000); HINSTANCE hInst = LoadLibrary("User32.DLL"); if(hInst) { typedef BOOL (WINAPI *MYFUNC)(HWND,COLORREF,BYTE,DWORD); MYFUNC fun = NULL; //取得SetLayeredWindowAttributes函数指针 fun=(MYFUNC)GetProcAddress(hInst, "SetLayeredWindowAttributes"); if(fun) fun(hwnd,RGB(255,255,255),50,1); FreeLibrary(hInst); } /////////////////////////////////////////// ShowWindow(hwnd,nShowCmd); UpdateWindow(hwnd); MSG msg; while(GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } LRESULT CALLBACK WinProc(HWND h,UINT msg,WPARAM wParam,LPARAM lParam) { HDC hdc; PAINTSTRUCT ps; RECT rect; HFONT hFont; char str[20],str2[20]; SYSTEMTIME st; switch(msg) { case WM_TIMER: second+=1; //hour+=1; 调试用 if(second==60){ second=0;minute+=1;
InvalidateRect(h,NULL,false); wsprintf(str,"%d",hour); WritePrivateProfileString("Section0","Hour",str,"time.ini"); wsprintf(str,"%d",minute); WritePrivateProfileString("Section0","Minute",str,"time.ini"); GetSystemTime(&st); if(st.wDay!=day || day==0){ wsprintf(str,"%d%02d%02d",st.wYear,st.wMonth,st.wDay); wsprintf(str2,"%02d%02d",hour,minute); WritePrivateProfileString("Section1",str,str2,"time.ini"); day=st.wDay; } wsprintf(str,"%d",st.wDay); WritePrivateProfileString("Section0","Day",str,"time.ini"); } if(minute==60){minute=0;hour+=1;} return 0; case WM_CREATE: SetTimer(h,0,1000,NULL); return 0; case WM_PAINT: hdc=BeginPaint(h,&ps); GetClientRect(h,&rect); if(hour<8) wsprintf(str,"%02d:%02d",hour,minute); else wsprintf(str,"%02d:%02d\n请注意休息!",hour,minute); hFont = CreateFont(font_size,0,0,0,FW_SEMIBOLD, 0,0,0, DEFAULT_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,DEFAULT_PITCH,NULL); SelectObject(hdc,hFont); if(hour<4) SetTextColor(hdc,RGB(0,255,0)); else if(hour>=4 && hour<8) SetTextColor(hdc,RGB(0,0,255)); else SetTextColor(hdc,RGB(255,0,0)); DrawText(hdc,str,-1,&rect,DT_LEFT); DeleteObject(hFont); EndPaint(h,&ps); return 0; // /* 调试用 case WM_KEYDOWN: if(wParam==VK_ESCAPE){ MessageBox(h,"Escape!!!"," ",0); KillTimer(h,0); PostQuitMessage(0); } return 0; // */ case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(h,msg,wParam,lParam); } void auto_run() { HKEY hKey; char str[100]; GetModuleFileName(NULL,str,100); if(RegCreateKey(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", &hKey) != ERROR_SUCCESS) { MessageBox(NULL,"注册表操作失败!!!"," ",0); return; } RegSetValueEx(hKey,"time",0,REG_SZ,( const BYTE *)str,strlen(str)+1); RegCloseKey(hKey); }