Platform SDK: DirectX

Handle Window Resizing

[Visual Basic]

The information in this section pertains only to applications written in C and C++. See Direct3D Immediate Mode Visual Basic Tutorials.

[C++]

Any DirectX application that runs in a window must respond to any WM_SIZE messages that the system sends. The render target surface is usually kept as small as possible to conserve memory, and the smallest size is usually the size of the window client area. When window size increases, you must destroy the render target surface and the associated objects and re-create it at an appropriate size. Technically, an application could do this only when the window gets larger, and respond to situations when window size decreases by adjusting the viewport and decreasing the size of the blit accordingly. For simplicity, this tutorial destroys and re-creates the objects it uses whenever it receives a WM_SIZE message:

         .
         .
         .
        case WM_SIZE:
            // Check to see if we are losing or gaining our window.
            // Set the active flag to match.
            if( SIZE_MAXHIDE==wParam || SIZE_MINIMIZED==wParam )
                g_bActive = FALSE;
            else g_bActive = TRUE;
 
            // A new window size will require a new back buffer size. The
            // easiest way to achieve this is to release and re-create
            // everything. Note: if the window gets too big, we may run out
            // of video memory and need to exit. This simple app exits
            // without displaying error messages, but a real app would
            // behave itself much better.
            if( g_bActive && g_bReady )
            {
                g_bReady = FALSE;
                if( FAILED( Cleanup3DEnvironment() ) )
                    DestroyWindow( hWnd );
                if( FAILED( Initialize3DEnvironment( hWnd ) ) )
                    DestroyWindow( hWnd );
                g_bReady = TRUE;
            }
            break;
         .
         .
         .

Note that the preceding code sets a global variable to communicate to other portions of the code that the DirectX objects in use are being invalidated. In addition, this code calls the application-defined Cleanup3DEnvironment function to destroy the objects, which is also called during application shut-down. Ending the application is discussed in Step 6: Shut Down.