Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

c++ - Why does this LRESULT CALLBACK-function not work after adding code to let it react to a button press?

So this is the function that works:

LRESULT CALLBACK windowCallback(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
        LRESULT result = 0;

        //check and execute input
        switch (uMsg) {
            case WM_CLOSE:
            case WM_DESTROY: {
                running = false;
            } break;

            case WM_SIZE: {
                RECT rect;
                GetClientRect(hWnd, &rect);

                //calculate width and height of the window in pixels
                bufferWidth = rect.right - rect.left;
                bufferHeight = rect.bottom - rect.top;

                //calculate memory for all pixels
                int bufferSize = bufferWidth * bufferHeight * sizeof(unsigned int);

                //check for allocated memory: if true: release it
                if (bufferMemory) {
                    VirtualFree(bufferMemory, 0, MEM_RELEASE);
                }

                //allocate memory for all pixels
                bufferMemory = VirtualAlloc(0, bufferSize, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);

                //fill in bitmapinfo
                bufferBitmapInfo.bmiHeader.biSize = sizeof(bufferBitmapInfo.bmiHeader);
                bufferBitmapInfo.bmiHeader.biWidth = bufferWidth;
                bufferBitmapInfo.bmiHeader.biHeight = bufferHeight;
                bufferBitmapInfo.bmiHeader.biPlanes = 1;
                bufferBitmapInfo.bmiHeader.biBitCount = 32;
                bufferBitmapInfo.bmiHeader.biCompression = BI_RGB;
                //window::resize();
                break;
            }

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

...but when I add this code to it, it doesn't work anymore and just tells me that the function is faulty, but doesn't give me any errors?(I added the code after the closing bracket of the "case WM_SIZE"-thing + the render::rect()-function works fine):

case WM_KEYDOWN: {
        switch (wParam) {
            case VK_LEFT: {
                render::rect(100, 100, 100, 100, 0x123456, false);
                break;
            }
        }
        break;
}

I changed the code now and edited here too...but it still doesn't work :/ This is the output:

1> ------ Build started: Project: IdkAnyNamesAnymore -_-, Configuration: Debug Win32 ------
1> A task was canceled.
1> A task was canceled.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Ok it seems like VS just didn't like me, because when I just retested it with the old Build it didn't work either, so I restarted the program and the solution works! Thx :D

question from:https://stackoverflow.com/questions/65919771/why-does-this-lresult-callback-function-not-work-after-adding-code-to-let-it-rea

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

1 Answer

0 votes
by (71.8m points)

Your WM_SIZE handler is missing a break statement:

case WM_SIZE: {
    ...
    break; // <-- ADD THIS
}

Without that, any WM_SIZE message that is received will fall through to your WM_KEYDOWN handler, skipping DefWindowProc().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...