好玩的Windows编程之"本地时间"(1)binaryclock

好玩的Windows编程之"本地时间"(1)binaryclock好玩的Windows编程之"本地时间"(1)binaryclock


(源码下载页面)

 

 

 

//程序名:方块时钟/binaryclock
//作者wisepragma
//主页:http://blog.csdn.net/wisepragma
//taskkill /im binaryclock.exe 
//cl binaryclock.cpp
//注释掉#define LENGTH 15然后编译时指定宏/DLENGTH=15
//CL binaryclock.CPP /DLENGTH=15
#include <windows.h>
#include <time.h>

#pragma comment(lib,"user32.lib")
#pragma comment(lib,"gdi32.lib")
#pragma comment(lib,"kernel32.lib")
//////////////////////////////////////////

#define LENGTH 15//20//10//100//方块边长
#define DX 1//方块间横向间隔
#define DY 1//方块间纵向间隔

int cwidth=LENGTH*6+1+DX*5;//窗体的宽
int cheight=LENGTH*4+1+DY*3;//窗体的高
 

//////////////////////////////////////////
struct XTIME
{
	int hour,minute,second;//时分秒
};
struct QUAD
{ 
   RGBTRIPLE 	m_color;//方块色彩
   bool 		m_bPaint;//是否显示
};

QUAD quad[6][4];//定义时间方格

//////////////////////////////////////////

void rand_color(void)//随机色彩
{ 
  for(int m=0;m<6;m++)
   {
      for(int n=0;n<4;n++)
       {
			quad[m][n].m_color.rgbtRed=rand()%255;
			quad[m][n].m_color.rgbtGreen=rand()%255;
			quad[m][n].m_color.rgbtBlue=rand()%255;
       }
   }
}
void rand_color_hour(void)//随机色彩
{ 
  for(int m=0;m<=1;m++)
   {
      for(int n=0;n<4;n++)
       {
			quad[m][n].m_color.rgbtRed=rand()%255;
			quad[m][n].m_color.rgbtGreen=rand()%255;
			quad[m][n].m_color.rgbtBlue=rand()%255;
       }
   }
}
void rand_color_minute(void)//随机色彩
{ 
  for(int m=2;m<=3;m++)
   {
      for(int n=0;n<4;n++)
       {
			quad[m][n].m_color.rgbtRed=rand()%255;
			quad[m][n].m_color.rgbtGreen=rand()%255;
			quad[m][n].m_color.rgbtBlue=rand()%255;
       }
   }
}
void rand_color_second(void)//随机色彩
{ 
  for(int m=4;m<=5;m++)
   {
      for(int n=0;n<4;n++)
       {
			quad[m][n].m_color.rgbtRed=rand()%255;
			quad[m][n].m_color.rgbtGreen=rand()%255;
			quad[m][n].m_color.rgbtBlue=rand()%255;
       }
   }
}


LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,PSTR szCmdLine, int iCmdShow)
{
	
		HANDLE hMutex=CreateMutex(NULL,FALSE,TEXT("Only One Instance"));
		if(  hMutex==NULL ||ERROR_ALREADY_EXISTS==GetLastError()  )  return 0;
	
	static char  *szAppName = TEXT("binaryclock");
	MSG          msg;
	HWND       hwnd;
	WNDCLASSEX   wndclass;

 	wndclass.cbSize        = sizeof (wndclass);
	wndclass.style         = CS_HREDRAW | CS_VREDRAW;
	wndclass.lpfnWndProc   = WndProc;
	wndclass.cbClsExtra    = 0;
	wndclass.cbWndExtra    = 0;
	wndclass.hInstance     = hInstance;
	wndclass.hIcon         = LoadIcon (NULL,LPCTSTR(100));
	wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW);
	wndclass.hbrBackground =(HBRUSH)CreateSolidBrush(RGB(255,255,255));
	wndclass.lpszMenuName  = 0;
	wndclass.lpszClassName = szAppName;
	wndclass.hIconSm       = wndclass.hIcon;
	RegisterClassEx (&wndclass);
	hwnd = CreateWindowEx (WS_EX_TOPMOST|WS_EX_TOOLWINDOW,
							szAppName,szAppName,WS_POPUPWINDOW,
							GetSystemMetrics(SM_CXSCREEN)-cwidth-10, 
							GetSystemMetrics(SM_CYSCREEN)-cheight-40,cwidth, cheight,
							NULL, NULL, hInstance, NULL);
	
	ShowWindow (hwnd,SW_SHOWNORMAL);
	UpdateWindow (hwnd);
	while (GetMessage (&msg, NULL, 0, 0))
		{
			TranslateMessage (&msg);
			DispatchMessage (&msg);
		}
	return msg.wParam;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{ 
    static XTIME old_time,t0,t10;//old_time前一次时间,t0时间的个位,t10时间的十位
    static SYSTEMTIME current_time;
    static HBRUSH hb;
    static HPEN hp;
    static HDC hdc;
    
	switch (uMsg)
	{
		case WM_CREATE :
		    {
				srand( (unsigned)time( NULL ) );
				memset(&quad,1,sizeof(quad));
				memset(&curren;t_time,0,sizeof(current_time));
				rand_color();
				SetTimer(hwnd,WM_NULL,100,NULL);
				GetLocalTime(&curren;t_time);
				return 0;
			}
		case WM_TIMER://SetTimer()定时每隔100毫秒更新一次时间
			{
			//old_time.second,old_time.minute,old_time.hour是旧的时间
			 old_time.second=current_time.wSecond;
			 old_time.minute=current_time.wMinute;
			 old_time.hour=current_time.wHour;
			 GetLocalTime(&curren;t_time);//取得当前最新时间
 			 if(old_time.minute!=current_time.wMinute)rand_color_minute();//分钟变化时改变分钟颜色
			 if(old_time.hour!=current_time.wHour)rand_color_hour();//小时变化时改变小时颜色
			 if( (old_time.second/10)!=(current_time.wSecond/10) )rand_color_second();//十秒钟变化时改变秒钟颜色
			 //s,m,h,ss,mm,hh,用于取得数值的模填入quad的m_bPaint中,为1者显示,为0者不显示
			  t0.second=current_time.wSecond%10;	t10.second=current_time.wSecond/10;//秒钟:求余取得个位数字t0.second,求商取得十位字t10.second,下同
			  t0.minute=current_time.wMinute%10;	t10.minute=current_time.wMinute/10;
			  t0.hour=current_time.wHour%10;		t10.hour=current_time.wHour/10;
			 for(int n=0;n<4;n++)//按时间的数值,求与依次填入框格中
				 {
				  quad[5][3-n].m_bPaint=(t0.second>>n)&1;
				  quad[4][3-n].m_bPaint=(t10.second>>n)&1;
				  quad[3][3-n].m_bPaint=(t0.minute>>n)&1;
				  quad[2][3-n].m_bPaint=(t10.minute>>n)&1;
				  quad[1][3-n].m_bPaint=(t0.hour>>n)&1;			
				  quad[0][3-n].m_bPaint=(t10.hour>>n)&1;
				 }
			  InvalidateRect(hwnd,NULL,0);//立即刷新整个窗口,即触发WM_PAINT	消息
			  break;
			}
		case WM_PAINT:
			{
			 hdc=GetDC(hwnd);
			 for(int m=0;m<6;m++)
			   {
			     for(int n=0;n<4;n++)
			      {
			        if(quad[m][n].m_bPaint)//位为1的用彩色画刷
					 {
					   BYTE r,g,b;
					   r=quad[m][n].m_color.rgbtRed;
					   g=quad[m][n].m_color.rgbtGreen;
					   b=quad[m][n].m_color.rgbtBlue;
					   hb=CreateSolidBrush(RGB(r,g,b));
					   hp=CreatePen(PS_INSIDEFRAME,1,RGB(0,0,0));
					 }
					 else {//位为0的用白色画刷
						hb=(HBRUSH)GetStockObject(WHITE_BRUSH);
						//hp=CreatePen(PS_INSIDEFRAME,1,RGB(0,0,0));//黑
						//hp=CreatePen(PS_INSIDEFRAME,1,RGB(255,255,255));//白
						hp=CreatePen(PS_INSIDEFRAME,1,RGB(215,215,215));//灰色
				     }
					SelectObject(hdc,hp);
					SelectObject(hdc,hb);
					Rectangle(hdc,m*(LENGTH+DX),n*(LENGTH+DY),m*(LENGTH+DX)+LENGTH,n*(LENGTH+DY)+LENGTH);
					DeleteObject(hp);
					DeleteObject(hb);
			      }
			   }
			 ReleaseDC(hwnd,hdc);
			 break;
			}
		case WM_LBUTTONDOWN://拖拽窗口
			SendMessage(hwnd,WM_NCLBUTTONDOWN,HTCAPTION,lParam);
		return 0;
		case WM_COMMAND :
			break;
		case WM_KEYDOWN:
		         if(wParam==VK_ESCAPE) PostQuitMessage (0); //ESC键退出
				 break;
		case WM_DESTROY:
			PostQuitMessage (0);
			return 0;
	}
	return DefWindowProc (hwnd, uMsg, wParam, lParam);
}


你可能感兴趣的:(结构体,Windows编程)