|
无聊弄的一个时钟~
-
- // CppClock.cpp : Defines the entry point for the application.
- //
- #include "stdafx.h"
- #include "resource.h"
- #include <math.h>
- ///////////////////////////////////////////////////////////////////////////////////
- #define GET_X(__r,__d) ((float)cos(__d)*__r )
- #define GET_Y(__r,__d) ((float)sin(__d)*__r )
- #define MAX_LOADSTRING 100
- #define TM_SEC 1
- HINSTANCE hInst;
- HMENU hMenu;
- HBITMAP hClock;
- HBITMAP hMemBmp;
- HDC hMemDc;
- TCHAR szTitle[MAX_LOADSTRING];
- TCHAR szWindowClass[MAX_LOADSTRING];
- const float Sr=55.0f,Mr=45.0f,Hr=30.0f;
- const float PI=3.14159f;
- float Sd=0,Md=0,Hd=0;
- float x=0,y=0;
- unsigned char WTran=128;//窗口透明度
- ///////////////////////////////////////////////////////////////////////////////////
- ATOM MyRegisterClass(HINSTANCE hInstance);
- BOOL InitInstance(HINSTANCE, int);
- LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
- LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
- ///////////////////////////////////////////////////////////////////////////////////
- int APIENTRY WinMain(HINSTANCE hInstance,
- HINSTANCE hPrevInstance,
- LPSTR lpCmdLine,
- int nCmdShow){
- MSG msg;
- HACCEL hAccelTable;
- LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
- LoadString(hInstance, IDC_CPPCLOCK, szWindowClass, MAX_LOADSTRING);
- MyRegisterClass(hInstance);
- if (!InitInstance (hInstance, nCmdShow)){return FALSE;}
- hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_CPPCLOCK);
- while (GetMessage(&msg, NULL, 0, 0)) {
- if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)){
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- }
- return msg.wParam;
- }
- ATOM MyRegisterClass(HINSTANCE hInstance){
- WNDCLASSEX wcex;
- wcex.cbSize = sizeof(WNDCLASSEX);
- wcex.style = CS_HREDRAW | CS_VREDRAW;
- wcex.lpfnWndProc = (WNDPROC)WndProc;
- wcex.cbClsExtra = 0;
- wcex.cbWndExtra = 0;
- wcex.hInstance = hInstance;
- wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_CPPCLOCK);
- wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
- wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
- wcex.lpszMenuName = NULL;
- wcex.lpszClassName = szWindowClass;
- wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
- return RegisterClassEx(&wcex);
- }
- BOOL InitInstance(HINSTANCE hInstance, int nCmdShow){
- HWND hWnd;
- hInst = hInstance;
- hWnd = CreateWindowEx(WS_EX_TOOLWINDOW,szWindowClass, szTitle, WS_POPUP|WS_SYSMENU,
- 300,300,150,150, NULL, NULL, hInstance, NULL);
- if (!hWnd) {
- return FALSE;
- }
- ShowWindow(hWnd, nCmdShow);
- UpdateWindow(hWnd);
- return TRUE;
- }
- LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
- int wmId, wmEvent,width=150,height=150;
- PAINTSTRUCT ps;
- HDC hdc;
- TCHAR szHello[MAX_LOADSTRING];
- LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
- switch (message)
- {
- case WM_COMMAND:
- wmId = LOWORD(wParam);
- wmEvent = HIWORD(wParam);
- switch (wmId)
- {
- case IDM_MENU:
- DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
- break;
- case IDM_ABOUT:
- MessageBox(NULL,"Windows Clock\n"
- "Author:shly\nQQ:378366979\n"
- "E-mail:[email protected]\n"
- ,"关于...",MB_OK);
- break;
- case IDM_EXIT:
- SendMessage(hWnd,WM_DESTROY,0,0);
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- break;
- ///////////////////////////////////////////////////////////////////////////////////
- case WM_TIMER:
- switch(wParam){
- case TM_SEC:{
- SYSTEMTIME st;
- GetLocalTime(&st);
- Hd=2*PI/12*(st.wHour-3+st.wMinute/60.0f);
- Md=2*PI/60*(st.wMinute-15);
- Sd=2*PI/60*(st.wSecond-15);
- }
- InvalidateRect(hWnd,NULL,FALSE);
- break;
- default:break;
- }
- break;
- ///////////////////////////////////////////////////////////////////////////////////
- case WM_PAINT:
- hdc = BeginPaint(hWnd, &ps);
- ///////////////////////////////////////////////////////////////////////////////////
- {
- //画 表盘
- HDC _cdc=CreateCompatibleDC(hdc);
- SelectObject(_cdc,hClock);
- BitBlt(hMemDc,0,0,width,height,_cdc,0,0,SRCCOPY);
- DeleteDC(_cdc);
- //画 时 分 秒针
- HPEN _hPen1=CreatePen(PS_SOLID,1,RGB(255,0,0));
- SelectObject(hMemDc,_hPen1);
- MoveToEx(hMemDc,(int)x,(int)y,NULL);
- LineTo(hMemDc,(int)(x+GET_X(Sr,Sd)),(int)(y+GET_Y(Sr,Sd)));
- HPEN _hPen2=CreatePen(PS_SOLID,2,RGB(150,255,150));
- SelectObject(hMemDc,_hPen2);
- MoveToEx(hMemDc,(int)x,(int)y,NULL);
- LineTo(hMemDc,(int)(x+GET_X(Mr,Md)),(int)(y+GET_Y(Mr,Md)));
- HPEN _hPen3=CreatePen(PS_SOLID,3,RGB(150,150,255));
- HPEN _oldPen=(HPEN)SelectObject(hMemDc,_hPen3);
- MoveToEx(hMemDc,(int)x,(int)y,NULL);
- LineTo(hMemDc,(int)(x+GET_X(Hr,Hd)),(int)(y+GET_Y(Hr,Hd)));
- SelectObject(hMemDc,_oldPen);
- DeleteObject(_hPen1);
- DeleteObject(_hPen2);
- DeleteObject(_hPen3);
- BitBlt(hdc,0,0,width,height,hMemDc,0,0,SRCCOPY);
- }
- ///////////////////////////////////////////////////////////////////////////////////
- EndPaint(hWnd, &ps);
- break;
- ///////////////////////////////////////////////////////////////////////////////////
- case WM_RBUTTONDOWN:{//右键弹出菜单
- POINT _p;
- GetCursorPos(&_p);
- TrackPopupMenu(hMenu,TPM_LEFTALIGN|TPM_TOPALIGN,_p.x,_p.y,0,hWnd,NULL);
- }
- break;
- case WM_LBUTTONDOWN://移动窗口
- UpdateWindow(hWnd);
- SendMessage(hWnd,WM_NCLBUTTONDOWN,HTCAPTION,0);
- break;
- case WM_CREATE:{//初始化
- //获取客户区宽度高度
- RECT _r;GetClientRect(hWnd,&_r);
- width=_r.right-_r.left;
- height=_r.bottom-_r.top;
- x=(float)width/2;y=(float)height/2;
- //加载获取右键菜单
- hMenu=LoadMenu(hInst,MAKEINTRESOURCE(IDC_CPPCLOCK));
- hMenu=GetSubMenu(hMenu,0);
- hClock=LoadBitmap(hInst,MAKEINTRESOURCE(IDB_CLOCK));
- //创建内存DC
- HDC _hdc=GetDC(hWnd);
- hMemDc=CreateCompatibleDC(_hdc);
- hMemBmp=CreateCompatibleBitmap(_hdc,width,height);
- SelectObject(hMemDc,hMemBmp);
- ReleaseDC(hWnd,_hdc);
- //创建圆形窗口
- HRGN _hRgn=CreateEllipticRgn(-1,-1,width+2,height+2);
- SetWindowRgn(hWnd,_hRgn,TRUE);
- DeleteObject(_hRgn);
- //设置为最顶端
- SetWindowPos(hWnd,HWND_TOPMOST,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);
- //开启计时器 初始数据
- SetTimer(hWnd,TM_SEC,1000,0);
- SYSTEMTIME st;
- GetLocalTime(&st);
- Hd=2*PI/12*(st.wHour-3+st.wMinute/60.0f);
- Md=2*PI/60*(st.wMinute-15);
- Sd=2*PI/60*(st.wSecond-15);
- //设置窗口透明
- HINSTANCE _hinst=(HINSTANCE)LoadLibrary("user32.dll");
- if(_hinst){
- typedef BOOL(WINAPI *pFun)(HWND,COLORREF,BYTE,DWORD);
- pFun fun = NULL;
- fun=(pFun)GetProcAddress(_hinst,"SetLayeredWindowAttributes");
- SetWindowLong(hWnd,GWL_EXSTYLE,GetWindowLong(hWnd,GWL_EXSTYLE)|0x80000);
- if(fun)fun(hWnd,0,WTran,0x2);
- FreeLibrary(_hinst);
- }
- }
- break;
- ///////////////////////////////////////////////////////////////////////////////////
- case WM_DESTROY:
- DeleteObject(hClock);
- DeleteObject(hMemBmp);
- DeleteDC(hMemDc);
- PostQuitMessage(0);
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- return 0;
- }
- LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam){
- switch (message){
- case WM_INITDIALOG:
- return TRUE;
- case WM_COMMAND:
- if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) {
- EndDialog(hDlg, LOWORD(wParam));
- return TRUE;
- }
- break;
- }
- return FALSE;
- }
复制代码 |
|