Window Close Button MessageBox with GLUT

Oct 13, 2023 at 1:38am
Is there a way to attach a messagebox confirmation if a GLUT Window close button is triggered?

Currently this works but it disables GLUT keyboard events:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
	int choice;
    switch (uMsg) {
        case WM_CLOSE:
            // Display the message box
            choice = MessageBox(NULL, "Quit?", "MyWindow", MB_YESNO | MB_ICONQUESTION);
            if (choice == IDYES) {
                // Close the window
                DestroyWindow(hWnd);
                exit(0);
            }
            // Do nothing if the user selects "No" or closes the message box without making a choice
            return 0;
        default:
            return DefWindowProc(hWnd, uMsg, wParam, lParam);
    }
}

...
...

hWnd = FindWindow(NULL, "MyWindow");
WNDPROC oldProc = (WNDPROC)SetWindowLongPtr(hWnd, GWLP_WNDPROC, (LONG_PTR)WindowProc);


I'm using GLUT since this is overall intended for cross-platform and this extra part is just specific for Windows.
Last edited on Oct 13, 2023 at 1:41am
Oct 13, 2023 at 11:42am
Have you tried forwarding the messages to oldProc(), instead of DefWindowProc(), in your WindowProc() ???

Probably GLUT has installed its own WNDPROC and you are kicking that one out of chain!

Also I think that obtaining the window handle with FindWindow() where only the title is specified, is not really reliable. Normally you should have the HWND of the window that you created yourself in a more direct way.
Last edited on Oct 13, 2023 at 12:52pm
Oct 13, 2023 at 10:11pm
Thanks kigar, solved.. also got a suggestion to use hWnd = GetActiveWindow() instead.
Oct 14, 2023 at 12:02pm
Try with global __glutCurrentWindow variable:

1
2
3
extern GLUTwindow *__glutCurrentWindow;
__glutCurrentWindow->win // <-- should be HWND on Win32
__glutCurrentWindow->renderWin // <-- should be HWND on Win32 
Last edited on Oct 14, 2023 at 12:13pm
Topic archived. No new replies allowed.