2014-03-05 47 views
0

我的环境:Windows7专业版(32位)上的C++ Builder XE4刚刚在程序执行后选择表格

我想在用户执行软件后自动选择两种表单。

我有两种形式如下。

  • FormStart:通常这显示了程序执行后
  • FormOther:这显示了当用户指定运行时间参数(例如,/ useOther)

当FormOther所示,FormStart是不需要显示。

我FormShow的TFormStart

添加以下代码()
TFormStart::FormShow(TObject *Sender) 
{ 
    if (useOther) { 
     FormOther->ShowModal(); 
     this->Close(); 
    } 
} 

这似乎工作。 当用户关闭FormOther时,FormStart显示并立即关闭。这种行为是我的期望,所以O.K.

还有什么方法可以实现上述功能?

我试了下面的错误(“你不能在OnShow或OnHide中改变Visible”);

所以,我放弃了使用以下内容。

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

回答

3

通过Application.CreateForm创建的第一个形式是应用程序的主要形式,并且当它关闭在应用程序终止。

要使用不同的表单,您必须在项目(.dpr或.bpr)源代码中执行此操作。使用IDE主菜单中的Project->View Source即可。

,它应该是这样的:

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. 

,它应该是这样的:

#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; 
} 

注意修改项目源可以使维护困难的,因为IDE将其用于表单信息和依赖关系。有时手动更改可能会导致问题。

+0

非常感谢您的回复。了解你选择表格的方式很有意思。正如你写的,我修改.dpr文件进行进一步维护时必须小心。 – sevenOfNine

+0

delphi中的代码对我来说不成问题。非常感谢你。 – sevenOfNine

+0

我添加了一个C++示例,以防万一有人遇到此答案并且不理解Delphi。 –

相关问题