faq10.htm

来自「C++builder学习资料C++builder」· HTM 代码 · 共 98 行

HTM
98
字号


<HTML>

<HEAD>

   <TITLE>Create a captionless form</TITLE>

   <META NAME="Author" CONTENT="Harold Howe">

</HEAD>

<BODY BGCOLOR="WHITE">



<CENTER>

<TABLE  BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH="640">



<TR>

<TD>







<H3>

Create a captionless form

</H3>

<BR>

<P>

You can remove a form's caption by setting <TT>BorderStyle</TT> to

<TT>bsDialog</TT> and overriding the <TT>CreateParams</TT> function of the form.

Inside <TT>CreateParams</TT>, you clear the <TT>WS_CAPTION</TT> bits from

<TT>Params.Style</TT>.

</P>

<P>

<B>Step 1:</B> Add the <TT>CreateParams</TT> prototype to the form class.

</P>

<pre>

    <b>private</b><b>:</b>

        <b>void</b> <b>__fastcall</b> CreateParams<b>(</b>TCreateParams <b>&</b>Params<b>)</b><b>;</b>

</pre>

<P>

<B>Step 2:</B> Code the function.

</P>

<pre>

    <b>void</b> <b>__fastcall</b> TForm1<b>:</b><b>:</b>CreateParams<b>(</b>TCreateParams <b>&</b>Params<b>)</b>

    <b>{</b>

        TForm<b>:</b><b>:</b>CreateParams<b>(</b>Params<b>)</b><b>;</b>  <font color="navy">// call base class first</font>

        Params<b>.</b>Style <b>&=</b> <b>~</b>WS_CAPTION<b>;</b>  <font color="navy">// then clear caption bit</font>

    <b>}</b>

</pre>

<BR>

<P>

<B>Note:</B> <TT>WS_CAPTION</TT> is defined in \INCLUDE\WINRESRC.H as

</P>

<pre>

    <font color="green">#define WS_CAPTION    0x00C00000L <font color="navy">/* WS_BORDER | WS_DLGFRAME */</font></font>

</pre>

<P>

In Windows 3.X, using <TT>WS_CAPTION</TT> 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 <TT>WS_CAPTION</TT> 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 <TT>WS_DLGFRAME</TT> portion in conjunction

with utilizing the <TT>WS_POPUP</TT> 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.

</P>

<P>

<TT>TForm::CreateParams</TT> contains these statements:

<pre>

     <b>case</b> bsDialog<b>:</b>

       Params<b>.</b>Style   <b>|=</b> WS_POPUP            <b>|</b> WS_CAPTION<b>;</b>

       Params<b>.</b>ExStyle <b>|=</b> WS_EX_DLGMODALFRAME <b>|</b> WS_EX_WINDOWEDGE<b>;</b>

</pre>

This combination produces a window with a border and a dialog frame

whenever you specify <TT>bsDialog</TT> as the <TT>BorderStyle</TT>.

We clear <TT>WS_CAPTION</TT> when we override <TT>CreateParams</TT>,

but the form retains its border because of the assignment to the

<TT>ExStyle</TT>. The assignment to <TT>ExStyle</TT>

does not happen when you set <TT>BorderStyle</TT> to something other than

<TT>bsDialog</TT>, which means you should stick with the <TT>bsDialog</TT> style when

utilizing the code from this FAQ.

</P>

<P><B>Note:</B> If you need a resizable, captionless form, change <TT>CreateParams</TT> like this:

</P>

<pre>

    <b>void</b> <b>__fastcall</b> TForm1<b>:</b><b>:</b>CreateParams<b>(</b>TCreateParams <b>&</b>Params<b>)</b>

    <b>{</b>

        TForm<b>:</b><b>:</b>CreateParams<b>(</b>Params<b>)</b><b>;</b>  <font color="navy">// call base class first</font>

        Params<b>.</b>Style <b>&=</b> <b>~</b>WS_DLGFRAME<b>;</b>

        Params<b>.</b>Style <b>|=</b> WS_POPUP<b>;</b>

    <b>}</b>

</pre>

<P>

This code always works, without regard to the <TT>BorderStyle</TT> property of the form. However, it is less intuitive

than clearing the <TT>WS_CAPTION</TT> bits.

</P>               



</TD> </TR>



</TABLE>

</CENTER>

</BODY>

</HTML>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?