Selection of Forms just after program execution

拈花ヽ惹草 提交于 2020-01-06 12:43:39

问题


My Environment: C++ Builder XE4 on Windows7 pro(32bit)

I would like to select two forms automatically just after the user execute the software.

I have two forms as follows.

  • FormStart : normally this shows up after program execution
  • FormOther : this shows up when user specify run-time-parameter (e.g. /useOther)

When FormOther is shown, FormStart is not necessary to be shown.

I added following code in FormShow() of the TFormStart

TFormStart::FormShow(TObject *Sender)
{
    if (useOther) {
        FormOther->ShowModal();
        this->Close();
    }
}

This seems work. When user close the FormOther, FormStart shows up and immediately closes. This behavior is what I expected, so O.K.

What other way can we realize the above function?

I tried the following, and had error ("You cannot change Visible in OnShow or OnHide");

So, I gave up using the following.

TFormStart::FormShow(TObject *Sender)
{
    if (userOther) {
        FormOther->Show();
        this->Hide();
    }
}

回答1:


The first form created by Application.CreateForm is the main form of the application, and when it's closed the application terminates.

To use a different form, you have to do so in the project (.dpr or .bpr) source instead. Use Project->View Source from the IDE main menu to get to it.

In delphi, it would look like this:

program Project1;

uses
  Forms, SysUtils,
  StartForm in 'StartForm.pas' {FormStart},
  OtherForm in 'OtherForm.pas' {FormOther};

{$R *.res}

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  if FindCmdLineSwitch('useOther') then
    Application.CreateForm(TFormOther, FormOther)
  else
    Application.CreateForm(TFormStart, FormStart);
  Application.Run;
end.

In c++builder, it would look like this:

#include <vcl.h>
#pragma hdrstop
#include <SysUtils.hpp>
#include <tchar.h>
//---------------------------------------------------------------------------

USEFORM("StartForm.cpp", StartForm);
USEFORM("OtherForm.cpp", OtherForm);
//---------------------------------------------------------------------------
WINAPI _tWinMain(HINSTANCE, HINSTANCE, LPTSTR, int)
{
    try
    {
        Application->Initialize();
        Application->MainFormOnTaskBar = true;
        if (FindCmdLineSwitch("useOther"))
            Application->CreateForm(__classid(TFormOther), &FormOther);
        else
            Application->CreateForm(__classid(TFormStart), &FormStart);
        Application->Run();
    }
    catch (Exception &exception)
    {
        Application->ShowException(&exception);
    }
    catch (...)
    {
        try
        {
            throw Exception("");
        }
        catch (Exception &exception)
        {
            Application->ShowException(&exception);
        }
    }
    return 0;
}

Note that modifying the project source can make maintenance difficult, as the IDE uses this for form information and dependencies. Sometimes changing it manually can cause issues.



来源:https://stackoverflow.com/questions/22187387/selection-of-forms-just-after-program-execution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!