在WM_PAINT之后调整窗口大小以适合内容

时间:2015-03-10 06:11:47

标签: c++ c winapi

有没有办法在完成所有WM_PAINT调用后调整大小?所以我可以自动调整我的窗口大小以适应我画的任何东西,因为最初我的窗口大小定义为一定的大小,可能有剩余的空间没有画,我怎么能删除这些剩余的空间? 从下面的屏幕截图中可以看出,白色空间是剩余的空间。 enter image description here

#include <windows.h>
#include "resource.h"
#include "loadFromTextFile.h"

const char g_szClassName[] = "myWindowClass";
const int recLength = 3;
const int wndHeight = 700;
const int wndWidth = 700;
PAINTSTRUCT ps;
HDC            hdc;
HBRUSH blackBrush = CreateSolidBrush(RGB(0, 0, 0));
HBRUSH whiteBrush = CreateSolidBrush(RGB(255, 255, 255));
HBRUSH redBrush = CreateSolidBrush(RGB(255, 0, 0));
HPEN myPen = CreatePen(PS_NULL, 1, RGB(0, 0, 0));

//load from text
string FILE_PATH = ("10x10.txt");
loadFromTextFile loaded = loadFromTextFile(FILE_PATH);
int mazeWidth = loaded.getColumns() * recLength;
int mazeHeight = loaded.getRows() * recLength;
void paintMaze(HWND hwnd, vector<vector<string> > grid){
    hdc = ::BeginPaint(hwnd, &ps);
    SelectObject(hdc, myPen);

    for (int r = 0; r < loaded.getRows(); r++){
        for (int c = 0; c < loaded.getColumns(); c++){

            RECT rect;
            rect.left = c*recLength;//x upper left
            rect.top = r*recLength;//y upper left
            rect.right = (c + 1)*recLength;//x lower right corner
            rect.bottom = (r + 1)*recLength;//y lower right corner

            if (grid[r][c] == "1"){//1 path

                FillRect(hdc, &rect, blackBrush);
            }
            else if (grid[r][c] == "0"){//wall

                FillRect(hdc, &rect, whiteBrush);
            }
            if (r == 50 && c == 50){
                FillRect(hdc, &rect, redBrush);
            }
        }
    }
    DeleteObject(blackBrush);
    DeleteObject(whiteBrush);
    DeleteObject(redBrush);
    DeleteObject(myPen);
    EndPaint(hwnd, &ps);
}
// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg)
    {
    case WM_COMMAND:
        switch (LOWORD(wParam))
        {
        case ID_FILE_EXIT:
            PostMessage(hwnd, WM_CLOSE, 0, 0);
            break;
        case ID_ABOUT:
            SetWindowPos(hwnd, 0, 0, 0, mazeWidth, mazeHeight, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
            break;
        }
        break;
    case WM_PAINT:

        {
            paintMaze(hwnd, loaded.getNodeGrid());
        }

        break;


    case WM_CLOSE:
        DestroyWindow(hwnd);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    //Step 1: Registering the Window Class
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = 0;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wc.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);;
    wc.lpszClassName = g_szClassName;
    wc.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON1));
    wc.hIconSm = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON1), IMAGE_ICON, 32, 32, 0);

    if (!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    // Step 2: Creating the Window
    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        g_szClassName,
        " Maze",
        WS_OVERLAPPED | WS_MINIMIZEBOX | WS_SYSMENU,//prevent resize
        CW_USEDEFAULT, CW_USEDEFAULT, wndWidth, wndHeight,
        NULL, NULL, hInstance, NULL);

    if (hwnd == NULL)
    {
        MessageBox(NULL, "Window Creation Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    // Step 3: The Message Loop
    while (GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
}

1 个答案:

答案 0 :(得分:0)

在屏幕外缓冲区中绘画。

这允许您调整窗口大小。

通过复制缓冲区来绘制窗口。