Advertisement
YauhenMardan

SH

Apr 6th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.40 KB | None | 0 0
  1. #pragma warning(disable:4996)
  2. #undef UNICODE
  3. #define _USE_MATH_DEFINES
  4. #include <windows.h>
  5. #include "KWnd.h"
  6. #include <cmath>
  7. #include <string>
  8. #include <sstream>
  9.  
  10. using namespace std;
  11.  
  12. LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
  13. void DrawBarChart(HDC hDC);
  14. void DrawPieChart(HDC hDC);
  15. void fillString(string& str, string name, int rating);
  16. string fromIntToString(int i);
  17.  
  18. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  19. {
  20. MSG msg;
  21. KWnd mainWnd("MyWin", hInstance, nCmdShow, WndProc);
  22. while (GetMessage(&msg, NULL, 0, 0))
  23. {
  24. TranslateMessage(&msg);
  25. DispatchMessage(&msg);
  26. }
  27. return (msg.wParam);
  28. }
  29.  
  30. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  31. {
  32. PAINTSTRUCT ps;
  33. HDC hDC;
  34. //
  35. const int Q_PARTICIPANT = 6;
  36. const int PIE_CHART_WIDTH = 314;
  37. const int PIE_CHART_HEIGHT = 314;
  38. const int BAR_CHART_WIDTH = 24;
  39. const int BAR_CHART_HEIGHT = 100;
  40. //coordinates
  41. static int sx, sy;
  42. static int x_scr1, y_scr1, x_scr2, y_scr2;
  43. //pens
  44. static HPEN hPenAxis, hPenGraphic;
  45. //brush
  46. static HBRUSH hBrushArr[Q_PARTICIPANT];
  47. //font
  48. static HFONT hFontName;
  49. static LOGFONT lf;
  50. //data
  51. string sNames[Q_PARTICIPANT] = { "Smith","Alzheimer","Freud","Tesla","Black","Parkinson" };
  52. static int iRating[Q_PARTICIPANT] = { 76,30,22,54,70,95 };
  53. static int iRatingSum = 0;
  54. string info;
  55. //values for pie
  56. double angle;
  57. double r; //radius
  58. static double start;
  59.  
  60.  
  61. switch (message) {
  62. case WM_CREATE:
  63. //create pens
  64. hPenAxis = CreatePen(PS_SOLID, 0.5, RGB(0, 0, 0));
  65. hPenGraphic = CreatePen(PS_SOLID, 0.5, RGB(50, 200, 80));
  66. //create brush
  67. hBrushArr[0] = CreateSolidBrush(RGB(255, 165, 0));
  68. hBrushArr[1] = CreateSolidBrush(RGB(0, 255, 165));
  69. hBrushArr[2] = CreateSolidBrush(RGB(165, 0, 255));
  70. hBrushArr[3] = CreateSolidBrush(RGB(165, 255, 0));
  71. hBrushArr[4] = CreateSolidBrush(RGB(0, 165, 255));
  72. hBrushArr[5] = CreateSolidBrush(RGB(255, 0, 165));
  73. //init logfont
  74. lf.lfCharSet = DEFAULT_CHARSET;
  75. lf.lfPitchAndFamily = DEFAULT_PITCH;
  76. strcpy_s(lf.lfFaceName, "My Font");
  77. lf.lfWeight = FW_NORMAL;
  78. lf.lfEscapement = 900;
  79. //count rating sum
  80. for (int i = 0; i < Q_PARTICIPANT; i++)
  81. {
  82. iRatingSum +=iRating[i];
  83. }
  84. break;
  85. case WM_SIZE:
  86. sx = LOWORD(lParam);
  87. sy = HIWORD(lParam);
  88. //init logfont
  89. break;
  90. case WM_PAINT:
  91. //begin paint
  92. hDC = BeginPaint(hWnd, &ps);
  93. //create font
  94. lf.lfHeight = sx / 400;
  95. hFontName = CreateFontIndirect(&lf);
  96. //devide space
  97. MoveToEx(hDC, sx / 2, 0, NULL);
  98. LineTo(hDC, sx / 2, sy);
  99. //
  100. //Draw bar chart
  101. //
  102. //set modes
  103. SetMapMode(hDC, MM_ANISOTROPIC);
  104. SetWindowExtEx(hDC, BAR_CHART_WIDTH, BAR_CHART_HEIGHT, NULL);
  105. SetViewportExtEx(hDC, (sx / 2 - 90), -(sy - 90), NULL);
  106. SetViewportOrgEx(hDC, 0+15, sy-15, NULL);
  107. //draw axis
  108. SelectObject(hDC, hPenAxis);
  109. MoveToEx(hDC, 0, BAR_CHART_HEIGHT, NULL);
  110. LineTo(hDC, 0, 0);
  111. LineTo(hDC, BAR_CHART_WIDTH, 0);
  112. //draw graphic
  113. SelectObject(hDC, hPenGraphic);
  114. SelectObject(hDC, hFontName);
  115. SetTextAlign(hDC, TA_RIGHT | TA_TOP);
  116. SetBkMode(hDC, TRANSPARENT);
  117. for (int i = 0; i < Q_PARTICIPANT; i++)
  118. {
  119. SelectObject(hDC, hBrushArr[i]);
  120. Rectangle(hDC, 4*i+1, iRating[i], 4*i+3, 0);
  121. TextOut(hDC, 4 * i + 2, iRating[i], fromIntToString(iRating[i]).c_str(), strlen(fromIntToString(iRating[i]).c_str()));
  122. TextOut(hDC, 4 * i + 3, iRating[i], sNames[i].c_str(), strlen(sNames[i].c_str()));
  123. }
  124. DeleteObject(hFontName);
  125. //
  126. //Draw pie chart
  127. //
  128. //create font
  129. lf.lfHeight = sx / 80;
  130. hFontName = CreateFontIndirect(&lf);
  131. //set modes
  132. SetMapMode(hDC, MM_ISOTROPIC);
  133. SetWindowExtEx(hDC, PIE_CHART_WIDTH, PIE_CHART_WIDTH, NULL);
  134. SetViewportExtEx(hDC, sx / 2, -(sy / 2), NULL);
  135. SetViewportOrgEx(hDC, 3 * sx / 4, sy / 2, NULL);
  136. //draw graphic
  137. SelectObject(hDC, hFontName);
  138. r = PIE_CHART_WIDTH / 2;
  139. start = 0;
  140. for (int i = 0; i < Q_PARTICIPANT; i++)
  141. {
  142. SelectObject(hDC, hBrushArr[i]);
  143. x_scr1 = r * cos(2 * M_PI* start / iRatingSum);
  144. y_scr1 = r * sin(2 * M_PI* start / iRatingSum);
  145. x_scr2 = r * cos(2 * M_PI* (iRating[i] + start) / iRatingSum);
  146. y_scr2 = r * sin(2 * M_PI* (iRating[i] + start) / iRatingSum);
  147. start += iRating[i];
  148. Pie(hDC, -PIE_CHART_WIDTH / 2, -PIE_CHART_WIDTH / 2, PIE_CHART_WIDTH / 2, PIE_CHART_WIDTH / 2, x_scr1, y_scr1, x_scr2, y_scr2);
  149. fillString(info, sNames[i], iRating[i]);
  150. TextOut(hDC, x_scr2, y_scr2, info.c_str(), strlen(info.c_str()));
  151. }
  152. //delete font
  153. DeleteObject(hFontName);
  154. //end paint
  155. EndPaint(hWnd, &ps);
  156. break;
  157. case WM_DESTROY:
  158. //delete pens
  159. DeleteObject(hPenAxis);
  160. DeleteObject(hPenGraphic);
  161. //delete brush
  162. for (int i = 0; i < Q_PARTICIPANT; i++)
  163. {
  164. DeleteObject(hBrushArr[i]);
  165. }
  166. PostQuitMessage(0);
  167. break;
  168. default: return DefWindowProc(hWnd, message, wParam, lParam);
  169. }
  170. return 0;
  171. }
  172.  
  173. void fillString(string& str, string name, int rating)
  174. {
  175. stringstream stream;
  176. stream << name << " " << rating << "%";
  177. str = stream.str();
  178. }
  179. string fromIntToString(int i)
  180. {
  181. stringstream stream;
  182. stream << i << "%";
  183. return stream.str();
  184. }
  185.  
  186. //r = PIE_CHART_WIDTH / 2;
  187. //MoveToEx(hDC, r * cos(0), r * sin(0), NULL);
  188. //for (angle = 0; angle <= 2 * M_PI; angle += 0.01)
  189. //{
  190. // x_scr = r * cos(angle);
  191. // y_scr = r * sin(angle);
  192. // LineTo(hDC, x_scr, y_scr);
  193. //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement