What should be returned in the Wndproc function in C++?

Asked

Viewed 203 times

1

Hello. See the code below:

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
    if (message == WM_CREATE) {
        return(?);
    } else if (message == WM_COMMAND) {
        return(?);
    } else if (message == WM_CLOSE) {
        return(?);
    } else if (message == WM_DESTROY) {
        return(?);
    } else {
        return(DefWindowProc(hWnd, message, wParam, lParam));
    }
}

I would like to know what should be returned in this function if it is processed correctly and what should be returned if an error occurs in the processing. I’d also like to know, if it’s not too much to ask, if the code Return(Defwindowproc(hwnd, message, wParam, lParam)); is placed in the right place.

Thank you.

1 answer

0


Overall, this function usually uses the switch to check the message event, but what you did is equivalent. Regarding what should be returned, when everything happens well but the event is not in the switch conditions (or in this case the "if"s), it returns "Defwindowproc(hwnd, message, wParam, lParam)" in the default just returning the message, or returns 0 at the end of the function after being accepted in a case and having passed the break, when returning 0 you are simply returning nothing, as if the event had already been solved.

In reality I do not know return of this function to inform error and I do not know if it exists, but generally, when a serious error occurs, or the program stops working, or you show a Messagebox informing the error and returns message in the loop asking to close the program, usually this is done using "Postquitmessage(0);" which basically returns the WM_CLOSE message to the loop, basically it’s like doing "Return Sendmessage(hwnd, WM_CLOSE, 0, 0);".

Basicamento to not repeat several Returns you can put it at the end of the function because whenever it is successful it will return 0, would be like this:

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
if (message == WM_CREATE) {
    // Ações durante o carregamento
} else if (message == WM_COMMAND) {
    // Ações ao clicar em algo
} else if (message == WM_CLOSE) {
    // Ações ao fechar a janela
} else if (message == WM_DESTROY) {
    PostQuitMessage(0); // MSG para fechar o programa, manda a mensagem WM_CLOSE
} else {
    return(DefWindowProc(hWnd, message, wParam, lParam)); // Devolve a mensagem
}
return 0;  // Se o evento foi resolvido retorna 0
}

Yes, the "Return(Defwindowproc(hwnd, message, wParam, lParam));" is placed in the right place, because this is called whenever none of the conditions were accepted, that is, it is the last case so it is in the default block of the switch, but when passing through an event returns if 0 at the end of the function to inform that the event has already been solved.

I hope I’ve helped

Browser other questions tagged

You are not signed in. Login or sign up in order to post.