Solved

DestroyWindow()

Posted on 2001-08-24
4
345 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: 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!

Question has a verified solution.

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

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…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
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.

630 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