(源碼下載頁面)
html
//程序名:方塊時鐘/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(¤t_time,0,sizeof(current_time)); rand_color(); SetTimer(hwnd,WM_NULL,100,NULL); GetLocalTime(¤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(¤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); }