Solved

Detecting closing of a window

Posted on 2001-06-14
7
671 Views
Last Modified: 2013-11-20
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
Comment
Question by:asavard
  • 3
  • 3
7 Comments
 
LVL 32

Expert Comment

by:jhance
ID: 6193207
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
 
LVL 2

Expert Comment

by:MadYugoslav
ID: 6194103
You need to cathc message handler for WM_CLOSE message. That way is to catch both cases.
0
 

Author Comment

by:asavard
ID: 6195488
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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 32

Accepted Solution

by:
jhance earned 50 total points
ID: 6195571
>>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
 

Author Comment

by:asavard
ID: 6195729
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
 
LVL 32

Expert Comment

by:jhance
ID: 6195950
>>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
 

Author Comment

by:asavard
ID: 6196161
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ODBC Connection Logging, ADO.NET 6 64
Expand to include initial dialog with two choices. 9 76
viewing source code from eclipse 13 103
sum67 challenge 35 97
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now