Messagebox on program exit

Hello. I've been doing C++ for a week or 2 now and I'm trying to have a messagebox appear when the "X" button on the title bar is clicked.

In notepad, when you type something in then press the X button, it asks you if you want to save the file. I'm trying to simulate that in my program.

I'm using Dev-C++ right now.

Thanks, any help is appreciated. (I wont be able to reply for a little while have to go to movies.)



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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <windows.h>


LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void CreateMenubar(HWND);
void OpenDialog(HWND);
void LoadFile(LPSTR);

#define IDM_FILE_NEW 1
#define IDM_FILE_QUIT 2
#define IDM_EDIT_UNDO 3
#define IDM_EDIT_REDO 4


HWND ghwndEdit;



int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, 
    LPSTR lpCmdLine, int nCmdShow )
{
  MSG  msg ;    
  WNDCLASS wc = {0};
  HWND hwnd;

  wc.lpszClassName = TEXT( "Opendialog" );
  wc.hInstance     = hInstance ;
  wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
  wc.lpfnWndProc   = WndProc ;
  wc.hCursor       = LoadCursor(0, IDC_ARROW);
  wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);


  
  RegisterClass(&wc);
hwnd =  CreateWindow( wc.lpszClassName, TEXT("Opendialog"),
                WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                150, 150, 400, 300, 0, 0, hInstance, 0);  
                
       

  while( GetMessage(&msg, NULL, 0, 0)) {
    TranslateMessage(&msg);

    DispatchMessage(&msg);
  }
  return (int) msg.wParam;
}

LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
   
  switch(msg)  
  {
      case WM_CREATE:

          ghwndEdit = CreateWindowEx(WS_EX_RIGHTSCROLLBAR, TEXT("edit"), NULL,    
                        WS_VISIBLE | WS_CHILD | WS_HSCROLL | WS_VSCROLL | ES_MULTILINE,
                        0, 0, 260, 180,        
                        hwnd, (HMENU) 1, NULL, NULL);    




	   CreateMenubar(hwnd);
	   break;

      case WM_SIZE:
          SetWindowPos(ghwndEdit, NULL, 0, 0, LOWORD(lParam), HIWORD(lParam),
             SWP_NOMOVE | SWP_NOZORDER);
             

          break;

      case WM_COMMAND:
	   if (wParam==IDM_FILE_NEW) {
              OpenDialog(hwnd);
	    }
	    
	    	    
	    	   if (wParam==IDM_FILE_QUIT) {
	    
           int msgboxID = MessageBox(hwnd, TEXT("Are you sure you want to quit?"), TEXT("Quit?"), MB_YESNO | MB_ICONQUESTION);
    switch (msgboxID)
    {
    case IDYES:
        PostQuitMessage(0);
        break;

    }

    return msgboxID;




          break;
}
          break;

      case WM_DESTROY:
          PostQuitMessage(0);
          break;

    

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

void CreateMenubar(HWND hwnd) {
  HMENU hMenubar;
  HMENU hMenu;
  HMENU hMenu2;

  hMenubar = CreateMenu();
  hMenu = CreateMenu();
  AppendMenu(hMenubar, MF_POPUP, (UINT_PTR)hMenu, TEXT("&File"));
  AppendMenu(hMenu, MF_STRING, IDM_FILE_NEW, TEXT("&Open"));
  AppendMenu(hMenu, MF_STRING, IDM_FILE_QUIT, TEXT("&Quit"));
  
  hMenu2 = CreateMenu();
  AppendMenu(hMenubar, MF_POPUP, (UINT_PTR)hMenu2, TEXT("&Edit"));
  AppendMenu(hMenu2, MF_STRING, IDM_EDIT_UNDO, TEXT("&Undo"));
  AppendMenu(hMenu2, MF_STRING, IDM_EDIT_REDO, TEXT("&Redo"));
  
  SetMenu(hwnd, hMenubar);
}

void OpenDialog(HWND hwnd) 
{
  OPENFILENAME ofn;
  TCHAR szFile[MAX_PATH];

 
  ZeroMemory(&ofn, sizeof(ofn));
  ofn.lStructSize = sizeof(ofn);
  ofn.lpstrFile = szFile;
  ofn.lpstrFile[0] = '\0';
  ofn.hwndOwner = hwnd;
  ofn.nMaxFile = sizeof(szFile);
  ofn.lpstrFilter = TEXT("All files(*.*)\0*.*\0");
  ofn.nFilterIndex = 1;
  ofn.lpstrInitialDir = NULL;
  ofn.lpstrFileTitle = NULL;
  ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
  

  if(GetOpenFileName(&ofn))
      LoadFile(ofn.lpstrFile);
}

void LoadFile(LPSTR file)
{
  HANDLE hFile;
  DWORD dwSize;
  DWORD dw;

  LPBYTE lpBuffer = NULL;

  hFile = CreateFile(file, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
  dwSize = GetFileSize(hFile, NULL);
  lpBuffer = (LPBYTE) HeapAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS, dwSize + 1);
  ReadFile(hFile, (LPWSTR)lpBuffer, dwSize, &dw, NULL);
  CloseHandle(hFile);
  lpBuffer[dwSize] = 0;
  SetWindowText(ghwndEdit, (LPSTR) lpBuffer);
  HeapFree(GetProcessHeap(), 0, lpBuffer);
}
What exactly is your question? What are you having trouble with? If you are more specific, you will get a more expedient solution.
Last edited on
closed account (zb0S216C)
Mike1 wrote:
I'm using Dev-C++ right now.

Oh, god. Please tell me your joking.

As for the code, you need to add the WM_CLOSE event to your procedure. This event handles the application termination when the X is activated. For example:

1
2
3
4
5
6
7
8
9
// This is inside your procedure.
switch( msg )
{
    WM_CLOSE:
        MessageBox( HWND_DESKTOP, "Are you sure you want to quit?", "Confirmation", MB_OKCANCEL | WS_POPUP );
        break;

    // Other events...
}


Wazzak
Last edited on
@Mike1 Here is an article about why DevC++ is a bad choice, and some alternatives:
http://www.cplusplus.com/forum/articles/36896/
Xander, I'm just wondering how I could have a messagebox appear when the X button is clicked and if No or Cancel is clicked it doesn't close the program.

Thanks Framework, works exactly how I wanted.

I'll try out some of the IDEs today Xander.

Thanks.

1 more thing, when I have it check if Cancel is clicked, how can I have it not close the program?

Edit

Nevermind, found out what I had to do.

Had to put "return 1;"
Last edited on
Topic archived. No new replies allowed.