Advertisement
YauhenMardan

WinAPI_1_Graphic

Apr 20th, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. #include <Windows.h>
  2. #include "KWnd.h"
  3. #include <string>
  4. #include <sstream>
  5. #include <cmath>
  6.  
  7. using namespace std;
  8.  
  9. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  10. int f(int x, int k);
  11. void fillString(string& str, double x, double y);
  12.  
  13. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  14. {
  15. MSG msg;
  16. KWnd mainWnd("Test Class KWnd app", hInstance, nCmdShow, WndProc, NULL, 50, 100, 200, 150);
  17.  
  18. while (GetMessage(&msg, NULL, 0, 0)) {
  19. TranslateMessage(&msg);
  20. DispatchMessage(&msg);
  21. }
  22. return msg.wParam;
  23. }
  24.  
  25. LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
  26. //system
  27. HDC hDC;
  28. PAINTSTRUCT ps;
  29. static RECT rect;
  30. int userReply;
  31. //coordinates
  32. //sx,sy
  33. static int sx, sy;
  34. //logic
  35. double xPh, yPh;
  36. //physical
  37. double xLo, yLo;
  38. //mouse
  39. int xMo, yMo;
  40. //with and height
  41. static const int width = 200;
  42. static const int height = 200;
  43. //coefficient
  44. int k = 10;
  45. //steps
  46. int graphStep = 1;
  47. int gridStep = 1;
  48. //string
  49. string coordText;
  50. //pens
  51. static HPEN hPenAxis, hPenGraphic, hPenGrid;
  52. //font
  53. static HFONT hFontCoord;
  54.  
  55. switch (uMsg)
  56. {
  57. case WM_CREATE:
  58. //create pens
  59. hPenAxis = CreatePen(PS_SOLID, 0.5, RGB(0, 0, 0));
  60. hPenGraphic = CreatePen(PS_SOLID, 1.5, RGB(200, 0, 0));
  61. hPenGrid = CreatePen(PS_SOLID, 0.1, RGB(0, 200, 0));
  62. //create font
  63. hFontCoord = (HFONT)GetStockObject(ANSI_VAR_FONT);
  64. break;
  65. case WM_SIZE:
  66. sx = LOWORD(lParam);
  67. sy = HIWORD(lParam);
  68. break;
  69. case WM_PAINT:
  70. //begin paint
  71. hDC = BeginPaint(hWnd, &ps);
  72. //draw graphic
  73. //coordinates
  74. xPh = sx;
  75. yPh = sy;
  76. xLo = width;
  77. yLo = height;
  78. //modes
  79. SetMapMode(hDC, MM_ANISOTROPIC);
  80. SetWindowExtEx(hDC, 2*xLo, 2*yLo, NULL);
  81. SetViewportExtEx(hDC, 9 * xPh / 10, -9 * yPh / 10, NULL);
  82. SetViewportOrgEx(hDC, 0.5*xPh, 0.5*yPh, NULL);
  83. //draw grid
  84. SelectObject(hDC, hPenGrid);
  85. for (int i = -xLo ; i <= xLo; i+=k * gridStep)
  86. {
  87. MoveToEx(hDC, i, -yLo, NULL);
  88. LineTo(hDC, i, yLo);
  89. }
  90. for (int i = -yLo; i <= yLo; i+= k * gridStep)
  91. {
  92. MoveToEx(hDC, -xLo, i, NULL);
  93. LineTo(hDC, xLo, i);
  94. }
  95. //draw axis
  96. SelectObject(hDC, hPenAxis);
  97. MoveToEx(hDC, -xLo, 0, NULL);
  98. LineTo(hDC, xLo, 0);
  99. MoveToEx(hDC, 0, -yLo, NULL);
  100. LineTo(hDC, 0, yLo);
  101. //draw graphic
  102. SelectObject(hDC, hPenGraphic);
  103. for (int i = -xLo; i < xLo; i += graphStep)
  104. {
  105. if ((f(i, k) / k) <= yLo)
  106. {
  107. MoveToEx(hDC, i, f(i, k)/k, NULL);
  108. break;
  109. }
  110. }
  111. for (int i = -xLo; i < xLo; i+=graphStep)
  112. {
  113. if ((f(i, k) / k) <= yLo)
  114. {
  115. LineTo(hDC, i, f(i, k) / k);
  116. }
  117. }
  118. //end paint
  119. EndPaint(hWnd, &ps);
  120. break;
  121. case WM_LBUTTONDOWN:
  122. hDC = GetDC(hWnd);
  123. //get coord
  124. xMo = LOWORD(lParam);
  125. yMo = HIWORD(lParam);
  126. if (GetPixel(hDC, xMo, yMo) == RGB(200, 0, 0))
  127. {
  128. //physical coord
  129. xPh = 9 * sx / 10;
  130. yPh = 9 * sy / 10;
  131. //convert coord
  132. xLo = (xMo - sx / 2) / (k*xPh / (2 * width));
  133. yLo = ((sy / 2 - yMo) / (k*yPh / (2 * height)));
  134. fillString(coordText, round(xLo * 100) / 100, round(yLo * 100) / 100);
  135. SelectObject(hDC, hFontCoord);
  136. TextOut(hDC, xMo, yMo, coordText.c_str(), strlen(coordText.c_str()));
  137. }
  138. ReleaseDC(hWnd, hDC);
  139. break;
  140. case WM_RBUTTONDOWN:
  141. rect.left = LOWORD(lParam);
  142. rect.top = HIWORD(lParam);
  143. break;
  144. case WM_RBUTTONUP:
  145. rect.right = LOWORD(lParam);
  146. rect.bottom = HIWORD(lParam);
  147. InvalidateRect(hWnd, &rect, TRUE);
  148. break;
  149. case WM_CLOSE:
  150. userReply = MessageBox(hWnd, "Do you want to exit the app?", "Notification", MB_YESNO | DT_VCENTER);
  151. if (IDYES == userReply)
  152. DestroyWindow(hWnd);
  153. break;
  154. case WM_DESTROY:
  155. //delete pens
  156. DeleteObject(hPenAxis);
  157. DeleteObject(hPenGraphic);
  158. DeleteObject(hPenGrid);
  159. //delete font
  160. DeleteObject(hFontCoord);
  161. PostQuitMessage(0);
  162. break;
  163. default:
  164. return DefWindowProc(hWnd, uMsg, wParam, lParam);
  165. }
  166. return 0;
  167. }
  168.  
  169. int f(int x, int k)
  170. {
  171. return (0.5*x * x + 6 * k * x + k * k);
  172. }
  173.  
  174. void fillString(string& str, double x, double y)
  175. {
  176. stringstream stream;
  177. stream << "[" << x << "," << y << "]";
  178. str = stream.str();
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement