begin
    If   FindWindow('TForm1','SingleApp')=0   then
      Begin
        Application.Initialize;
        Application.CreateForm(Tform1,Form1);
        Application.Run;
      end
    else
      begin
         MessageBox(0,'程序已经运行,请注意系统托盘','',mb_OK);
         Application.Terminate;
      end
end.希望达到的效果:当程序运行时,如果再次激发程序,则自动弹出消息框“程序已经运行,请注意系统托盘”,当单击消息框的确定按钮后,除自动显示程序窗体Form1外,无其他操作。
我遇到的问题:当把这一组begin~end放在所有procedure之前时,因为遇到“end.”所以后面的procedure全都无效;当我把end后的“.”变成“;”时,提示“[Error]: '.' expected but ';' found”;当我把这一组begin~end放在所有procedure之后时,这组begin~end一点儿作用都起不到。
这段程序到底怎么处理啊?

解决方案 »

  1.   

    这程序个人觉得本身就有错误
    begin 
        If  FindWindow('TForm1','SingleApp')=0  then 
          Begin 
            Application.Initialize; 
            Application.CreateForm(Tform1,Form1); 
            Application.Run; 
          end 
        else 
    在判断FindWindow('TForm1','SingleApp')这个肯定不会=0的,因为这个你的程序已经运行起来,
      

  2.   

    if OpenMutex(MUTEX_ALL_ACCESS,True,'你的程序名称')>0 then
      begin
        ShowWindow(FindWindowEx(0,0,nil,'提示'), SW_RESTORE);
        exit;
      end;
    你试试可行?
      

  3.   

    就是单实例的问题吧
    我贴一段源码给你,希望对你有帮助
    在program文件中
    const         //for singleton(it must uses messages,windows)
      CM_RESTORE = WM_USER + $1000; {自定义的"恢复"消息}
      MYAPPNAME = 'PearlMIS';
    {$R *.res}
    var
      pHandle:Hwnd;
    begin
      //singleton
      pHandle := FindWindow(MYAPPNAME, NIL);        //checkout if there is an instance existing already
      if pHandle > 0 then                           //if exists
      begin
        PostMessage(pHandle, CM_RESTORE, 0, 0);     //restore it
        Exit;
      end;
      Application.Initialize;
      Application.CreateForm(TfrmControl, frmControl);
      Application.ShowMainForm:=false;
      Application.CreateForm(TfrmSplash, frmSplash);
      frmSplash.Show;
      frmSplash.Update;
      Sleep(1500);
      frmSplash.Hide;
      frmSplash.Free;  
      Application.ShowMainForm:=true;
      Application.Run;
    end.
    在主窗体中需要添加两个消息处理
    const   //for singleton (it must uses shellAPI)
      CM_RESTORE = WM_USER + $1000; {自定义的"恢复"消息}
      MYAPPNAME = 'PearlMIS';    procedure CreateParams(var Params: TCreateParams); override;
        Procedure RestoreRequest(var message: TMessage); message CM_RESTORE;procedure TfrmMain.CreateParams(var Params: TCreateParams);
    begin
      inherited CreateParams(Params);
      Params.WinClassName := MYAPPNAME;
    end;procedure TfrmMain.RestoreRequest(var message: TMessage);
    begin
      if IsIconic(Application.Handle) = TRUE then
        Application.Restore
      else
        Application.BringToFront;
    end;
      

  4.   


     var
       MHandle : Cardinal;
    begin
       MHandle := CreateMutex(nil,true,'DataTransfer');
       if GetLastError = ERROR_ALREADY_EXISTS  then
       begin
         Application.MessageBox(pchar('DataTransfer Application is Running!!!!'),'Warning',MB_OK+MB_ICONERROR);
         Application.Terminate;
         exit;
       end;
      

  5.   

    begin 
        If  FindWindow('TForm1','SingleApp')=0  then 
          Begin 
            Application.Initialize; 
            Application.CreateForm(Tform1,Form1); 
            Application.Run; 
          end 
        else 
          begin 
            MessageBox(0,'程序已经运行,请注意系统托盘','',mb_OK); 
            Application.Terminate; 
          end
    end. else begin ...end后面少了一个分号
      

  6.   


    怎么建啊???
    还是不知道怎么处理这段程序和procedure之间的关系,能给个完整的程序么,从uses开始的???
    我这不是单实例啊。
      

  7.   

    互斤对象:
    unit PrevInst;interfaceuses
      Windows, Classes, SysUtils;type
      TMethod = ( mdAuto, mdManual );
      TMethodStr = array [0..1] of string;  TPrevInst = class(TObject)
      private
        { Private declarations }
        FhSem: THandle; // Semaphore handle
      protected
        { Protected declarations }
      public
        { Public declarations }
        destructor Destroy(); override;
        function CreateAtom(Semaphore: string): THandle;
        procedure FreeAtom();
      end;implementationdestructor TPrevInst.Destroy;
    begin
      if FhSem <> 0 then
        CloseHandle(FhSem);
      inherited;
    end;procedure TPrevInst.FreeAtom();
    begin
      if FhSem <> 0 then
      begin
        CloseHandle(FhSem);
        FhSem := 0;
      end;
    end;function TPrevInst.CreateAtom(Semaphore: string): THandle; // return handle
    var  // return 0 if found prev instance
        thSem: THandle;
    //    hWndMe, hWndPrev: HWnd;
        SemName: array [0..255] of Char;
    begin
        StrPCopy(SemName, Semaphore);
        Result := 0;//fail
        thSem := CreateSemaphore(nil, 0, 1, semName);
        if thSem <> 0 then
          if GetLastError() = ERROR_ALREADY_EXISTS then
          begin
            CloseHandle(thSem);
            Result := 0;//fail
          end
          else
          begin
            FhSem := thSem;
            Result := thSem;//success
          end;
    end;end.
    用法:
    var
      prev: TPrevInst;begin
      prev := TPrevInst.Create();
      if prev.CreateAtom('TNetBarCltSema') = 0 then
      begin
        // 已经运行
      end
      else
      begin
        // 还没运行
      end
    end.
      

  8.   

    http://download.csdn.net/source/880103
    现成的演示互斥的例子
      

  9.   

    在主窗口中:var
      frmMain: TfrmMain;
      Mutex: HWnd; {全局变量,钩子作用}function CreateMutex: Boolean;
    procedure DestroyMutex;implementationfunction CreateMutex: Boolean;
    var
      PrevInstHandle: THandle;
      AppTitle: Pchar;
    begin
      AppTitle := StrAlloc(100);
      StrPCopy(AppTitle, Application.Title);
      Result := True;
      Mutex := Windows.CreateMutex(nil, False, AppTitle);
      if (GetLastError = ERROR_ALREADY_EXISTS) or (Mutex = 0) then
      begin
        Result := False;
        SetWindowText(Application.Handle, '');
        PrevInstHandle := FindWindow(nil, AppTitle);
        if PrevInstHandle <> 0 then
        begin
          if IsIconic(PrevInstHandle) then
            ShowWindow(PrevInstHandle, SW_RESTORE)
          else
            BringWindowToTop(PrevInstHandle);
          SetForegroundWindow(PrevInstHandle);
        end;    if Mutex <> 0 then
          Mutex := 0;
      end;  StrDispose(AppTitle);end;procedure DestroyMutex;
    begin
      if Mutex <> 0 then
        CloseHandle(Mutex);
    end;program 中begin
      Application.Initialize;
      Application.Title := '你的标题';
      if CreateMutex then
      begin
        Application.CreateForm(TfrmMain, frmMain);
        Application.Run;
      end
      else
      begin
        //    Application.Messagebox('本程序正在运行当中,请检查任务栏!', '提示', MB_OK + MB_ICONINFORMATION); //提示程序已运行
        DestroyMutex;
      end;
    代码是通用的,复制到你想要的程序里即可
      

  10.   

    在program里
    var
      hMutex: HWND;
      Ret: Integer;begin
      Application.Initialize;  
      Application.Title := '标题;
      hMutex := CreateMutex(nil, False, '标题');
      Ret := GetLastError;
      if Ret = ERROR_ALREADY_EXISTS then
      begin
        showmessage('提示的信息');
        ReleaseMutex(hMutex);
        Application.Terminate;
        exit;
      end;
      Application.CreateForm(TForm1, Form1);
      Application.Run;
    end.
    uses里加Dialogs;
      

  11.   

    其实程序逻辑上没有问题,主要是
    If  FindWindow('TForm1','SingleApp')=0  then 
    中'SingleApp'应该为'Form1',
    我已经验证过