Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 680
  • Last Modified:

Detecting closing of a window

Hi!

I want to get a notification when user clicks on the X box at the upper right corner of the window.  Its a non-modal dialog box and it's not destroyed when the button is clicked, just hidden.

I've tried using ON_WM_SHOWWINDOW and it doesnt seem to work.  It's not WM_CLOSE either.

Any clues?

Thanks!
0
asavard
Asked:
asavard
  • 3
  • 3
1 Solution
 
jhanceCommented:
You want the notification in the process that "owns" the window getting clicked or you want it in some other process?

In the first case, your window will receive the WM_SYSCOMMAND message with wParam = SC_CLOSE.  You can handle this message and do whatever you want.

In the second case, you'll need to hook the window procedure of the window you are watching to get access to it's messages.  You'll still be looking for WM_SYSCOMMAND however...
0
 
MadYugoslavCommented:
You need to cathc message handler for WM_CLOSE message. That way is to catch both cases.
0
 
asavardAuthor Commented:
From what I've observed, WM_CLOSE isnt sent when you click the X in upper left corner.  It is sent when you do OnOK or OnCancel, or whatever.  But the X isnt sending the message.  I've tried WM_SYSCOMMAND with SC_CLOSE and it didnt work either.  Anyway, I've figured it out.  I had to use :

OnWindowPosChanged()

And then check for the flag SWP_HIDEWINDOW.  Seems like the only way to trap this (in my app at least).

So, how do I award points to myself :)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jhanceCommented:
>>But the X isnt sending the message.  I've tried WM_SYSCOMMAND with SC_CLOSE and it didnt work either.  


Then you've not coded this correctly:

Add to your MESSAGE MAP:

ON_MESSAGE(WM_SYSCOMMAND, OnSysCommand)

and the handler for the message:

LRESULT CYourDialogClassDlg::OnSysCommand(WPARAM wParam, LPARAM lParam)
{
     if(wParam == SC_CLOSE){
          AfxMessageBox("X CLICKED!!", MB_OK);
     }

     return DefWindowProc(WM_SYSCOMMAND, wParam, lParam);
}


The HIDE WINDOW is NOT what you want here.  
0
 
asavardAuthor Commented:
I did what you said bro.  I even tried once more.  For some reason, it is not working.  Now i know your answer seems good, and ill accept to rate it.  But it really isnt working in the present case.  Don't ask me why.  The HIDE_WINDOW trick seems to work fine with me, why would you say it is not what I want?
0
 
jhanceCommented:
>>have been rewarded with a grade of C.

Excuse me for trying to HELP!

{helpful comment deleted....}

Show your code!  (Of course some other expert is going to have to comment on it since I observe a strict policy of NO MORE HELP after getting a "C" grade!!)  In other words, you're on your own on this Q.
0
 
asavardAuthor Commented:
In some ways you had a good answer, but the problem is I found a solution by myself.  That leaves me with the choice of not accepting your question at all, or accepting it despite the fact that this is not what I was looking for.  So my decision was to rate it with a lower grade to keep things on balance.

Sorry if I offend you with a lower grade.  I'm trying to put up the best of both worlds here.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now