Solved

DestroyWindow()

Posted on 2001-08-24
4
339 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Dialogbox API leak? 18 118
Unable to start eclipse ? 17 173
Outlook 13 100
Need some help with mailto 16 42
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…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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.

734 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