这个程序能正确的运行,在主程序出现之前先调用另外一个窗口:
unit frm_mainU;
interface
uses
  Windows, Messages,   Controls, Forms ;
type
  Tform1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
  end;
var
  form1: Tform1;
implementation
uses frm_loginU;
{$R *.dfm}
procedure Tform1.FormCreate(Sender: TObject);
begin
  Tform2.Create(self);//.Create(self);    //创建登录窗体
end;
end.unit frm_loginU;
interface
uses
  Windows, Messages,  Classes,  Forms,
 frm_mainU,  Mask, Controls, StdCtrls, ExtCtrls;
type
  Tform2 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
  public
  end;
var
  form2: Tform2;
implementation
{$R *.dfm}
procedure Tform2.Button1Click(Sender: TObject);
begin
   form1.Show; //打开程序主窗口
      self.Close;
end;
end.program xqwy;
uses
  Forms,
  frm_mainU in 'frm_mainU.pas' {form1},
  frm_loginU in 'frm_loginU.pas' {form2};
{$R *.res}
begin
  Application.Initialize;
  Application.CreateForm(Tform1, form1);
  Application.ShowMainForm:=False;
  Application.Run;
end.
下面这个程序和上面那个程序功能一样,只是表单名和文件名不一样:unit Unit1;interfaceuses
    Windows, Messages,   Controls, Forms ;
type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;implementationuses Unit2;{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
begin
Tform2.Create(self);<============运行到此处不动了!!!!!!
end;end.
unit Unit2;interfaceuses
   Windows, Messages,  Classes,  Forms,
 unit1,  Mask, Controls, StdCtrls, ExtCtrls;
type
  TForm2 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form2: TForm2;implementation{$R *.dfm}procedure TForm2.Button1Click(Sender: TObject);
begin
  form1.Show;
  self.Close;
end;end.
program Project1;uses
  Forms,
  Unit1 in 'Unit1.pas' {Form1},
  Unit2 in 'Unit2.pas' {Form2};{$R *.res}begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.ShowMainForm:=False;//.CreateForm(TForm2, Form2);
  Application.Run;
end.请教,是什么原因造成的如此怪象,
谢!!!!!!!!!!!!!!!!!!!!!!!!!

解决方案 »

  1.   

    1. 建议检查两个版本的窗体控件的visible属性是否相同。
    2. Create Main Form -> Create Login Form -> Login Success的时候再关闭Login Form并显示Main Form,虽然常见,不过从数据流程的角度来讲并不清晰。
    个人倾向于直接在工程文件中采用如下方式实现同样的目的:
    ...
    Application.Initialize;
    loginForm:= TLoginForm.create(nil);
    loginForm.showModule;
    ...
    If loginForm.ModuleResult=XXX then
    begin
        //保存成功时候的登录相关信息
         ...
    end 
    else
    begin
        //登录失败则直接退出程序
        Application.Terminate;
        exit;
    end ;//正常的初始化代码
    ...
      

  2.   

    想请教一下,Tform2.Create(self);这一步时不运行下去了?
      

  3.   

    不是不运行下去了,是你没有写让它运行下去的代码
    要写成Form2:=TForm2.Create(Self);From2.Show;这样才能让Form2实例创建之后显示出来,要不然窗体只是在内存里创建了,但是看不见它,这样有什么用呢?
      

  4.   

    其实,你应该看一下 project-->view source
    立马就看出来了