Home Articles Books Downloads FAQs Tips

Q: Create a captionless form


Answer:

You can remove a form's caption by setting BorderStyle to bsDialog and overriding the CreateParams function of the form. Inside CreateParams, you clear the WS_CAPTION bits from Params.Style.

Step 1: Add the CreateParams prototype to the form class.

    private:
        void __fastcall CreateParams(TCreateParams &Params);

Step 2: Code the function.

    void __fastcall TForm1::CreateParams(TCreateParams &Params)
    {
        TForm::CreateParams(Params);  // call base class first
        Params.Style &= ~WS_CAPTION;  // then clear caption bit
    }

Note: WS_CAPTION is defined in \INCLUDE\WINRESRC.H as

    #define WS_CAPTION    0x00C00000L /* WS_BORDER | WS_DLGFRAME */

In Windows 3.X, using WS_CAPTION got you a border and a title bar, but not a dialog frame. This meant that having a border and a dialog frame were mutually exclusive. Clearing WS_CAPTION would remove both the title and the border in a Windows 3 application. In these apps, you would remove the title bar by clearing only the WS_DLGFRAME portion in conjunction with utilizing the WS_POPUP style. Win32 offers a new set of extended windows styles. You can use extended window styles to create a window with a dialog frame, a border, and no title bar.

TForm::CreateParams contains these statements:

     case bsDialog:
       Params.Style   |= WS_POPUP            | WS_CAPTION;
       Params.ExStyle |= WS_EX_DLGMODALFRAME | WS_EX_WINDOWEDGE;
This combination produces a window with a border and a dialog frame whenever you specify bsDialog as the BorderStyle. We clear WS_CAPTION when we override CreateParams, but the form retains its border because of the assignment to the ExStyle. The assignment to ExStyle does not happen when you set BorderStyle to something other than bsDialog, which means you should stick with the bsDialog style when utilizing the code from this FAQ.

Note: If you need a resizable, captionless form, change CreateParams like this:

    void __fastcall TForm1::CreateParams(TCreateParams &Params)
    {
        TForm::CreateParams(Params);  // call base class first
        Params.Style &= ~WS_DLGFRAME;
        Params.Style |= WS_POPUP;
    }

This code always works, without regard to the BorderStyle property of the form. However, it is less intuitive than clearing the WS_CAPTION bits.



Copyright © 1997-2000 by Harold Howe.
All rights reserved.