When program close

Hi, I want to know when closing the program can make any command before close ? Is there any function or something similar? Can you help me ?

Can you help me ?


Not unless you tell us what technology you are using to create the program. Windows Forms? Win32 Api? MFC? wxWidgets? QT? Something else?
I use win32.
Well, when the user clicks the little [x] thingie in the title bar to close the program, Windows sends WM_CLOSE and WM_DESTROY messages to the Window Procedure for the class of window where the click occurred. All you need to do is write a function containing valid code you want executed when either of those messages are received. When the message comes in, call the function. Do you know how to do that?
Last edited on
Please can you show me some example?
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
#ifndef UNICODE
    #define UNICODE
#endif
#ifndef _UNIODE
    #define _UNICODE
#endif
#include <windows.h>


void DoCloseOutWork(HWND hWnd)
{
 MessageBox(hWnd,L"Doing App Clean Up Work!",L"Progress Report",MB_OK);
}


LRESULT CALLBACK WindowProcedure(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
 if(message==WM_CLOSE)
 {
    DoCloseOutWork(hWnd);
    PostQuitMessage(0);
 }

 return DefWindowProc(hWnd, message, wParam, lParam);
}


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

 wc.lpszClassName=szClassName;                wc.lpfnWndProc=WindowProcedure;
 wc.cbSize=sizeof (WNDCLASSEX);               wc.style=0;
 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;    wc.cbWndExtra=0;
 wc.lpszMenuName=NULL;                        wc.cbClsExtra=0;
 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;
}
Last edited on
Thank you.
I was trying to be as concise as possible, but the code I posted might be overly so. It seems to work, but here is a more technically correct Window Procedure...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
LRESULT CALLBACK WindowProcedure(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
 switch(message)
 {
   case WM_CLOSE:
    {
        DoCloseOutWork(hWnd);
        DestroyWindow(hWnd);
        return 0;
    }
   case WM_DESTROY:
    {
        PostQuitMessage(0);
        return 0;
    }
 }

 return DefWindowProc(hWnd, message, wParam, lParam);
}


Do a search on WM_CLOSE
Depending on what you want to do, you could just call your function from WinMain() after exiting the message loop.

Andy

PS Remember the +1 for the brush color

wc.hbrBackground = (HBRUSH)(COLOR_BTNSHADOW + 1);

(if you want COLOR_BTNSHADOW)
Topic archived. No new replies allowed.