Thread: how do i close one window at a time?

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    3

    how do i close one window at a time?

    im new at sdk
    n here is my question
    i have created 2 windows with the same Windows class
    what am i trying to do is to close one window first n when the other(the second one) window is closed or Destroyed as well, then the application will terminate(WM_QUIT will not be sent until both windows have been closed)
    i know it has somthing to do with hwnd but how?
    than you.

    here is the code(when i close either window, they both close and application terminates):
    Code:
    LRESULT CALLBACK WindowProc(HWND hwnd,
    							UINT msg,
    							WPARAM wparam,
    							LPARAM lparam)
    {
    	switch(msg)
    	{
    	case WM_CREATE:
    		{
    
    			// return success
    			return(0);
    		} break;
    
    	case WM_DESTROY:
    		{
    			PostQuitMessage(0);
    
    			return(0);
    		} break;
    
    	default:break;
    
    	} 
    
    	return (DefWindowProc(hwnd, msg, wparam, lparam));
    
    } // end WindProc

  2. #2
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    Hi, I'm new at windows API too! I'm sure someone will have a better answer to this, but here's what I would do:

    When you create each window, save its handle globally (so it can be accessed in the other window's procedure too).
    Then when one window closes, simply call DestroyWindow(handle) for the other window, like this:
    Code:
    /* declare globally */
    HWND hWnd1, hWnd2;
    
    WinMain()
    {
        ...
        hWnd1 = CreateWindowEx(...);
        hWnd2 = CreateWindowEx(...);
    }
    
    WinProc1(...)
    {
        ...
        case WM_DESTROY:
        DestroyWindow(hWnd2);
        PostQuitMessage(0);
        break;
    }
    I believe there is an API function that will return a handle to a window created with a specified class, if so you can also use this function to get the handles of any windows that were created with a specified class name
    more functions here
    Last edited by @nthony; 08-01-2006 at 10:05 AM.

  3. #3
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Here is some old script that I dug up:

    Code:
    #define WIN32_LEAN_AND_MEAN
    #include <windows.h>
    /*  Declare Windows procedure  */
    LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
    /*  Make the class name into a global variable  */
    char szClassName[ ] = "wndclass1";
    /*  Make the paint variable available globally  */
    HDC hdc[3];
    /*  Make the paint variable available globally  */
    HWND wndh[3];
    /*  Main function  */
    int WINAPI WinMain( HINSTANCE hThisInstance,
                        HINSTANCE hPrevInstance,
                        LPSTR lpszArgument,
                        int nFunsterStil )
    {
        MSG messages;            /* Here messages to the application are saved */
        WNDCLASSEX wincl;        /* Data structure for the windowclass */
        /* The Window structure */
        wincl.hInstance = hThisInstance;
        wincl.lpszClassName = szClassName;
        wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
        wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
        wincl.cbSize = sizeof(WNDCLASSEX);
        /* Use default icon and mouse-pointer */
        wincl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        wincl.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
        wincl.hCursor = LoadCursor(NULL, IDC_ARROW);
        wincl.lpszMenuName = NULL;                 /* No menu */
        wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
        wincl.cbWndExtra = 0;                      /* structure or the window instance */
        /* Use Windows's default color as the background of the window */
        wincl.hbrBackground = (HBRUSH)COLOR_BACKGROUND + 1;
        /* Register the window class, and if it fails quit the program */
        if (!RegisterClassEx (&wincl))
            return 0;
        int winX = 200;
        int winY = 200;
        /* The class is registered, let's create the windows */
        wndh[0] = CreateWindowEx( 0,
                                  szClassName,
                                  "Window 1",
                                  WS_OVERLAPPEDWINDOW&~WS_THICKFRAME&~WS_MAXIMIZEBOX,
                                  50,
                                  GetSystemMetrics(SM_CYSCREEN)/2-winY/2,
                                  winX,
                                  winY,
                                  HWND_DESKTOP,
                                  NULL,
                                  hThisInstance,
                                  NULL );
        wndh[1] = CreateWindowEx( 0,
                                  szClassName,
                                  "Window 2",
                                  WS_OVERLAPPEDWINDOW&~WS_THICKFRAME&~WS_MAXIMIZEBOX,
                                  GetSystemMetrics(SM_CXSCREEN)/2-winX/2,
                                  GetSystemMetrics(SM_CYSCREEN)/2-winY/2,
                                  winX,
                                  winY,
                                  HWND_DESKTOP,
                                  NULL,
                                  hThisInstance,
                                  NULL );
        wndh[2] = CreateWindowEx( 0,
                                  szClassName,
                                  "Window 3",
                                  WS_OVERLAPPEDWINDOW&~WS_THICKFRAME&~WS_MAXIMIZEBOX,
                                  GetSystemMetrics(SM_CXSCREEN) - winX - 50,
                                  GetSystemMetrics(SM_CYSCREEN)/2-winY/2,
                                  winX,
                                  winY,
                                  HWND_DESKTOP,
                                  NULL,
                                  hThisInstance,
                                  NULL );
        /* Make the windows visible on the screen */
        ShowWindow (wndh[0], nFunsterStil);
        ShowWindow (wndh[1], nFunsterStil);
        ShowWindow (wndh[2], nFunsterStil);
        /* Get the window's DC */
        hdc[0] = GetDC(wndh[0]);
        hdc[1] = GetDC(wndh[1]);
        hdc[2] = GetDC(wndh[2]);
        /* Run the message loop. It will run until GetMessage() returns 0 */
        while (GetMessage (&messages, NULL, 0, 0))
        {
            /* Translate virtual-key messages into character messages */
            TranslateMessage(&messages);
            /* Send message to WindowProcedure */
            DispatchMessage(&messages);
            /* Check to see if all the windows are closed */
            if(!wndh[0] && !wndh[1] && !wndh[2]) break;
        }
        /* The program return-value is 0 - The value that PostQuitMessage() gave */
        return messages.wParam;
    }
    
    /*  This function is called by the Windows function DispatchMessage()  */
    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        switch (message)                  /* handle the messages */
        {
            case WM_PAINT:
            {
                PAINTSTRUCT ps;
                HDC hdc = BeginPaint(hwnd, &ps);
                SetBkMode(hdc, TRANSPARENT);
                char sz[100];
                GetWindowText(hwnd, sz, 100);
                TextOut(hdc, 65, 75, sz, strlen(sz));
                EndPaint(hwnd,&ps);
            }
            break;
            case WM_DESTROY:
            {
                if(hwnd == wndh[0]) wndh[0] = NULL;
                if(hwnd == wndh[1]) wndh[1] = NULL;
                if(hwnd == wndh[2]) wndh[2] = NULL;
            }
            break;
            default:
            return DefWindowProc(hwnd, message, wParam, lParam);
        }
        return 0;
    }
    Last edited by Queatrix; 08-01-2006 at 10:54 AM.

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    12
    try this:

    Code:
    LRESULT CALLBACK WindowProc(HWND hwnd,
    							UINT msg,
    							WPARAM wparam,
    							LPARAM lparam)
    {
    	switch(msg)
    	{
    	case WM_CREATE:
    		{
    
    			// return success
    			return(0);
    		} break;
    
    	case WM_DESTROY:
    		{
    			if(hwnd == mainWindow)PostQuitMessage(0);
    		} break;
    
    	default:break;
    
    	} 
    
    	return (DefWindowProc(hwnd, msg, wparam, lparam));
    
    } // end WindProc
    So basically that should only quit the application if the mainWindow (replace w/ hwnd of first created window) is closed. I think that is all you need. I use a seperate class to load a window in my application and just removed the PostQuitMessage function completely from its procedure.

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    I think you are asking how to only call PostQuitMessage when the last window is closed. Probably the easiest way is to use a counter:
    Code:
    static int windowsOpen = 0;
    
    LRESULT CALLBACK WindowProc(HWND hwnd,
    							UINT msg,
    							WPARAM wparam,
    							LPARAM lparam)
    {
    	switch(msg)
    	{
    	case WM_CREATE:
    		{
    			windowsOpen++;
    
    			// return success
    			return(0);
    		} break;
    
    	case WM_DESTROY:
    		{
    			if (--windowsOpen <= 0)
    			{
    				PostQuitMessage(0);
    			}
    
    			return(0);
    		} break;
    
    	default:break;
    
    	} 
    
    	return (DefWindowProc(hwnd, msg, wparam, lparam));
    
    } // end WindProc
    Ideally, the count would be stored in class memory (with SetClassLong) but for simple programs the above solution is fine.

    Another method would be to use FindWindowEx to check if anymore windows belonging to the class are open.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sending an email in C program
    By Moony in forum C Programming
    Replies: 28
    Last Post: 10-19-2006, 10:42 AM
  2. Help Needed - Window won't close
    By DZeek in forum C++ Programming
    Replies: 12
    Last Post: 03-06-2005, 07:24 PM
  3. Problem with creating new window, from another window
    By Garfield in forum Windows Programming
    Replies: 6
    Last Post: 01-11-2004, 02:10 PM
  4. inputting time in separate compilation
    By sameintheend01 in forum C++ Programming
    Replies: 6
    Last Post: 03-13-2003, 04:33 AM
  5. I apologize. Good bye.
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 05-03-2002, 06:51 PM