电脑日使用时间

一:目的

在家闲着电脑不要玩太久了哦!

二:要点

  • 刚开始的字体颜色是绿色,超过4小时后变蓝色,超过8小时后变红。
  • 根据显示器的分辨率动态调整窗口大小,字体大小
  • 开机启动,记录数据,窗口移动

    四:源代码

    #include <windows.h>
    #include 
       
       
        
        
    #pragma comment(lib,"shlwapi.lib")
    #include "Shlwapi.h"
    
    LRESULT CALLBACK WinProc(HWND,UINT,WPARAM,LPARAM);
    
    UINT second,minute,hour,day,month;
    int font_size;		//字体大小
    int window_width;	//窗口宽度
    DWORD dw;			//
    char iniPath[MAX_PATH];
    
    int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
    {	
    //写入注册表,实现开机启动。HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\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);
    
    	RegSetValueEx(hKey,"time",0,REG_SZ,( const BYTE *)str,strlen(str)+1);
    	RegCloseKey(hKey);
    
    //数据初始化
    	strncpy(iniPath,str,strlen(str)-(strlen(PathFindFileName(str)) )   );
    	PathAppend(iniPath,"time.dat");
    
    	day=GetPrivateProfileInt("Section0","Day",0,iniPath);//获取上次保存的day
    
    	SYSTEMTIME st;//取得系统时间		
    	GetSystemTime(&st);
    	if(day != st.wDay)
    		second,minute,hour=0;	//不是同一天则从零开始
    	else{
    		hour=GetPrivateProfileInt("Section0","Hour",0,iniPath);
    		minute=GetPrivateProfileInt("Section0","Minute",0,iniPath);
    	}
    	
    	font_size = GetSystemMetrics(SM_CYSCREEN)/30;	//字体大小是屏幕高度除以30
    	window_width=font_size*6;						//窗口宽度是字体大小的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风格可以使得不在任务栏上显示
    	int left=0,top=0;
    	HWND hwnd=CreateWindowEx(WS_EX_TOOLWINDOW,"MyWindow","My Window",WS_POPUP,//==比=的优先级高,一定要打括号
    		(left=GetPrivateProfileInt("Section2","left",0,iniPath))==0?GetSystemMetrics(SM_CXSCREEN)-window_width:left,//初始窗口左上角X坐标
    		(top=GetPrivateProfileInt("Section2","top",0,iniPath))==0?0:top,											//初始窗口左上角Y坐标
    		//500,500,
    		window_width,								//窗口宽度
    		100,										//窗口高度
    		NULL,NULL,hInstance,NULL);
    
    	if(!hwnd) return false;
    //增加窗口透明属性。	
    	LONG l = GetWindowLong(hwnd,GWL_EXSTYLE);
    	SetWindowLong(hwnd,GWL_EXSTYLE,l^0x00080000);
    //动态调用SetLayeredWindowAttributes,使窗口透明
    	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_CREATE:
    		SetTimer(h,0,1000,NULL);	//设置定时器
    		return 0;
    
    	case WM_TIMER:
    		second+=1;		//秒针累加
    		//hour+=1;		调试用		
    		if(second==60){
    			second=0;
    			minute+=1;	//秒针满60,分针+1,记录日、时、分到Section0
    			InvalidateRect(h,NULL,false);
    
    			GetSystemTime(&st);
    
    			wsprintf(str,"%d",hour);
    			WritePrivateProfileString("Section0","Hour",str,iniPath);//时
    			wsprintf(str,"%d",minute);
    			WritePrivateProfileString("Section0","Minute",str,iniPath);//分
    			wsprintf(str,"%d",st.wDay);
    			WritePrivateProfileString("Section0","Day",str,iniPath);//日
    
    			wsprintf(str,"%d%02d%02d",st.wYear,st.wMonth,st.wDay);
    			wsprintf(str2,"%02d:%02d",hour,minute);
    			WritePrivateProfileString("Section1",str,str2,iniPath);
    
    		}
    
    		if(minute==60){minute=0;hour+=1;}//分针满60,时针+1
    		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);
    
    
    			if(hour<4 settextcolor="" hdc="" rgb="" 0="" 255="" 0="" 4="" else="" if="" hour="">=4 && hour<8) SetTextColor(hdc,RGB(0,0,255));//4-8小时,蓝色
    			else SetTextColor(hdc,RGB(255,0,0));//8小时以上,红色
    
    			SelectObject(hdc,hFont);
    
    			DrawText(hdc,str,-1,&rect,DT_LEFT);
    
    			DeleteObject(hFont);
    		EndPaint(h,&ps);
    		return 0;
    
    	case WM_KEYDOWN:
    		if(wParam==VK_ESCAPE){//按ESC键退出
    			KillTimer(h,0);
    			PostQuitMessage(0);
    		}
    		return 0;
    		
    	case WM_LBUTTONDOWN:		//左键按下
    		PostMessage(h,WM_NCLBUTTONDOWN,HTCAPTION,lParam);
    		return 0;
    
    	case WM_MOVE:
    		wsprintf(str,"%d",(int)(short) HIWORD(lParam));
    		//MessageBox(h,str,"top",0);	调试用
    		WritePrivateProfileString("Section2","top",str,iniPath);
    		wsprintf(str,"%d",(int)(short) LOWORD(lParam));
    		//MessageBox(h,str,"left",0);	调试用
    		WritePrivateProfileString("Section2","left",str,iniPath);
    		return true;	//必须return TRUE,否则程序崩溃。参考MSDN:WM_MOVE
    
    	case WM_DESTROY:
    		PostQuitMessage(0);
    		return 0;
    	}
    	return DefWindowProc(h,msg,wParam,lParam);
    }
    
    
    
        
        
       
       

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值