Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. #include <windows.h>
  2. #include <windowsx.h>
  3.  
  4. void setRowsHigh(int);
  5. void DrawTable(HWND);
  6. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  7.  
  8. const int N = 4, M = 5;
  9. HPEN hPen;
  10. int penWidth = 2;
  11.  
  12. int rowsHigh[N];
  13.  
  14. HDC winDC, memDC, drawTextDC;
  15. HBITMAP hBmpSprite, hBmpMem;
  16.  
  17. RECT prevRect, clientRect;
  18. BITMAP bm;
  19. COLORREF bckColor = COLOR_WINDOW + 1;
  20.  
  21. char strArray[N * M][255] = {
  22. {"hello"}, {"world"}, {"eqlwkeqwl"}, {"ewkw;lrkel;rkelndnf,,dmf,dsfm,fnfmd,nndf,smfnfnskdfsnkflweorjiewrioejwreworjrkel;r"}, {"1234321212"},
  23. {"qweweqwe"}, {"qweweqwe"}, {"qweweqwe"}, {"qweweqwe"}, {"qweweqwe"},
  24. {"qweweqwe"}, {"qweweqwe"}, {"qweweqwe"}, {"qweweqwe"}, {"qweweqwe"},
  25. {"qweweqwe"}, {"qweweqwe"}, {"qweweqwe"}, {"qweweqwe"}, {"qweweqwe"}
  26. };
  27.  
  28.  
  29. int APIENTRY WinMain(HINSTANCE hInstance,
  30. HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  31. {
  32. WNDCLASSEX wcex; HWND hWnd; MSG msg;
  33.  
  34. wcex.cbSize = sizeof(WNDCLASSEX);
  35. wcex.style = NULL;
  36. wcex.lpfnWndProc = WndProc;
  37. wcex.cbClsExtra = 0;
  38. wcex.cbWndExtra = 0;
  39. wcex.hInstance = hInstance;
  40. wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  41. wcex.hCursor = LoadCursor(NULL, IDI_APPLICATION);
  42. wcex.hbrBackground = (HBRUSH)(bckColor);
  43. wcex.lpszMenuName = NULL;
  44. wcex.lpszClassName = "SpriteClass";
  45. wcex.hIconSm = wcex.hIcon;
  46.  
  47. RegisterClassEx(&wcex);
  48. hWnd = CreateWindow(wcex.lpszClassName, "Table", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0,
  49. 1100, 550, NULL, NULL, hInstance, NULL);
  50. ShowWindow(hWnd, nCmdShow);
  51. UpdateWindow(hWnd);
  52.  
  53. while (GetMessage(&msg, NULL, 0, 0))
  54. {
  55. TranslateMessage(&msg);
  56. DispatchMessage(&msg);
  57. }
  58. return (int)msg.wParam;
  59. }
  60.  
  61. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  62. {
  63. // PAINTSTRUCT ps;
  64.  
  65. switch (message)
  66. {
  67. case WM_CREATE:
  68. winDC = GetDC(hWnd);
  69. memDC = CreateCompatibleDC(winDC);
  70. drawTextDC = CreateCompatibleDC(winDC);
  71. SetMapMode(drawTextDC, MM_TEXT);
  72.  
  73. GetClientRect(hWnd, &clientRect);
  74. hBmpMem = CreateCompatibleBitmap(winDC, clientRect.right, clientRect.bottom);
  75. SelectObject(memDC, hBmpMem);
  76. //SelectObject(bmpDC, hBmpSprite);
  77.  
  78. BitBlt(memDC, 0, 0, clientRect.right, clientRect.bottom, winDC, 0, 0, SRCCOPY);
  79. SetBkColor(winDC, bckColor);
  80.  
  81. hPen = CreatePen(PS_SOLID, penWidth, RGB(0, 0, 0));
  82. SelectObject(memDC, hPen);
  83. break;
  84. case WM_SIZE:
  85. SetRect(&clientRect, 0, 0, LOWORD(lParam), HIWORD(lParam));
  86. hBmpMem = CreateCompatibleBitmap(winDC, clientRect.right, clientRect.bottom);
  87. DeleteObject(SelectObject(memDC, hBmpMem));
  88. FillRect(memDC, &clientRect, (HBRUSH)GetBkColor(winDC));
  89. DrawTable(hWnd);
  90. //setRowsHigh();
  91. BitBlt(winDC, 0, 0, clientRect.right, clientRect.bottom, memDC, 0, 0, SRCCOPY);
  92. break;
  93. case WM_DESTROY:
  94. DeleteDC(memDC);
  95. DeleteDC(drawTextDC);
  96. ReleaseDC(hWnd, winDC);
  97. DeleteObject(hPen);
  98. PostQuitMessage(0);
  99. break;
  100. default:
  101. return DefWindowProc(hWnd, message, wParam, lParam);
  102. }
  103. return 0;
  104. }
  105.  
  106. void DrawTable(HWND hWnd)
  107. {
  108. int columnWidth = (clientRect.right - penWidth * (M - 1)) / M;
  109.  
  110. /*MoveToEx(memDC, 1, 0, NULL);
  111. LineTo(memDC, 1, clientRect.bottom);*/
  112. for (int i = 1; i < M; i++)
  113. {
  114. MoveToEx(memDC, i * (columnWidth + penWidth), 0, NULL);
  115. LineTo(memDC, i * (columnWidth + penWidth), clientRect.bottom);
  116. }
  117. /*MoveToEx(memDC, clientRect.right - penWidth, 0, NULL);
  118. LineTo(memDC, clientRect.right - penWidth, clientRect.bottom);*/
  119.  
  120. setRowsHigh(columnWidth);
  121.  
  122. int sum = 0;
  123. RECT rect;
  124. for (int i = 0; i < N; i++)
  125. {
  126. sum += rowsHigh[i] + penWidth;
  127. MoveToEx(memDC, 0, sum, NULL);
  128. LineTo(memDC, clientRect.right, sum);
  129. }
  130.  
  131. sum = 0;
  132. for (int i = 0; i < N; i++)
  133. {
  134. for (int j = 0; j < M; j++)
  135. {
  136. SetRect(&rect, j * (columnWidth + penWidth), sum, j * (columnWidth + penWidth) + columnWidth, sum + rowsHigh[i]);
  137. DrawText(memDC, strArray[i * M + j], -1, &rect, DT_WORDBREAK);
  138. }
  139. sum += rowsHigh[i] + penWidth;
  140. }
  141. }
  142.  
  143. void setRowsHigh(int columnWidth)
  144. {
  145. int currHigh;
  146. RECT rect;
  147. SetRect(&rect, 0, 0, columnWidth, 1000);
  148. for (int i = 0; i < N; i++)
  149. {
  150. int maxHighInRow = 0;
  151. for (int j = 0; j < M; j++)
  152. {
  153. currHigh = DrawText(drawTextDC, strArray[i * M + j], -1, &rect, DT_WORDBREAK);
  154. if (currHigh > maxHighInRow)
  155. maxHighInRow = currHigh;
  156. }
  157. rowsHigh[i] = maxHighInRow;
  158. }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement