📄 faq5.htm
字号:
<HTML>
<HEAD>
<TITLE>Start a program minimized</TITLE>
<META NAME="Author" CONTENT="Harold Howe">
</HEAD>
<BODY BGCOLOR="WHITE">
<CENTER>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH="640">
<TR>
<TD>
<H3>
Start a program minimized<BR>
</H3>
<BR>
<P>
You can call the <TT>Application->Minimize</TT> function to
minimize a program to the taskbar. If you want your program to start minimized,
then simply call <TT>Application->Minimize</TT> in the <TT>WinMain</TT> function
before the <TT>Run</TT> method is called.
</P>
<pre>
WINAPI WinMain<b>(</b>HINSTANCE<b>,</b> HINSTANCE<b>,</b> LPSTR<b>,</b> <b>int</b><b>)</b>
<b>{</b>
<b>try</b>
<b>{</b>
Application<b>-></b>Initialize<b>(</b><b>)</b><b>;</b>
Application<b>-></b>CreateForm<b>(</b><b>__classid</b><b>(</b>TForm1<b>)</b><b>,</b> <b>&</b>Form1<b>)</b><b>;</b>
Application<b>-></b>Minimize<b>(</b><b>)</b><b>;</b>
Application<b>-></b>Run<b>(</b><b>)</b><b>;</b>
<b>}</b>
<b>catch</b> <b>(</b>Exception <b>&</b>exception<b>)</b>
<b>{</b>
Application<b>-></b>ShowException<b>(</b><b>&</b>exception<b>)</b><b>;</b>
<b>}</b>
<b>return</b> <font color="blue">0</font><b>;</b>
<b>}</b>
</pre>
<H3>Note for C++Builder 1.0 users</H3>
<P>The steps that follow pertain only to C++Builder 1. If you use C++Builder 3 or newer, you can ignore the rest of this
FAQ.
</p>
<P>
The file \VCL\SYSTEM.PAS contains a function called <TT>_InitExe</TT> that runs when the program starts.
<TT>_InitExe</TT> contains this statement
</P>
<PRE>
MOV CmdShow,10 { SW_SHOWDEFAULT }
</PRE>
<P>
The global <TT>CmdShow</TT> variable determines the state of program on
startup. In theory, you could set <TT>CmdShow</TT> to <TT>SW_MINIMIZE</TT>
to start a program minimized, but because <TT>_InitExe</TT> hardcodes a
value into <TT>CmdShow</TT>, your assignment gets overwritten. Because of
this assignment in <TT>_InitExe</TT>, applications cannot begin minimized.
The best we can do is to minimize the program as soon as we get a chance
from code. The main form will flash briefly on the screen before being
minimized, but we can prevent this by telling <TT>TApplication</TT> to keep
the main form hidden when the program starts.
</P>
<B>Step 1:</B> Choose View|Project Source from the C++Builder menu so you
can edit the <TT>WinMain</TT> function. Call the <TT>Minimize</TT> method of
<TT>TApplication</TT> to minimize the program to the taskbar. Set
<TT>ShowMainForm</TT> to <TT>false</TT> to prevent the main form from
flashing on the screen. Modify <TT>WinMain</TT> as follows:
</P>
<pre>
WINAPI WinMain<b>(</b>HINSTANCE<b>,</b> HINSTANCE<b>,</b> LPSTR<b>,</b> <b>int</b><b>)</b>
<b>{</b>
<b>try</b>
<b>{</b>
Application<b>-></b>Initialize<b>(</b><b>)</b><b>;</b>
Application<b>-></b>CreateForm<b>(</b><b>__classid</b><b>(</b>TForm1<b>)</b><b>,</b> <b>&</b>Form1<b>)</b><b>;</b>
Application<b>-></b>ShowMainForm <b>=</b> <b>false</b><b>;</b>
Application<b>-></b>Minimize<b>(</b><b>)</b><b>;</b>
Application<b>-></b>Run<b>(</b><b>)</b><b>;</b>
<b>}</b>
<b>catch</b> <b>(</b>Exception <b>&</b>exception<b>)</b>
<b>{</b>
Application<b>-></b>ShowException<b>(</b><b>&</b>exception<b>)</b><b>;</b>
<b>}</b>
<b>return</b> <font color="blue">0</font><b>;</b>
<b>}</b>
</pre>
<P>
<B>Step 2:</B> Setting <TT>ShowMainForm</TT> to <TT>false</TT> has one bad
side effect. The form will not appear when the user restores the program by
clicking on the taskbar icon. You can solve this problem by creating an
<TT>OnRestore</TT> handler for the application. Open the header file for the
main form and add this prototype to your main form's class.
</P>
<pre>
<b>private</b><b>:</b> <font color="navy">// User declarations</font>
<b>void</b> <b>__fastcall</b> AppRestore<b>(</b>TObject <b>*</b>Sender<b>)</b><b>;</b>
</pre>
<P>
<B>Step 3:</B> Open the main form's CPP file and code the
<TT>AppRestore</TT> function.
</P>
<pre>
<b>void</b> <b>__fastcall</b> TForm1<b>:</b><b>:</b>AppRestore<b>(</b>TObject <b>*</b>Sender<b>)</b>
<b>{</b>
Visible <b>=</b> <b>true</b><b>;</b>
<b>}</b>
</pre>
<P>
<B>Step 4:</B> Assign the <TT>AppRestore</TT> function to the
<TT>OnRestore</TT> handler of <TT>TApplication</TT>. Make this assignment
in the constructor of the main form.
</P>
<pre>
<b>__fastcall</b> TForm1<b>:</b><b>:</b>TForm1<b>(</b>TComponent<b>*</b> Owner<b>)</b>
<b>:</b> TForm<b>(</b>Owner<b>)</b>
<b>{</b>
Application<b>-></b>OnRestore <b>=</b> AppRestore<b>;</b>
<b>}</b>
</pre>
</TD> </TR>
</TABLE>
</CENTER>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -