Solved

DestroyWindow()

Posted on 2001-08-24
4
327 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
ID: 6421418
>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
ID: 6422813
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
ID: 6422933
yep
0
 
LVL 49

Accepted Solution

by:
DanRollins earned 50 total points
ID: 6425955
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

832 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