wwwtc3.htm

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

HTM
96
字号


<HTML>

<HEAD>

   <TITLE> What's Wrong With This Code? Volume #3</TITLE>

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

</HEAD>

<BODY>



<CENTER>

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

<TR>



<TD>







<H2>

What's Wrong With This Code? Volume #3

</H2>



<H4>

The hidden danger of overriding virtual pascal base functions

</H4>

<P>

The following code overrides the virtual <TT>CreateParams</TT> function to specify the window class name of a utility

form. The window class name is passed to the form in the constructor, and then stored away in a private variable.

<TT>CreateParams</TT> then uses the private string variable to intialize the <TT>WinClassName</TT> member of the

<TT>TCreateParams</TT> structure

</P>

<pre>

<font color="navy">//---------------------------------------------------------------</font>

<font color="navy">// form header file</font>

<b>class</b> TForm2 <b>:</b> <b>public</b> TForm

<b>{</b>

<b>__published</b><b>:</b>

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

    AnsiString m_WndClassName<b>;</b>

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

<b>public</b><b>:</b>

    <b>__fastcall</b> TForm2<b>(</b>TComponent<b>*</b> Owner<b>,</b>

                      <b>const</b> AnsiString <b>&amp;</b>WndClassName <b>=</b> <font color="blue">&quot;TForm2&quot;</font><b>)</b><b>;</b>

<b>}</b><b>;</b>



<font color="navy">//---------------------------------------------------------------</font>

<font color="navy">// form cpp file</font>

<b>__fastcall</b> TForm2<b>:</b><b>:</b>TForm2<b>(</b>TComponent<b>*</b> Owner<b>,</b>

                          <b>const</b> AnsiString <b>&amp;</b>WndClassName<b>)</b>

    <b>:</b> TForm<b>(</b>Owner<b>)</b><b>,</b>

      m_WndClassName<b>(</b>WndClassName<b>)</b>

<b>{</b>

<b>}</b>

<font color="navy">//---------------------------------------------------------------</font>

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

<b>{</b>

    TForm<b>:</b><b>:</b>CreateParams<b>(</b>Params<b>)</b><b>;</b>

    strcpy<b>(</b>Params<b>.</b>WinClassName<b>,</b>m_WndClassName<b>.</b>c_str<b>(</b><b>)</b><b>)</b><b>;</b>

<b>}</b>



<font color="navy">//---------------------------------------------------------------</font>

<font color="navy">// showing TForm2 from the main form</font>

<b>void</b> <b>__fastcall</b> TForm1<b>:</b><b>:</b>Button1Click<b>(</b>TObject <b>*</b>Sender<b>)</b>

<b>{</b>

    TForm2 <b>*</b>form <b>=</b> <b>new</b> TForm2<b>(</b><b>this</b><b>,</b> <font color="blue">&quot;MotherPumpkin&quot;</font><b>)</b><b>;</b>

    form<b>-&gt;</b>ShowModal<b>(</b><b>)</b><b>;</b>

<b>}</b>

</pre>



<P>

On the surface, it looks like this should work. However, when we run the program, an <TT>EWin32Error</TT> exception is

raised during the creation of the <TT>TForm2</TT> object. The error message box is shown in Figure 1.

</P>

<BR>

<IMG SRC="images/wndclasserror.gif" BORDER=0 ALIGN="BOTTOM" width="389" height="119">  <BR>

<H4>Figure 1. EWin32Error</H4>

<P>

Can you find out what caused the error?

</P>



<H4>

<A TARGET=_top href="wwwtc3answer.htm">Answer</A>

</H4>

<BR>

<TABLE  BORDER=1 CELLPADDING=10 CELLSPACING=0 WIDTH="100%">

<TR> <TD colspan = 2><B>Code for this edition</B> </TD> </TR>

<TR> <TD><TT><a HREF="download/wwwtc3.zip">wwwtc3.zip</a></TT></TD><TD>BCB4 project that contains problem code.</TD> </TR>

<TR> <TD><TT><a HREF="download/wwwtc3x.zip">wwwtc3x.zip</a></TT></TD><TD>Same project, includes an EXE (188 kb).</TD> </TR>

</TABLE>





</TD> </TR>



</TABLE>

</CENTER>

</BODY>

</HTML>

⌨️ 快捷键说明

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