找回密码
 点一下
查看: 908|回复: 3

无聊扔个源码到此.

[复制链接]
发表于 2009-6-25 03:14:36 | 显示全部楼层 |阅读模式
无聊弄的一个时钟~

QQ截图未命名.jpg
  1. // CppClock.cpp : Defines the entry point for the application.
  2. //
  3. #include "stdafx.h"
  4. #include "resource.h"
  5. #include <math.h>
  6. ///////////////////////////////////////////////////////////////////////////////////
  7. #define GET_X(__r,__d) ((float)cos(__d)*__r )
  8. #define GET_Y(__r,__d) ((float)sin(__d)*__r )
  9. #define MAX_LOADSTRING 100
  10. #define TM_SEC 1
  11. HINSTANCE hInst;
  12. HMENU hMenu;
  13. HBITMAP hClock;
  14. HBITMAP hMemBmp;
  15. HDC hMemDc;
  16. TCHAR szTitle[MAX_LOADSTRING];
  17. TCHAR szWindowClass[MAX_LOADSTRING];
  18. const float Sr=55.0f,Mr=45.0f,Hr=30.0f;
  19. const float PI=3.14159f;
  20. float Sd=0,Md=0,Hd=0;
  21. float x=0,y=0;
  22. unsigned char WTran=128;//窗口透明度
  23. ///////////////////////////////////////////////////////////////////////////////////
  24. ATOM MyRegisterClass(HINSTANCE hInstance);
  25. BOOL InitInstance(HINSTANCE, int);
  26. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  27. LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
  28. ///////////////////////////////////////////////////////////////////////////////////
  29. int APIENTRY WinMain(HINSTANCE hInstance,
  30. HINSTANCE hPrevInstance,
  31. LPSTR lpCmdLine,
  32. int nCmdShow){
  33. MSG msg;
  34. HACCEL hAccelTable;
  35. LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
  36. LoadString(hInstance, IDC_CPPCLOCK, szWindowClass, MAX_LOADSTRING);
  37. MyRegisterClass(hInstance);
  38. if (!InitInstance (hInstance, nCmdShow)){return FALSE;}
  39. hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_CPPCLOCK);
  40. while (GetMessage(&msg, NULL, 0, 0)) {
  41. if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)){
  42. TranslateMessage(&msg);
  43. DispatchMessage(&msg);
  44. }
  45. }
  46. return msg.wParam;
  47. }
  48. ATOM MyRegisterClass(HINSTANCE hInstance){
  49. WNDCLASSEX wcex;
  50. wcex.cbSize = sizeof(WNDCLASSEX);
  51. wcex.style = CS_HREDRAW | CS_VREDRAW;
  52. wcex.lpfnWndProc = (WNDPROC)WndProc;
  53. wcex.cbClsExtra = 0;
  54. wcex.cbWndExtra = 0;
  55. wcex.hInstance = hInstance;
  56. wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_CPPCLOCK);
  57. wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
  58. wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  59. wcex.lpszMenuName = NULL;
  60. wcex.lpszClassName = szWindowClass;
  61. wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
  62. return RegisterClassEx(&wcex);
  63. }
  64. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow){
  65. HWND hWnd;
  66. hInst = hInstance;
  67. hWnd = CreateWindowEx(WS_EX_TOOLWINDOW,szWindowClass, szTitle, WS_POPUP|WS_SYSMENU,
  68. 300,300,150,150, NULL, NULL, hInstance, NULL);
  69. if (!hWnd) {
  70. return FALSE;
  71. }
  72. ShowWindow(hWnd, nCmdShow);
  73. UpdateWindow(hWnd);
  74. return TRUE;
  75. }
  76. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
  77. int wmId, wmEvent,width=150,height=150;
  78. PAINTSTRUCT ps;
  79. HDC hdc;
  80. TCHAR szHello[MAX_LOADSTRING];
  81. LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
  82. switch (message)
  83. {
  84. case WM_COMMAND:
  85. wmId = LOWORD(wParam);
  86. wmEvent = HIWORD(wParam);
  87. switch (wmId)
  88. {
  89. case IDM_MENU:
  90. DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
  91. break;
  92. case IDM_ABOUT:
  93. MessageBox(NULL,"Windows Clock\n"
  94. "Author:shly\nQQ:378366979\n"
  95. "E-mail:[email protected]\n"
  96. ,"关于...",MB_OK);
  97. break;
  98. case IDM_EXIT:
  99. SendMessage(hWnd,WM_DESTROY,0,0);
  100. break;
  101. default:
  102. return DefWindowProc(hWnd, message, wParam, lParam);
  103. }
  104. break;
  105. ///////////////////////////////////////////////////////////////////////////////////
  106. case WM_TIMER:
  107. switch(wParam){
  108. case TM_SEC:{
  109. SYSTEMTIME st;
  110. GetLocalTime(&st);
  111. Hd=2*PI/12*(st.wHour-3+st.wMinute/60.0f);
  112. Md=2*PI/60*(st.wMinute-15);
  113. Sd=2*PI/60*(st.wSecond-15);
  114. }
  115. InvalidateRect(hWnd,NULL,FALSE);
  116. break;
  117. default:break;
  118. }
  119. break;
  120. ///////////////////////////////////////////////////////////////////////////////////
  121. case WM_PAINT:
  122. hdc = BeginPaint(hWnd, &ps);
  123. ///////////////////////////////////////////////////////////////////////////////////
  124. {
  125. //画 表盘
  126. HDC _cdc=CreateCompatibleDC(hdc);
  127. SelectObject(_cdc,hClock);
  128. BitBlt(hMemDc,0,0,width,height,_cdc,0,0,SRCCOPY);
  129. DeleteDC(_cdc);
  130. //画 时 分 秒针
  131. HPEN _hPen1=CreatePen(PS_SOLID,1,RGB(255,0,0));
  132. SelectObject(hMemDc,_hPen1);
  133. MoveToEx(hMemDc,(int)x,(int)y,NULL);
  134. LineTo(hMemDc,(int)(x+GET_X(Sr,Sd)),(int)(y+GET_Y(Sr,Sd)));
  135. HPEN _hPen2=CreatePen(PS_SOLID,2,RGB(150,255,150));
  136. SelectObject(hMemDc,_hPen2);
  137. MoveToEx(hMemDc,(int)x,(int)y,NULL);
  138. LineTo(hMemDc,(int)(x+GET_X(Mr,Md)),(int)(y+GET_Y(Mr,Md)));
  139. HPEN _hPen3=CreatePen(PS_SOLID,3,RGB(150,150,255));
  140. HPEN _oldPen=(HPEN)SelectObject(hMemDc,_hPen3);
  141. MoveToEx(hMemDc,(int)x,(int)y,NULL);
  142. LineTo(hMemDc,(int)(x+GET_X(Hr,Hd)),(int)(y+GET_Y(Hr,Hd)));
  143. SelectObject(hMemDc,_oldPen);
  144. DeleteObject(_hPen1);
  145. DeleteObject(_hPen2);
  146. DeleteObject(_hPen3);
  147. BitBlt(hdc,0,0,width,height,hMemDc,0,0,SRCCOPY);
  148. }
  149. ///////////////////////////////////////////////////////////////////////////////////
  150. EndPaint(hWnd, &ps);
  151. break;
  152. ///////////////////////////////////////////////////////////////////////////////////
  153. case WM_RBUTTONDOWN:{//右键弹出菜单
  154. POINT _p;
  155. GetCursorPos(&_p);
  156. TrackPopupMenu(hMenu,TPM_LEFTALIGN|TPM_TOPALIGN,_p.x,_p.y,0,hWnd,NULL);
  157. }
  158. break;
  159. case WM_LBUTTONDOWN://移动窗口
  160. UpdateWindow(hWnd);
  161. SendMessage(hWnd,WM_NCLBUTTONDOWN,HTCAPTION,0);
  162. break;
  163. case WM_CREATE:{//初始化
  164. //获取客户区宽度高度
  165. RECT _r;GetClientRect(hWnd,&_r);
  166. width=_r.right-_r.left;
  167. height=_r.bottom-_r.top;
  168. x=(float)width/2;y=(float)height/2;
  169. //加载获取右键菜单
  170. hMenu=LoadMenu(hInst,MAKEINTRESOURCE(IDC_CPPCLOCK));
  171. hMenu=GetSubMenu(hMenu,0);
  172. hClock=LoadBitmap(hInst,MAKEINTRESOURCE(IDB_CLOCK));
  173. //创建内存DC
  174. HDC _hdc=GetDC(hWnd);
  175. hMemDc=CreateCompatibleDC(_hdc);
  176. hMemBmp=CreateCompatibleBitmap(_hdc,width,height);
  177. SelectObject(hMemDc,hMemBmp);
  178. ReleaseDC(hWnd,_hdc);
  179. //创建圆形窗口
  180. HRGN _hRgn=CreateEllipticRgn(-1,-1,width+2,height+2);
  181. SetWindowRgn(hWnd,_hRgn,TRUE);
  182. DeleteObject(_hRgn);
  183. //设置为最顶端
  184. SetWindowPos(hWnd,HWND_TOPMOST,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);
  185. //开启计时器 初始数据
  186. SetTimer(hWnd,TM_SEC,1000,0);
  187. SYSTEMTIME st;
  188. GetLocalTime(&st);
  189. Hd=2*PI/12*(st.wHour-3+st.wMinute/60.0f);
  190. Md=2*PI/60*(st.wMinute-15);
  191. Sd=2*PI/60*(st.wSecond-15);
  192. //设置窗口透明
  193. HINSTANCE _hinst=(HINSTANCE)LoadLibrary("user32.dll");
  194. if(_hinst){
  195. typedef BOOL(WINAPI *pFun)(HWND,COLORREF,BYTE,DWORD);
  196. pFun fun = NULL;
  197. fun=(pFun)GetProcAddress(_hinst,"SetLayeredWindowAttributes");
  198. SetWindowLong(hWnd,GWL_EXSTYLE,GetWindowLong(hWnd,GWL_EXSTYLE)|0x80000);
  199. if(fun)fun(hWnd,0,WTran,0x2);
  200. FreeLibrary(_hinst);
  201. }
  202. }
  203. break;
  204. ///////////////////////////////////////////////////////////////////////////////////
  205. case WM_DESTROY:
  206. DeleteObject(hClock);
  207. DeleteObject(hMemBmp);
  208. DeleteDC(hMemDc);
  209. PostQuitMessage(0);
  210. break;
  211. default:
  212. return DefWindowProc(hWnd, message, wParam, lParam);
  213. }
  214. return 0;
  215. }
  216. LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam){
  217. switch (message){
  218. case WM_INITDIALOG:
  219. return TRUE;
  220. case WM_COMMAND:
  221. if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) {
  222. EndDialog(hDlg, LOWORD(wParam));
  223. return TRUE;
  224. }
  225. break;
  226. }
  227. return FALSE;
  228. }
复制代码

clock.rar

189 KB, 下载次数: 16

发表于 2009-6-25 03:29:05 | 显示全部楼层
大人早~
开始中间以为是头目最爱的灰原……
回复

使用道具 举报

 楼主| 发表于 2009-6-25 12:38:23 | 显示全部楼层
来源于这个图片:
093108.jpg
回复

使用道具 举报

阿里山慝少 该用户已被删除
发表于 2009-6-25 12:46:30 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 点一下

本版积分规则

Archiver|移动端|小黑屋|地精研究院

GMT+8, 2024-12-22 16:28 , Processed in 0.062064 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表