程序执行后立即选择表格 [英] Selection of Forms just after program execution

查看:80
本文介绍了程序执行后立即选择表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的环境:Windows7 pro(32位)上的C ++ Builder XE4



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



我有以下两种形式。




  • FormStart:通常在程序执行后显示

  • FormOther:当用户指定运行时参数(例如/ useOther)时显示。



显示FormOther时,不必显示FormStart。



我在TFormStart的FormShow()中添加了以下代码

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

这似乎有用。
当用户关闭FormOther时,FormStart出现并立即关闭。这种行为是我所期望的,所以可以



我们还可以通过其他方式实现上述功能吗?



我尝试了以下操作,并遇到了错误(您无法在OnShow或OnHide中更改可见性);



因此,我放弃了以下操作

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


解决方案

Application.CreateForm 创建的第一个表单是应用程序的主要表单,当它关闭时,应用程序终止。



要使用其他格式,您必须在项目(.dpr或.bpr)源中使用。在IDE主菜单中使用 Project-> View Source 进行访问。



,看起来像这样:

 程序Project1; 

在$ StartForm.pas {FormStart}中使用
Forms,SysUtils,
StartForm在 OtherForm.pas {FormOther}中使用
OtherForm;

{$ R * .res}

开始
Application.Initialize;
Application.MainFormOnTaskbar:=真;
,如果FindCmdLineSwitch(’useOther’),则
Application.CreateForm(TFormOther,FormOther)
else
Application.CreateForm(TFormStart,FormStart);
Application.Run;
结尾。

,看起来像这样:



< pre class = lang-cpp prettyprint-override> #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)
{
试试
{
Application-> Initialize();
Application-> MainFormOnTaskBar = true;
if(FindCmdLineSwitch( useOther))
Application-> CreateForm(__ classid(TFormOther),& FormOther);
else
Application-> CreateForm(__ classid(TFormStart),& FormStart);
Application-> Run();
}
捕获(异常和异常)
{
Application-> ShowException(& exception);
}
catch(...)
{
try
{
throw Exception();
}
捕获(异常和异常)
{
Application-> ShowException(& exception);
}
}
返回0;
}

请注意,修改项目源可能会使维护变得困难,因为IDE会使用它用于表单信息和依赖项。有时手动更改它可能会导致问题。


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();
    }
}

解决方案

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 , 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 , 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.

这篇关于程序执行后立即选择表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆