Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
146 views
in Technique[技术] by (71.8m points)

c++ - Window won't update while changing its shape

I have successfully created a window but it wont update the window to change its shape and color. I have tried to solve this for days an appreciate any help.

The program is meant to show an orange window that can be reshaped. It would also help, if you could check the code by compiling it in Visual Studios.

Here is the code:

#include <Windows.h>
#include "Header.h"

bool running = true;

void *buffer_memory;
int buffer_width;
int buffer_hight;
BITMAPINFO buffer_bitmap_info;

LRESULT CALLBACK windows_callback(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    LRESULT result = 0;
    switch (uMsg)
    {
        case WM_CLOSE:
        case WM_DESTROY:
            {
                running = false;
            }
            break;

        case WM_SIZE:
            {
                RECT rect;
                GetClientRect(hwnd, &rect);
                buffer_width = rect.left - rect.right;
                buffer_hight = rect.bottom - rect.top;
                int buffer_size = buffer_width *buffer_hight* sizeof(unsigned int);
                if (buffer_memory) VirtualFree(buffer_memory, 0, MEM_RELEASE);
                buffer_memory = VirtualAlloc(0, buffer_size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
                buffer_bitmap_info.bmiHeader.biSize = sizeof(buffer_bitmap_info.bmiHeader);
                buffer_bitmap_info.bmiHeader.biWidth = buffer_width;
                buffer_bitmap_info.bmiHeader.biHeight = buffer_hight;
                buffer_bitmap_info.bmiHeader.biPlanes = 1;
                buffer_bitmap_info.bmiHeader.biBitCount = 32;
                buffer_bitmap_info.bmiHeader.biCompression = BI_RGB;
            }
            break;

        default:
            {
                result = DefWindowProc(hwnd, uMsg, wParam, lParam);
            }
    }
    return result;
}

int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    //compile window
    CHAR clsName[] = "test";
    WNDCLASSA window_class = {};
    window_class.style = CS_HREDRAW | CS_VREDRAW;
    window_class.lpszClassName = clsName;
    window_class.lpfnWndProc = windows_callback;
    //register clases
    ATOM atom = RegisterClassA(&window_class);
    if (0 == atom)
    {
        DWORD err = GetLastError();
        return 1;
    }

    // create window
    HWND window = CreateWindow(clsName, "game", WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT,
        CW_USEDEFAULT, 720, 360, 0, 0, hInstance, 0);
    if (NULL == window)
    {

        DWORD err = GetLastError();
        return 1;
    }
    while (running)
    {
        HDC hdc = GetDC(window);
        // input

        // simulate
        MSG mesage;
        while (PeekMessage(&mesage, window, 0, 0, PM_REMOVE))
        {
            TranslateMessage(&mesage);
            DispatchMessage(&mesage);
        }
        unsigned int *pixel = (unsigned int *) buffer_memory;
        for (int y = 0; y < buffer_hight; y++)
        {
            for (int x = 0; x < buffer_width; x++)
            {

                *pixel++ = 0xff5500;
            }
        }
        // render
        StretchDIBits(hdc, 0, 0, buffer_width, buffer_hight, 0, 0, buffer_width, buffer_hight,
            buffer_memory, &buffer_bitmap_info, DIB_RGB_COLORS, SRCCOPY); {}
    }
};

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The line: buffer_width = rect.left - rect.right;

=> buffer_width = rect.right- rect.left ;

You should draw in the WM_PAINT message,

case WM_PAINT:
    {
        PAINTSTRUCT ps;
        HDC hdc = BeginPaint(hwnd, &ps);
        unsigned int* pixel = (unsigned int*)buffer_memory;
        for (int y = 0; y < buffer_hight; y++)
        {
            for (int x = 0; x < buffer_width; x++)
            {

                *pixel++ = 0xff5500;
            }
        }
        // render
        StretchDIBits(hdc, 0, 0, buffer_width, buffer_hight, 0, 0, buffer_width, buffer_hight,
            buffer_memory, &buffer_bitmap_info, DIB_RGB_COLORS, SRCCOPY); {}
        EndPaint(hwnd, &ps);
    }
    break;

Updated:

#include <Windows.h>
//#include "Header.h"

bool running = true;

void* buffer_memory;
int buffer_width;
int buffer_hight;
BITMAPINFO buffer_bitmap_info;

LRESULT CALLBACK windows_callback(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    LRESULT result = 0;
    switch (uMsg)
    {
    case WM_CLOSE:
    case WM_DESTROY:
    {
        PostQuitMessage(0);
    }
    break;

    case WM_SIZE:
    {
        RECT rect;
        GetClientRect(hwnd, &rect);
        buffer_width = rect.right - rect.left;
        buffer_hight = rect.bottom - rect.top;
        int buffer_size = buffer_width * buffer_hight * sizeof(unsigned int);
        if (buffer_memory) VirtualFree(buffer_memory, 0, MEM_RELEASE);
        buffer_memory = VirtualAlloc(0, buffer_size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
        buffer_bitmap_info.bmiHeader.biSize = sizeof(buffer_bitmap_info.bmiHeader);
        buffer_bitmap_info.bmiHeader.biWidth = buffer_width;
        buffer_bitmap_info.bmiHeader.biHeight = buffer_hight;
        buffer_bitmap_info.bmiHeader.biPlanes = 1;
        buffer_bitmap_info.bmiHeader.biBitCount = 32;
        buffer_bitmap_info.bmiHeader.biCompression = BI_RGB;
    }
    break;
    case WM_PAINT:
    {
        PAINTSTRUCT ps;
        HDC hdc = BeginPaint(hwnd, &ps);
        unsigned int* pixel = (unsigned int*)buffer_memory;
        for (int y = 0; y < buffer_hight; y++)
        {
            for (int x = 0; x < buffer_width; x++)
            {

                *pixel++ = 0xff5500;
            }
        }
        // render
        StretchDIBits(hdc, 0, 0, buffer_width, buffer_hight, 0, 0, buffer_width, buffer_hight,
            buffer_memory, &buffer_bitmap_info, DIB_RGB_COLORS, SRCCOPY); {}
        EndPaint(hwnd, &ps);
    }
    break;
    default:
    {
        result = DefWindowProc(hwnd, uMsg, wParam, lParam);
    }
    }
    return result;
}

int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    //compile window
    CHAR clsName[] = "test";
    WNDCLASSA window_class = {};
    window_class.style = CS_HREDRAW | CS_VREDRAW;
    window_class.lpszClassName = clsName;
    window_class.lpfnWndProc = windows_callback;
    //register clases
    ATOM atom = RegisterClassA(&window_class);
    if (0 == atom)
    {
        DWORD err = GetLastError();
        return 1;
    }

    // create window
    HWND window = CreateWindow(clsName, "game", WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT,
        CW_USEDEFAULT, 720, 360, 0, 0, hInstance, 0);
    if (NULL == window)
    {

        DWORD err = GetLastError();
        return 1;
    }
    MSG msg;

    // Main message loop:
    while (GetMessage(&msg, nullptr, 0, 0))
    {     
         TranslateMessage(&msg);
         DispatchMessage(&msg);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...