Home Articles Books Downloads FAQs Tips

Q: Start a program minimized


Answer:

You can call the Application->Minimize function to minimize a program to the taskbar. If you want your program to start minimized, then simply call Application->Minimize in the WinMain function before the Run method is called.

WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
    try
    {
        Application->Initialize();
        Application->CreateForm(__classid(TForm1), &Form1);
        Application->Minimize();
        Application->Run();
    }
    catch (Exception &exception)
    {
        Application->ShowException(&exception);
    }
    return 0;
}

Note for C++Builder 1.0 users

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.

The file \VCL\SYSTEM.PAS contains a function called _InitExe that runs when the program starts. _InitExe contains this statement

        MOV     CmdShow,10      { SW_SHOWDEFAULT }

The global CmdShow variable determines the state of program on startup. In theory, you could set CmdShow to SW_MINIMIZE to start a program minimized, but because _InitExe hardcodes a value into CmdShow, your assignment gets overwritten. Because of this assignment in _InitExe, 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 TApplication to keep the main form hidden when the program starts.

Step 1: Choose View|Project Source from the C++Builder menu so you can edit the WinMain function. Call the Minimize method of TApplication to minimize the program to the taskbar. Set ShowMainForm to false to prevent the main form from flashing on the screen. Modify WinMain as follows:

WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
    try
    {
        Application->Initialize();
        Application->CreateForm(__classid(TForm1), &Form1);
        Application->ShowMainForm = false;
        Application->Minimize();
        Application->Run();
    }
    catch (Exception &exception)
    {
        Application->ShowException(&exception);
    }
    return 0;
}

Step 2: Setting ShowMainForm to false 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 OnRestore handler for the application. Open the header file for the main form and add this prototype to your main form's class.

private:	// User declarations
    void __fastcall AppRestore(TObject *Sender);

Step 3: Open the main form's CPP file and code the AppRestore function.

void __fastcall TForm1::AppRestore(TObject *Sender)
{
    Visible = true;
}

Step 4: Assign the AppRestore function to the OnRestore handler of TApplication. Make this assignment in the constructor of the main form.

__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
    Application->OnRestore = AppRestore;
}


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