WindowProc

hi
i have just started to learn win32 programing.i wrote my 1st program.which is very simple.i created a window.which has options to minimize maximize and close.when i click close it asks for confirmation."Are you sure to quit" with two options yes or no.problem is window is destroyed even after i click no.i want program to quit only when i click yes.but no matter i click yes or no window closes.this is windowproc function i have written.plz tell me what is wrong
LRESULT CALLBACK MyWindowProc (HWND hwnd,UINT message,WPARAM
wParam,LPARAM lParam)
{
switch(message)
{

case WM_SYSCOMMAND:

wParam &= 0xFFF0;
switch(wParam)
{

case SC_CLOSE:

if(MessageBox(hwnd, "Are you sure to quit?",
"Please Confirm", MB_YESNO) == IDYES)

DestroyWindow(hwnd);

break;

default:
return DefWindowProc(hwnd,message,wParam,lParam);
}

default:
return DefWindowProc(hwnd,message,wParam,lParam);

return 0;
}}
Last edited on
You are breaking the rules. Read the documentation on WM_CLOSE very, very, very close. Actually, it is a bit tricky. If you want your program to work, after the DestroyWindow (which only executes if IDYES) , remove the break and in its place put return 0.

Also, learn to use code tags. No one (except me, perhaps), will read gobblygood without them.
thanks a lot.for solving the issue.
This might be a better way to go...

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
#include <windows.h>
#include <tchar.h>

LRESULT CALLBACK fnWndProc(HWND hwnd, unsigned int msg, WPARAM wParam, LPARAM lParam)
{
 switch(msg)
 {
   case WM_CLOSE:
     {
        if(MessageBox(hwnd,_T("Do You Want To Exit?"),_T("Exit?"),MB_YESNO)==IDYES)
        {
           DestroyWindow(hwnd);
           PostQuitMessage(0);
        }
        return 0;
     }
 }

 return (DefWindowProc(hwnd, msg, wParam, lParam));
}

int WINAPI WinMain(HINSTANCE hIns, HINSTANCE hPrevIns, LPSTR lpszArgument, int iShow)
{
 TCHAR szClassName[]=_T("Form1");
 WNDCLASSEX wc={};
 MSG messages;
 HWND hWnd;

 wc.lpszClassName=szClassName;                wc.lpfnWndProc=fnWndProc;
 wc.cbSize=sizeof (WNDCLASSEX),               wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
 wc.hInstance=hIns,                           wc.hIconSm=LoadIcon(NULL, IDI_APPLICATION);
 wc.hCursor=LoadCursor(NULL,IDC_ARROW),       wc.hbrBackground=(HBRUSH)COLOR_BTNSHADOW;
 RegisterClassEx(&wc);
 hWnd=CreateWindowEx(0,szClassName,szClassName,WS_OVERLAPPEDWINDOW,75,75,320,305,HWND_DESKTOP,0,hIns,0);
 ShowWindow(hWnd,iShow);
 while(GetMessage(&messages,NULL,0,0))
 {
    TranslateMessage(&messages);
    DispatchMessage(&messages);
 }

 return messages.wParam;
}


Note that if msg is WM_CLOSE, the return 0 will execute regardless of whether ID_YES was returned by the message box. If DefWindowProc() executes on a WM_CLOSE, the system will send a WM_DESTROY message. That you don't want if the user says no.
Last edited on
Topic archived. No new replies allowed.