启动一个程序,该程序启动时发现当前已经运行本程序一个实例时,自动将已运行的实例最大化。请问有什么好的处理方式,我曾经想过用网络组件来联系两个实例,但是视乎太笨了,是否有更好的方式?

解决方案 »

  1.   

    var
      myMutex:HWND;begin
      myMutex:=CreateMutex(nil,false,'hkOneCopy');
      if WaitForSingleObject(myMutex,0) = wait_TimeOut then
      begin
        Application.MessageBox('Programme is running','Duplicate');
        halt;
      end;
      Application.Initialize;
      Application.CreateForm(TMainForm, MainForm);
      Application.Run;
    end.
      

  2.   


    procedure TMainFrm.OpenForm(FormClass: TFormClass; var FormInstance; AOwner: TComponent);
    var
      i: integer;
      alived: boolean;
    begin
      alived:= false;                                                 
      for i := 0 to Screen.FormCount - 1 do
        if screen.Forms[i].Owner = Aowner then exit else
          if Screen.Forms[i].ClassType = FormClass then alived:= true;   
      if not alived then                                              
        Application.CreateForm(FormClass,FormInstance);
      ShowWindow(TForm(FormInstance).Handle,SW_SHOWMINIMIZED);        
      ShowWindow(TForm(FormInstance).Handle,SW_SHOWMAXIMIZED);        
    end;//执行
    //  OpenForm(TForm1,Form1,MainFrm);
      

  3.   

    搞错了,是指程序吖,根据Mutex来判断,跟一楼差不多,多的处理是,如果存在了,就SendMessage给那个程序执行最大化..
      

  4.   

    Prj档里写
    program Project1;uses
      Forms,
      Windows,
      Messages,
      Unit1 in 'Unit1.pas' {Form1};{$R *.res}var
      hMutex: THandle;
      h: HWND;
    const
      NameMutex = 'Global\ABCMutex';begin
      if OpenMutex(MUTEX_ALL_ACCESS, False, NameMutex) <> 0 then
      begin
        MessageBox(0, '有了', '提示', MB_OK);
        h:= FindWindow('TForm1','Form1');
        SendMessage(h, WM_SYSCOMMAND, SC_MAXIMIZE, 0);
        Application.Terminate;
      end else
      begin
        hMutex := CreateMutex(nil, False, NameMutex);
        Application.Initialize;
        Application.CreateForm(TForm1, Form1);
        Application.Run;
        CloseHandle(hMutex);
      end;
    end.