Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.19 KB | None | 0 0
  1. #include <windows.h>
  2. #include "resource.h"
  3. #include "CommCtrl.h"
  4. #include "CommDlg.h"
  5. #include <vector>
  6. #define ID_STATUS 300
  7. LRESULT CALLBACK WndProc(HWND hwnd, UINT messg, WPARAM wParam, LPARAM lParam);
  8. BOOL CALLBACK SettingProc(HWND hdlg, UINT messg, WPARAM wParam, LPARAM lParam);
  9. void Draw_all_Ellipses(int x,int y, int n);
  10. void Draw_Ellipses_and_Find_Points(int x,int y, int n);
  11. bool first = true,step = true,fst = true;
  12. int count = 1;
  13. std::vector<POINT> vec;
  14. std::vector<POINT>::iterator it;
  15. HPEN hpen;
  16. HBRUSH hbrush;
  17. HDC hdc;
  18. DWORD dColors[3]={0};
  19. char file[80] = "";
  20. COLORREF color=RGB(0,0,0);
  21. PAINTSTRUCT ps;
  22. static HINSTANCE hInst;
  23. HWND hWnd, hStatus;
  24. int be = 41;
  25. int r = 0;
  26. int n = 0;
  27. int x = 400;
  28. int y = 380;
  29.  
  30. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
  31. {
  32. MSG lpMsg;
  33. WNDCLASS w;
  34. hInst = hInstance;
  35. w.lpszClassName = "Моё окно";
  36. w.hInstance = hInstance;
  37. w.lpfnWndProc = WndProc;
  38. w.hCursor = LoadCursor(NULL, IDC_ARROW);
  39. w.hIcon = LoadIcon(NULL,"IDI_ICON1");
  40. w.lpszMenuName = "IDR_MENU1";
  41. w.hbrBackground = CreateSolidBrush(RGB(250,250,250));
  42. w.style = CS_HREDRAW|CS_VREDRAW;
  43. w.cbClsExtra = 0;
  44. w.cbWndExtra = 0;
  45. if(!RegisterClass(&w))
  46. return 0;
  47. hWnd=CreateWindow("Моё окно",
  48. "Треугольный секундомер",
  49. WS_OVERLAPPEDWINDOW,
  50. 250,
  51. 100,
  52. 800,
  53. 500,
  54. (HWND)NULL,
  55. (HMENU)NULL,
  56. (HINSTANCE)hInstance,
  57. (HINSTANCE)NULL);
  58. ShowWindow(hWnd,nCmdShow);
  59. UpdateWindow(hWnd);
  60. hStatus = CreateStatusWindow(WS_CHILD|WS_VISIBLE, "Количество строк: 0, Радиус: 0, Цвет: 0,0,0\n", hWnd, ID_STATUS);
  61. ShowWindow(hStatus,nCmdShow);
  62. UpdateWindow(hStatus);
  63. while(GetMessage(&lpMsg, NULL, 0, 0))
  64. {
  65. TranslateMessage(&lpMsg);
  66. DispatchMessage(&lpMsg);
  67. }
  68. return(lpMsg.wParam);
  69. }
  70.  
  71. LRESULT CALLBACK WndProc(HWND hwnd, UINT messg, WPARAM wParam, LPARAM lParam)
  72. {
  73. OPENFILENAME of;
  74. of.lStructSize = sizeof(OPENFILENAME);
  75. of.hwndOwner = hWnd;
  76. of.lpstrFilter = "txt\000 *.txt\000\000";
  77. of.lpstrFile = file;
  78. of.nMaxFile = MAX_PATH;
  79. of.lpstrFileTitle = NULL;
  80. of.lpstrInitialDir = "D:\\";
  81. of.lpstrTitle = "Открытие файла настроек";
  82. of.Flags = OFN_HIDEREADONLY;
  83. of.hInstance = hInst;
  84. of.lpTemplateName = NULL;
  85. of.lpstrDefExt = NULL;
  86. of.lpstrCustomFilter = NULL;
  87. CHOOSECOLOR cc;
  88. cc.Flags=CC_RGBINIT|CC_FULLOPEN;
  89. cc.hInstance=NULL;
  90. cc.hwndOwner=hWnd;
  91. cc.lCustData=0L;
  92. cc.lpCustColors=dColors;
  93. cc.lpfnHook=NULL;
  94. cc.lpTemplateName=(LPSTR)NULL;
  95. cc.lStructSize=sizeof(cc);
  96. cc.rgbResult=RGB(255,0,0);
  97. PAINTSTRUCT ps;
  98. char Buf[256];
  99. switch(messg)
  100. {
  101. case WM_SIZE:
  102. MoveWindow(hStatus, 0,0,0,0, TRUE);
  103. break;
  104. case WM_PAINT:
  105. if(!n)
  106. {
  107. ValidateRect(hwnd,NULL);
  108. break;
  109. }
  110. ShowWindow(hStatus,SW_NORMAL);
  111. UpdateWindow(hStatus);
  112. hdc = BeginPaint(hwnd,&ps);
  113. hpen = CreatePen(1,2,RGB(200,0,0));
  114. SelectObject(hdc,hpen);
  115. if(first)
  116. {
  117. Draw_Ellipses_and_Find_Points(x,y,n);
  118. it = vec.begin();
  119. first = false;
  120. }
  121. else
  122. {
  123. Draw_all_Ellipses(x,y,n);
  124. if(step)
  125. {
  126. hbrush = CreateSolidBrush(color);
  127. SelectObject(hdc,hbrush);
  128. Ellipse(hdc,(it -> x) - 2*r,(it -> y) - 2*r,(it -> x) + 2*r,(it -> y) + 2*r);
  129. DeleteObject(hbrush);
  130. }
  131. else
  132. {
  133. hbrush = CreateSolidBrush(RGB(255,255,255));
  134. SelectObject(hdc,hbrush);
  135. Ellipse(hdc,(it -> x) - r,(it -> y) - r,(it -> x) + r,(it -> y) + r);
  136. DeleteObject(hbrush);
  137. it++;
  138. if(fst)
  139. {
  140. it--;
  141. fst = false;
  142. }
  143. if(it == vec.end())
  144. {
  145. it = vec.begin();
  146. KillTimer(hWnd,1);
  147. }
  148. }
  149. }
  150. DeleteObject(hpen);
  151. ValidateRect(hwnd,NULL);
  152. EndPaint(hwnd,&ps);
  153. break;
  154. case WM_DESTROY:
  155. PostQuitMessage(0);
  156. break;
  157. case WM_TIMER:
  158. if(count%2)
  159. step = true;
  160. else
  161. step = false;
  162. count++;
  163. InvalidateRect(hwnd,NULL,true);
  164. break;
  165. case WM_COMMAND:
  166. switch(wParam)
  167. {
  168. case ID_40004:
  169. MessageBox(hwnd,"Здесь могла быть ваша реклама!","Место для рекламы",MB_ICONINFORMATION|MB_OK);
  170. break;
  171. case ID_40002:
  172. DialogBox(hInst,"IDD_DIALOG",hwnd,SettingProc);
  173. break;
  174. case ID_40001:
  175. GetOpenFileName(&of);
  176. break;
  177. case ID_40003:
  178. PostQuitMessage(0);
  179. break;
  180. case ID_40005:
  181. if(ChooseColor(&cc))
  182. {
  183. color=(COLORREF)cc.rgbResult;
  184. std::sprintf(Buf, "Количество строк: %d, Радиус: %d, Цвет: %d,%d,%d\n",n,r,GetRValue(color),GetGValue(color),GetBValue(color));
  185. SendMessage(hStatus, SB_SETTEXT, 0, (LONG)Buf);
  186. }
  187. break;
  188. default:
  189. return DefWindowProc(hwnd,messg,wParam,lParam);
  190. }
  191. break;
  192. default:
  193. return DefWindowProc(hwnd,messg,wParam,lParam);
  194. }
  195. return 0;
  196. }
  197.  
  198. BOOL CALLBACK SettingProc(HWND hdlg, UINT messg, WPARAM wParam, LPARAM lParam)
  199. {
  200. char Buf[256];
  201. int c1,c2,c3,red,green,blue,i = 0;
  202. switch(messg)
  203. {
  204. case WM_INITDIALOG:
  205. return true;
  206. case WM_COMMAND:
  207. switch(wParam)
  208. {
  209. case IDNO: case WM_CLOSE:
  210. EndDialog(hdlg,LOWORD(wParam));
  211. break;
  212. case IDOK:
  213. if(SendDlgItemMessage(hdlg,IDC_RADIO2,BM_GETCHECK,0,0))
  214. {
  215. _lread(_lopen(file,OF_READ),Buf,30);
  216. n = atoi(&Buf[i]);
  217. while(Buf[i++] != ' ');
  218. r = atoi(&Buf[i++]);
  219. while(Buf[i++] != ' ');
  220. red = atoi(&Buf[i++]);
  221. while(Buf[i++] != ' ');
  222. green = atoi(&Buf[i++]);
  223. while(Buf[i++] != ' ');
  224. blue = atoi(&Buf[i++]);
  225. color = RGB(red,green,blue);
  226. }
  227. if(SendDlgItemMessage(hdlg,IDC_RADIO1,BM_GETCHECK,0,0))
  228. {
  229. char tempn[5],tempr[5];
  230. GetDlgItemText(hdlg,IDC_EDIT1,tempn,4);
  231. n = atoi(tempn);
  232. GetDlgItemText(hdlg,IDC_EDIT2,tempr,4);
  233. r = atoi(tempr);
  234. c1 = SendDlgItemMessage(hdlg,IDC_CHECK1,BM_GETCHECK,0,0);
  235. c3 = SendDlgItemMessage(hdlg,IDC_CHECK2,BM_GETCHECK,0,0);
  236. c2 = SendDlgItemMessage(hdlg,IDC_CHECK3,BM_GETCHECK,0,0);
  237. if(c1&&c2&&c3)
  238. color = RGB(255,255,255);
  239. if(c1&&c2&&(!c3))
  240. color = RGB(255,255,0);
  241. if(c1&&(!c2)&&c3)
  242. color = RGB(255,0,255);
  243. if(c1&&(!c2)&&(!c3))
  244. color = RGB(255,0,0);
  245. if((!c1)&&c2&&c3)
  246. color = RGB(0,255,255);
  247. if((!c1)&&(!c2)&&c3)
  248. color = RGB(0,0,255);
  249. if((!c1)&&c2&&(!c3))
  250. color = RGB(0,255,0);
  251. }
  252. first = true;
  253. std::sprintf(Buf, "Количество строк: %d, Радиус: %d, Цвет: %d,%d,%d\n",n,r,GetRValue(color),GetGValue(color),GetBValue(color));
  254. SendMessage(hStatus, SB_SETTEXT, 0, (LONG)Buf);
  255. EndDialog(hdlg,LOWORD(wParam));
  256. SetTimer(hWnd,1,500,NULL);
  257. break;
  258. default:
  259. return DefWindowProc(hdlg,messg,wParam,lParam);
  260. }
  261. break;
  262. default:
  263. return DefWindowProc(hdlg,messg,wParam,lParam);
  264. }
  265. return 0;
  266. }
  267.  
  268. void Draw_all_Ellipses(int a,int b, int n)
  269. {
  270. a += (n-1)*(2*r+be)/1.41;
  271. for(int i = 0; i < n; i++)
  272. {
  273. Ellipse(hdc,a-r,b-r,a+r,b+r);
  274. a -= (2*r+be)*1.41;
  275. }
  276. POINT prevline[20];
  277. for (int i = n-1, j = 0; i <= 2*n-2;)
  278. prevline[j++] = vec[i++];
  279. for(int j = n; j > 1;)
  280. {
  281. POINT curline[20];
  282. int i = 1;
  283. for(i; i < j;)
  284. {
  285. POINT p;
  286. p.x = (prevline[i-1].x + prevline[i].x)/2;
  287. p.y = prevline[i-1].y - (prevline[i-1].x - prevline[i].x)/2;
  288. curline[i-1] = p;
  289. Ellipse(hdc,p.x - r, p.y - r, p.x + r, p.y + r);
  290. i++;
  291. }
  292. for(int k = 0; k < j; k++)
  293. prevline[k] = curline[k];
  294. j--;
  295. }
  296. }
  297.  
  298. void Draw_Ellipses_and_Find_Points(int a,int b, int n)
  299. {
  300. vec.erase(vec.begin(),vec.end());
  301. a += (n-1)*(2*r+be)/1.41;
  302. for(int i = 0; i < n; i++)
  303. {
  304. Ellipse(hdc,a-r,b-r,a+r,b+r);
  305. POINT p;
  306. p.x = a;
  307. p.y = b;
  308. vec.push_back(p);
  309. a -= (2*r+be)*1.41;
  310. }
  311. POINT prevline[20];
  312. for (int i = 0; i < n; i++)
  313. prevline[i] = vec[i];
  314. for(int j = n; j > 1;)
  315. {
  316. POINT curline[20];
  317. int i = 1;
  318. for(i; i < j;)
  319. {
  320. POINT p;
  321. p.x = (prevline[i-1].x + prevline[i].x)/2;
  322. p.y = prevline[i-1].y - (prevline[i-1].x - prevline[i].x)/2;
  323. if( i == 1 )
  324. vec.insert(vec.begin(),p);
  325. else
  326. {
  327. if( i == j-1 )
  328. vec.push_back(p);
  329. }
  330. curline[i-1] = p;
  331. Ellipse(hdc,p.x - r, p.y - r, p.x + r, p.y + r);
  332. i++;
  333. }
  334. for(int k = 0; k < j; k++)
  335. prevline[k] = curline[k];
  336. j--;
  337. }
  338. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement