Solved

DestroyWindow()

Posted on 2001-08-24
4
313 Views
Last Modified: 2012-06-27
Hi everyone,

I have a problem concerning destroying a window (under windows using pure windows SDK).

I have a class (CWindowClass) where I in a member function called CWindowClass::Create() register and display a window using ::RegisterClass(...) and ::CreateWindow(...). In this class I have an OK-button which when pressed shall destroy the window, so far so good...

However, I want the window to destroy it self when I get the WM_NCDESTROY (either I send it myself or it is sent when the "x"-button in the upper corner is pressed). When I try to unregister the window it (offcourse) complains that there are still windows open, but I cannot close the window before I have processed the message.... Is there any good way to solve this problem???

Following is some code to explain the problem.

Please help me, Im totally stuck in the problem...

Adam


CWindowClass::CWindowClass()
{

}

CWindowClass::Create()
{
    WNDCLASS wcWinProp;

     GetClassInfo(g_hInst, "Button", &wcWinProp); /*g_hInst is a global variable holding the instance*/

    ...
    /*Change some properties for "Button" including name*/
    wcWinProp.lpszClassName = "TestWindow";
    ...

    /*Register the class*/
     if(::RegisterClass(&wcWinProp) == NULL) {
        DWORD dwErrorCode = GetLastError();

        ::MessageBox(NULL, "Class registration failed.", "Fatal Error", MB_OK);
        return false;
     }

    /*Create the window*/
    m_hwnd = ::CreateWindow(...);

    ::ShowWindow(m_hwnd, SW_SHOW);

    return true;
}


BOOL CWindowClass::OnReceiveMessage(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam){

...
...
   switch(uMsg) {

...

        case WM_NCDESTROY:
            ...
             if(::UnregisterClass("TestWindow", g_hInst) == NULL){
                    DWORD dwErrorCode = GetLastError();
                  ::MessageBox(NULL, "Class deregistration failed.", "Fatal Error", MB_OK);
             }
            ...
            return FALSE;
...
}
0
Comment
Question by:adamlundqvist
4 Comments
 
LVL 6

Expert Comment

by:MichaelS
Comment Utility
>When I try to unregister the window
why you want to do that?

any way on your wm_close you can call a member or CWindowClass which will destroy window and unregister class.

wm_close:
pWindowClass->DestroyWindow(hwnd);


CWindowClass::DestroyWindow(HWND hwnd)
{    ::DestroyWindow(hwnd);
     Unre.....();
}
0
 
LVL 2

Expert Comment

by:abesoft
Comment Utility
The problem is that you are trying to unregister the window class before the window is actually destroyed. The window (or at least the class handler) still exists while you are processing the message. What you probably want to do is to move the "Unregister" code out to a higher level.

Either put it in your message pump, so that when your app terminates it can unregister the window, or move it into your class's destructor (where it belongs, IMO).
0
 

Expert Comment

by:ken_ken
Comment Utility
yep
0
 
LVL 49

Accepted Solution

by:
DanRollins earned 50 total points
Comment Utility
There is usually no need to call ::UnregisterClass()  because all classes registered by an application are automatically unregistered when that app terminates.  The overhead of a class registration is miniscule on the grand scheme of things.  

Really... it just isn't done.  Just remove the UnregisterClass call and your code will work.

If you just can't let go... then do the unregister in the destructor after the Window (the actual HWND) is destroyed.  And if there is an error, ignore that error.

-- Dan
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

743 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

17 Experts available now in Live!

Get 1:1 Help Now