我的数字钟0.0
程序代码:
#include <windows.h> #include <stdio.h> #define IDT_TIMER 1 #define IDM_TOPMOST 0x1000 #define HEIGHT 59 // 每个数字的高度 #define WIDTH 35 // 每个数字的宽度 #define MAX_LOADSTRING 36 #define IDT_TIMER 1 #define IDM_TOPMOST 0x1000 #define IDM_ALERT 0X1001 #define IDM_CLOSE 0X1002 #define HEIGHT 59 // 每个数字的高度 #define WIDTH 35 BOOL bTopMost = TRUE; bool init_win(); HWND create_win(); void show_win(); void run_win(); void display_time(); void chk(); HBITMAP bg; HBITMAP bitmap; HBITMAP clock_1,clock_2; char buf[10]; HDC hdc; HDC mdc; HDC bufdc; HDC bufdc1; HDC bufdc2; int dx=0; SYSTEMTIME time; char *name="xiaoyuer"; HINSTANCE hinst; HWND hwnd; WNDCLASS wnd; int x=0,y=0; LRESULT CALLBACK WinProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam); int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd ) {hinst=hInstance; if(init_win()) { if(hwnd=create_win()) { hdc=GetDC(hwnd); mdc=CreateCompatibleDC(hdc); bufdc=CreateCompatibleDC(hdc); bitmap=CreateCompatibleBitmap(hdc,35*8,59); SelectObject(mdc,bitmap); bg=(HBITMAP)LoadImage(hinst,"bg.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE); clock_1=(HBITMAP)LoadImage(hinst,"time_bg.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE); clock_2=(HBITMAP)LoadImage(hinst,"time.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE); show_win(); run_win(); return true; } } return false; } bool init_win() {memset(&wnd,0,sizeof(wnd)); wnd.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH); wnd.hInstance=hinst; wnd.lpfnWndProc=WinProc; wnd.lpszClassName=name; if(RegisterClass(&wnd)) return true; else return false; } HWND create_win() { return CreateWindow(name,name, WS_SYSMENU|WS_POPUP,0, 0, 35*8, 59, NULL, NULL, hinst, NULL); } void show_win() { ShowWindow(hwnd,SW_SHOW); UpdateWindow(hwnd); } void run_win() {MSG msg; while(GetMessage(&msg,0,0,0 )) { TranslateMessage(&msg); DispatchMessage(&msg); } } LRESULT CALLBACK WinProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam) {int wmId; HMENU hSysMenu; HDC hdc; PAINTSTRUCT ps; switch(msg) {case WM_CLOSE: KillTimer(hwnd,1); DeleteObject(bg); DeleteObject(clock_1); DeleteObject(clock_2); DestroyWindow(hwnd); break; case WM_DESTROY: PostQuitMessage(0); break; case WM_TIMER: hdc=GetDC(hwnd); chk(); ReleaseDC(hwnd, hdc); break; case WM_CREATE: if( SetTimer(hwnd,IDT_TIMER,1000,NULL) == 0 ) { MessageBox(hwnd,TEXT("安装定时器失败"), TEXT("错误"),MB_OK | MB_ICONHAND); exit(-1); } hSysMenu = GetSystemMenu(hwnd, FALSE); DeleteMenu(hSysMenu, 0, MF_BYPOSITION); DeleteMenu(hSysMenu, 1, MF_BYPOSITION); DeleteMenu(hSysMenu, 1, MF_BYPOSITION); DeleteMenu(hSysMenu, 1, MF_BYPOSITION); DeleteMenu(hSysMenu, 1, MF_BYPOSITION); AppendMenu(hSysMenu, MF_SEPARATOR, 0, NULL); AppendMenu(hSysMenu, MF_STRING, IDM_TOPMOST, TEXT("总在最前")); AppendMenu(hSysMenu,MF_STRING,IDM_ALERT,"提醒"); AppendMenu(hSysMenu, MF_SEPARATOR, 0, NULL); AppendMenu(hSysMenu,MF_STRING,IDM_CLOSE,"定时关机"); EnableMenuItem(hSysMenu,IDM_TOPMOST, MF_BYCOMMAND | MF_ENABLED); SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOREDRAW | SWP_NOSIZE); CheckMenuItem(hSysMenu, IDM_TOPMOST, MF_BYCOMMAND | MF_CHECKED); break; case WM_PAINT:hdc=BeginPaint(hwnd,&ps); chk(); EndPaint(hwnd,&ps); break; case WM_NCHITTEST: { UINT nHitTest; nHitTest = DefWindowProc(hwnd, WM_NCHITTEST, wparam, lparam); if(nHitTest == HTCLIENT && GetAsyncKeyState(MK_LBUTTON) < 0) nHitTest = HTCAPTION; return nHitTest; } case WM_ERASEBKGND: // 防止开始绘制时的闪烁 return TRUE; case WM_SYSCOMMAND: wmId = wparam; { if(wmId == IDM_TOPMOST) { hSysMenu = GetSystemMenu(hwnd, FALSE); if(bTopMost) { CheckMenuItem(hSysMenu, IDM_TOPMOST, MF_UNCHECKED); SetWindowPos(hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOREDRAW | SWP_NOSIZE); bTopMost = FALSE; } else { CheckMenuItem(hSysMenu, IDM_TOPMOST, MF_CHECKED); SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOREDRAW | SWP_NOSIZE); bTopMost = TRUE; } } return DefWindowProc(hwnd, WM_SYSCOMMAND, wmId, 0); } case WM_KEYDOWN: switch(wparam) { case VK_ESCAPE: SendMessage(hwnd,WM_CLOSE,0,0); break; default:break; } break; case WM_CONTEXTMENU: POINT pt; pt.x = LOWORD(lparam); pt.y = HIWORD(lparam); hSysMenu = GetSystemMenu(hwnd, FALSE); wmId =TrackPopupMenu(hSysMenu, TPM_LEFTALIGN | TPM_RETURNCMD, pt.x, pt.y, 0, hwnd, NULL); if(wmId>0) SendMessage(hwnd, WM_SYSCOMMAND, wmId, 0); return 0; default:return DefWindowProc(hwnd,msg,wparam,lparam); } return 0; } void display_time(int x) { if(dx==2) dx++; if(dx==5) dx++; SelectObject(bufdc,clock_1); BitBlt(mdc,dx*35,0,35,59,bufdc,x*35,0,SRCAND); SelectObject(bufdc,clock_2); BitBlt(mdc,dx*35,0,35,59,bufdc,x*35,0,SRCPAINT); dx+=1; if(dx==8) dx=0; } void chk() { int x; GetLocalTime(&time); SelectObject(mdc,bitmap); SelectObject(bufdc,bg); BitBlt(mdc,0,0,35*8,59,bufdc,0,0,SRCCOPY); x=time.wHour/10; display_time(x); x=time.wHour%10; display_time(x); SelectObject(bufdc,clock_1); BitBlt(mdc,2*35,0,35,59,bufdc,10*35,0,SRCAND); SelectObject(bufdc,clock_2); BitBlt(mdc,2*35,0,35,59,bufdc,10*35,0,SRCPAINT); x=time.wMinute/10; display_time(x); x=time.wMinute%10; display_time(x); SelectObject(bufdc,clock_1); BitBlt(mdc,5*35,0,35,59,bufdc,10*35,0,SRCAND); SelectObject(bufdc,clock_2); BitBlt(mdc,5*35,0,35,59,bufdc,10*35,0,SRCPAINT); x=time.wSecond/10; display_time(x); x=time.wSecond%10; display_time(x); BitBlt(hdc,0,0,8*35,59,mdc,0,0,SRCCOPY); }