小弟做了文件关联程序,
关联问题已解决,现在的问题是在点击一个文件通过关联打开了我的程序,
而点击另一个文件,同时将我的已运行程序激活,关闭原来的文件,而打开新的文件。
我也从我的参考资料中用了一些阻止程序二次运行的例子,
但是我的程序刚起动又关闭了,
另我的程序的主窗口是MAINFORM,
而在创建窗体前加了一个封窗fm,
待所有窗口创建完成后将FM.FREE 了,
我该怎阻止程序二次运行呢?

解决方案 »

  1.   

    var
        logoForm:Tform;
        hMutex:integer;   // ***  互斥 窗口句柄
    begin
        hMutex:=CreateMutex(nil,false,'hkOneCopy');
        if WaitForSingleObject(hMutex,0)<>wait_TimeOut then
        begin
            // *** 第一次 运行!
            Application.Initialize;        LogoForm:=Tform.Create(nil);
            logoForm.Position:=poScreenCenter;
            logoForm.Caption:='请等待';
            logoForm.Show;        Application.Title := 'eIIP(Enterprise Information Integrate Platform)';
            Application.HelpFile := '帮助文件\eIIP.net.chm';
      application.Icon.LoadFromFile(extractfilepath(application.ExeName)+'images\icons\application-icon.ico');
            Application.CreateForm(TFrmMain, FrmMain);
      Application.CreateForm(TFrmLogin, FrmLogin);
      Application.CreateForm(TFrmDataConnection, FrmDataConnection);
      Application.CreateForm(TFrmViewFieldSelect, FrmViewFieldSelect);
      LogoForm.Close;
            logoForm.free;
            //logoForm.Destroy;        Application.Run;    end
        else
        begin
            // ***  已经运行
            messagebox(application.Handle,
                        '警告: 本应用系统已经在本机运行,系统不建议您打开该系统副本。'+chr(13)+chr(10)+chr(13)+chr(10)
                        +'选择【确定】退出!',
                        'eIIP(Enterprise Information Integrate Platform)',
                        MB_ICONERROR);
        end;
    end.
      

  2.   

    我来个简单一点的:program Project1;uses
      windows,
      Messages,
      system,
      Forms,
      Unit1 in 'Unit1.pas' {MainForm};var
      SendData: tagCOPYDATASTRUCT;
    {$R *.res}begin  if FindWindow('TMainForm', nil) > 0 then
      begin
        with SendData do
        begin
          dwData := 0; // may use a value do identify content of message
          cbData := SizeOf(PChar(ParamStr(1))) + 1; //Need to transfer terminating #0 as well
          lpData := PChar(ParamStr(1))
        end;
        SendMessage(FindWindow('TMainForm', nil), WM_COPYDATA, 0, integer(@SendData));
      end
      else
      begin
        Application.Initialize;
        Application.CreateForm(TMainForm, MainForm);
        Application.Run;
      end;
    end.主窗口中:
    type
      TMainForm = class(TForm)
      private
        { Private declarations }
        procedure WMCopyData(var Msg: TWMCopyData); message WM_COPYDATA;
      public
        { Public declarations }
      end;var
      MainForm: TMainForm;implementation{$R *.dfm}{ TMainForm }procedure TMainForm.WMCopyData(var Msg: TWMCopyData);
    var
      Txt: string;
    begin
      Txt := Trim(strpas(Msg.CopyDataStruct.lpData));
      if Txt = '' then//如果为空,就显示主窗口
      begin
        Application.BringToFront ;
        SetActiveWindow(Self.Handle);
      end
      else
      begin
    //否则就处理传送过来的文件
      end;
    end;
    end.
      

  3.   

    我在mainform的CREATE事件中设置了
    if paramcount<>0 then 
         begin 
         docname:=paramstr(1); 
         richedit1.Lines.LoadFromFile(paramstr(1)); 
         richedit1.Modified:=false; 
         end 
      不次二次激活了还能否再次执行该事件,
    即点击1.xyz运行程序后,
    再点击2.xyz激活程序自动关闭1.xyz,而打开2.xyz.  scywolf(勇往直前)可以搜索'编程实现文件关联'就看到结果了.
      

  4.   

    CreateMutex(nil, True, 'MyApp');
    if GetLastError = ERROR_ALREADY_EXISTS then
    begin
      MessageBox(0, '程序已经在运行', '我的程序', MB_ICONERROR);
      Halt;
    end;这段代码可以放在 .dpr 项目文件中