面白い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);
}