A Popup Window



A window is referred to as popup when it is relatively small, is equipped with a short than usual title bar, is equipped with only the System Close button on its title bar, and has thin borders. Such a window is usually used to accompany or assist another big window or the main window of an application. This means that a popup window is hardly used by itself or as the main frame of an application. Based on this description, there are some characteristics you should apply to the frame to make it appear as a popup window.

To create a popup window, it should have the WS_POPUPWINDOW and the WS_CAPTION styles. It should fit a relatively small rectangle. It may also have the WS_EX_TOOLWINDOW extended style.

In this exercise, to illustrate our point, we will create a frame-based application where a popup window is the main object.

Practical Learning Practical Learning: Introducing Tables

  1. Start your programming environment. For this example, we will use Borland C++BuilderX.
    Therefore, start Borland C++BuilderX and, on the main menu, click File -> New...
  2. In the Object Gallery dialog box, click New GUI Application and click OK
  3. In the New GUI Application Project Wizard - Step 1 of 3, in the Directory edit box of the Project Settings section, type the path you want. Otherwise, type
    C:\Programs\Win32 Programming
  4. In the Name edit box, type Popup1 and click Next
  5. In the New GUI Application Project Wizard - Step 2 of 3, accept the defaults and click Next
  6. In the New GUI Application Project Wizard - Step 3 of 3, click the check box under Create
  7. Select Untitled under the Name column header. Type Exercise to replace the name and press Tab
  8. Click Finish
  9. Display the Exercise.cpp file and change it as follows:
    #include <windows.h>
    #ifdef __BORLANDC__
      #pragma argsused
    const char *ClsName = "WndProperties";
    const char *WndName = "Popup Window";
    LRESULT CALLBACK WndProcedure(HWND hWnd, UINT uMsg,
                                  WPARAM wParam, LPARAM lParam);
    int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                         LPSTR lpCmdLine, int nCmdShow )
      MSG        Msg;
            HWND       hWnd;
            WNDCLASSEX WndClsEx;
            // Create the application window
            WndClsEx.cbSize        = sizeof(WNDCLASSEX);
            WndClsEx.style         = CS_HREDRAW | CS_VREDRAW;
            WndClsEx.lpfnWndProc   = WndProcedure;
            WndClsEx.cbClsExtra    = 0;
            WndClsEx.cbWndExtra    = 0;
            WndClsEx.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
            WndClsEx.hCursor       = LoadCursor(NULL, IDC_ARROW);
            WndClsEx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
            WndClsEx.lpszMenuName  = NULL;
            WndClsEx.lpszClassName = ClsName;
            WndClsEx.hInstance     = hInstance;
            WndClsEx.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
            // Register the application
            // Create the window object
            hWnd = CreateWindowEx(WS_EX_TOOLWINDOW,
                                  ClsName, WndName,
                                  WS_POPUPWINDOW | WS_CAPTION,
                                  200, 120, 200, 320,
                                  NULL, NULL, hInstance, NULL);
            // Find out if the window was created
            if( !hWnd ) // If the window was not created,
                    return 0; // stop the application
            // Display the window to the user
            ShowWindow(hWnd, SW_SHOWNORMAL);
            // Decode and treat the messages
            // as long as the application is running
            while( GetMessage(&Msg, NULL, 0, 0) )
            return Msg.wParam;
    LRESULT CALLBACK WndProcedure(HWND hWnd, UINT Msg,
                               WPARAM wParam, LPARAM lParam)
        // If the user wants to close the application
        case WM_DESTROY:
            // then close it
            // Process the left-over messages
            return DefWindowProc(hWnd, Msg, wParam, lParam);
        // If something was not done, let it go
        return 0;
  10. Test the application


See Also: