Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <windowsx.h>
- void setRowsHigh(int);
- void DrawTable(HWND);
- LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
- const int N = 4, M = 5;
- HPEN hPen;
- int penWidth = 2;
- int rowsHigh[N];
- HDC winDC, memDC, drawTextDC;
- HBITMAP hBmpSprite, hBmpMem;
- RECT prevRect, clientRect;
- BITMAP bm;
- COLORREF bckColor = COLOR_WINDOW + 1;
- char strArray[N * M][255] = {
- {"hello"}, {"world"}, {"eqlwkeqwl"}, {"ewkw;lrkel;rkelndnf,,dmf,dsfm,fnfmd,nndf,smfnfnskdfsnkflweorjiewrioejwreworjrkel;r"}, {"1234321212"},
- {"qweweqwe"}, {"qweweqwe"}, {"qweweqwe"}, {"qweweqwe"}, {"qweweqwe"},
- {"qweweqwe"}, {"qweweqwe"}, {"qweweqwe"}, {"qweweqwe"}, {"qweweqwe"},
- {"qweweqwe"}, {"qweweqwe"}, {"qweweqwe"}, {"qweweqwe"}, {"qweweqwe"}
- };
- int APIENTRY WinMain(HINSTANCE hInstance,
- HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
- {
- WNDCLASSEX wcex; HWND hWnd; MSG msg;
- wcex.cbSize = sizeof(WNDCLASSEX);
- wcex.style = NULL;
- wcex.lpfnWndProc = WndProc;
- wcex.cbClsExtra = 0;
- wcex.cbWndExtra = 0;
- wcex.hInstance = hInstance;
- wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- wcex.hCursor = LoadCursor(NULL, IDI_APPLICATION);
- wcex.hbrBackground = (HBRUSH)(bckColor);
- wcex.lpszMenuName = NULL;
- wcex.lpszClassName = "SpriteClass";
- wcex.hIconSm = wcex.hIcon;
- RegisterClassEx(&wcex);
- hWnd = CreateWindow(wcex.lpszClassName, "Table", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0,
- 1100, 550, NULL, NULL, hInstance, NULL);
- ShowWindow(hWnd, nCmdShow);
- UpdateWindow(hWnd);
- while (GetMessage(&msg, NULL, 0, 0))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return (int)msg.wParam;
- }
- LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- // PAINTSTRUCT ps;
- switch (message)
- {
- case WM_CREATE:
- winDC = GetDC(hWnd);
- memDC = CreateCompatibleDC(winDC);
- drawTextDC = CreateCompatibleDC(winDC);
- SetMapMode(drawTextDC, MM_TEXT);
- GetClientRect(hWnd, &clientRect);
- hBmpMem = CreateCompatibleBitmap(winDC, clientRect.right, clientRect.bottom);
- SelectObject(memDC, hBmpMem);
- //SelectObject(bmpDC, hBmpSprite);
- BitBlt(memDC, 0, 0, clientRect.right, clientRect.bottom, winDC, 0, 0, SRCCOPY);
- SetBkColor(winDC, bckColor);
- hPen = CreatePen(PS_SOLID, penWidth, RGB(0, 0, 0));
- SelectObject(memDC, hPen);
- break;
- case WM_SIZE:
- SetRect(&clientRect, 0, 0, LOWORD(lParam), HIWORD(lParam));
- hBmpMem = CreateCompatibleBitmap(winDC, clientRect.right, clientRect.bottom);
- DeleteObject(SelectObject(memDC, hBmpMem));
- FillRect(memDC, &clientRect, (HBRUSH)GetBkColor(winDC));
- DrawTable(hWnd);
- //setRowsHigh();
- BitBlt(winDC, 0, 0, clientRect.right, clientRect.bottom, memDC, 0, 0, SRCCOPY);
- break;
- case WM_DESTROY:
- DeleteDC(memDC);
- DeleteDC(drawTextDC);
- ReleaseDC(hWnd, winDC);
- DeleteObject(hPen);
- PostQuitMessage(0);
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- return 0;
- }
- void DrawTable(HWND hWnd)
- {
- int columnWidth = (clientRect.right - penWidth * (M - 1)) / M;
- /*MoveToEx(memDC, 1, 0, NULL);
- LineTo(memDC, 1, clientRect.bottom);*/
- for (int i = 1; i < M; i++)
- {
- MoveToEx(memDC, i * (columnWidth + penWidth), 0, NULL);
- LineTo(memDC, i * (columnWidth + penWidth), clientRect.bottom);
- }
- /*MoveToEx(memDC, clientRect.right - penWidth, 0, NULL);
- LineTo(memDC, clientRect.right - penWidth, clientRect.bottom);*/
- setRowsHigh(columnWidth);
- int sum = 0;
- RECT rect;
- for (int i = 0; i < N; i++)
- {
- sum += rowsHigh[i] + penWidth;
- MoveToEx(memDC, 0, sum, NULL);
- LineTo(memDC, clientRect.right, sum);
- }
- sum = 0;
- for (int i = 0; i < N; i++)
- {
- for (int j = 0; j < M; j++)
- {
- SetRect(&rect, j * (columnWidth + penWidth), sum, j * (columnWidth + penWidth) + columnWidth, sum + rowsHigh[i]);
- DrawText(memDC, strArray[i * M + j], -1, &rect, DT_WORDBREAK);
- }
- sum += rowsHigh[i] + penWidth;
- }
- }
- void setRowsHigh(int columnWidth)
- {
- int currHigh;
- RECT rect;
- SetRect(&rect, 0, 0, columnWidth, 1000);
- for (int i = 0; i < N; i++)
- {
- int maxHighInRow = 0;
- for (int j = 0; j < M; j++)
- {
- currHigh = DrawText(drawTextDC, strArray[i * M + j], -1, &rect, DT_WORDBREAK);
- if (currHigh > maxHighInRow)
- maxHighInRow = currHigh;
- }
- rowsHigh[i] = maxHighInRow;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement