如有一个程序!最小化后藏在任务栏里!
很多时候都会不小心看不见,又打开了一个同样的程序!我想知道,程序打开时如何判断程序本身是否已执行??

解决方案 »

  1.   

    CreateMutex(nil, True, 'MyApp');
    if GetLastError = ERROR_ALREADY_EXISTS then
    begin
      MessageBox(0, '程序已经在运行', '我的程序', MB_ICONERROR);
    end
    else
    begin
        Application.Initialize;
        Application.CreateForm(TMainForm, MainForm);
        Application.Run;
    end;
      

  2.   

    if (CreateMutex(nil,TRUE,pChar('你的应用程序名称'))<>NULL) and (GetLastError=ERROR_ALREADY_EXISTS) then
      begin
         Application.MessageBox(pchar('系统已经运行!'),pchar('警告'),MB_OK);
         exit;
      end;
      

  3.   

    xzhifei(星级饭桶(抵制日货)·飞)
     比较好。我正在用。
      

  4.   

    :P 所见略同,写在dpr文件中,下面添加功能是若在运行让其显示出来:
    var
      mHandle, fHandle: THandle;{$R *.res}begin
      Application.Initialize;
      mHandle := Windows.CreateMutex(nil, true, 'PerRecord');
      if mHandle <> 0 then
      begin
        if GetLastError = Windows.ERROR_ALREADY_EXISTS then
        begin
          fHandle := FindWindow('TfrmLogin', nil);
          if fHandle = 0 then
            fHandle := FindWindow('TfrmPer', nil);
          if fHandle <> 0 then
          begin
            ShowWindow(fHandle, SW_SHOW);
            SetForeGroundWindow(fHandle);
          end;
          Windows.ReleaseMutex(mHandle);
          Halt;
        end;
      end;  Application.CreateForm(TdmPer, dmPer);
      Application.CreateForm(TfrmPer, frmPer);
      Application.Run;
    end.