WM_DESTROY is not being caught

help please, I'm trying to make it so that when the x on my second window is pressed I can intercept it and call code, but what I've got that I think should work isn't working

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <windows.h>
#include <string>
#include  <stdio.h>
#define ID_1 1000
#define ID_2 1001
#define blah 1003
    HWND mainwin;  
    HWND hwnd2;
char ID_ONE;
/*  Declare Windows procedure  */
LRESULT CALLBACK Proc2 (HWND, UINT, WPARAM, LPARAM);
WNDCLASSEX wincl2;
/*  Make the class name into a global variable  */
char szClassName[ ] = "WindowsApp";

int WINAPI WinMain (HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int nCmdShow)

{
             /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX winclz;        /* Data structure for the windowclass */
    
    /* The Window structure */
    winclz.hInstance = hThisInstance;
    winclz.lpszClassName = szClassName;
    winclz.lpfnWndProc = Proc2;      /* This function is called by windows */

    winclz.style = CS_DBLCLKS;                 /* Catch double-clicks */
    winclz.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    winclz.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    winclz.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    winclz.hCursor = LoadCursor (NULL, IDC_ARROW);
    winclz.lpszMenuName = NULL;                 /* No menu */
    winclz.cbClsExtra = 0;                      /* No extra bytes after the window class */
    winclz.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default color as the background of the window */
    winclz.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&winclz))
        return 0;

HMENU hmenu = CreateMenu();
	InsertMenu(hmenu, 1, MF_BYPOSITION | MF_STRING, ID_ONE, NULL);
	
HMENU hSubMenu2 = CreatePopupMenu();
      AppendMenu(hSubMenu2, MF_STRING, ID_1, "connect");
      AppendMenu(hSubMenu2, MF_STRING, ID_2, "quit");
      InsertMenu(hmenu, 0, MF_POPUP|MF_BYPOSITION, (UINT_PTR)hSubMenu2, "Actions");
      
      
      
    /* The class is registered, let's create the program*/
    mainwin = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           "Pocket Rocket client",       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           544,                 /* The programs width */
           375,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           hmenu,                /* some menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

 hwnd2 = CreateWindowEx (0, "EDIT", "", WS_SYSMENU, CW_USEDEFAULT, CW_USEDEFAULT, 100, 24, mainwin,	NULL, GetModuleHandle(NULL), NULL);
    /* Make the window visible on the screen */
    ShowWindow (mainwin, nCmdShow);

                                  
    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}


/*  This function is called by the Windows function DispatchMessage()  */


LRESULT CALLBACK Proc2(HWND mainwin, UINT message, WPARAM wParam, LPARAM lParam )
{
switch (message)                  /* handle the messages */
    {
                                  case WM_DESTROY:
                                       PostQuitMessage(0);
                                       break;
    } 
     switch (wParam)                  /* handle the messages */
    {
                  case ID_2 :
                PostQuitMessage(0);
                break;
                  case ID_1 :
                   ShowWindow (hwnd2, SW_SHOW);     
                   EnableWindow(mainwin, false); 
                   break;
                                default:                      /* for messages that we don't deal with */
            return DefWindowProc (mainwin, message, wParam, lParam); 
                        break;
    }  

        return 0;
}    
LRESULT CALLBACK Proc3(HWND hwnd2, UINT message, WPARAM wParam, LPARAM lParam )
{
       switch (wParam)
      {
         case WM_DESTROY:
            EnableWindow(mainwin, true);
            PostQuitMessage(0);
          break;
          }           
}
Hi,
Your second window doesn't have a window procedure as far as I can determine. The window procedure (Proc2) is called "by the window" when it is assigned to it. You assign a window procedure by setting is in a class and putting the class in the CreateWindow function.
1
2
3
WNDCLASSEX winclz;
	winclz.lpfnWndProc   = Proc2;
hwnd2 = CreateWindowEx (0, winclz,


When you're just starting windows API stuff, use the following message box function to debug:
MessageBox(NULL,"error or success","",0);
Last edited on
Topic archived. No new replies allowed.