不可以第二次运行程序,只运行一次。

解决方案 »

  1.   

    Var Hwnd:Thandle;
    begin
      Hwnd:=FindWindow(nil,'frmmain');
      If Hwnd=0 then
      begin
      .................
      Application.Run;
      end;
    end;
      

  2.   

    大概是这样的
    *.dpr文件:program Wine;uses
      Forms,
      windows,
      messages,
      MainUnit in 'MainUnit.pas' {MainForm},{$R *.RES}
    const
      CM_RESTORE = WM_USER + $1000; {自定义的“恢复”消息}
      MYAPPNAME = 'My Delphi Program';var
      RvHandle : hWnd;
    begin
      RvHandle := FindWindow(MYAPPNAME, NIL);
      if RvHandle > 0 then begin
        PostMessage(RvHandle, CM_RESTORE, 0, 0);
        Exit;
      end;
      Application.Initialize;
      Application.Title := '酒水管理系统';
      Application.CreateForm(TMainForm, MainForm);
      Application.CreateForm(TDM, DM);
      Application.Run;
    end.
      

  3.   

    这是一个比较简单的防止程序执行两次的方法
    implementation 
    var hnd: THandle;initialization
        hnd := CreateMutex(nil, True, 'irgendwaseinmaliges');
        if GetLastError = ERROR_ALREADY_EXISTS then Halt;finalization
        if hnd <> 0 then CloseHandle(hnd);
    end.
      

  4.   

    这是我在网上拷贝的一段别人的文章:——————————————————————————实现单实例运行的关键是判断前一实例是否存在,Win3.x中运行的程序能获知前一实例的句柄,从而可以方便地进行判断,但 Windows 95 是抢先式多任务系统,其程序的前一实例句柄恒为零,所以只有另寻其他办法。目前最有效的办法是通过查看是否有相同窗口类名的例程存在来进行判断。下面介绍在Delphi中实现的方法。1、对主窗口程序的改动:在主窗口(即程序创建的第一个窗口)中interface节加入
    const
    CM_RESTORE = WM_USER + $1000; {自定义的“恢复”消息}
    MYAPPNAME = "My Delphi Program";
    并在Form的定义的public节中加入
    procedure CreateParams(var Params: TCreateParams); override;
    Procedure RestoreRequest(var message: Tmessage); message CM_RESTORE;
    在implementation节中加入
    {指定窗口名称} procedure TForm1.CreateParams(var Params: TCreateParams);
    begin
    inherited CreateParams(Params);
    Params.WinClassName := MYAPPNAME;
    end;{处理“恢复”消息}procedure TForm1.RestoreRequest(var message: Tmessage);
    begin
    if IsIconic(Application.Handle) = TRUE then
    Application.Restore
    else
    Application.BringToFront;
    end;
    经过以上修改,程序的主窗口的类名已经被指定了,这是进行判断的基础。一般在程序刚开始运行的时候进行判断,所以还要对DPR文件进行修改。2、对DPR文件的改动在 uses 节中添加 windows、messages这两个单元加入下列语句,注意两个文件中常量CM_RESTORE和MYAPPNAME的定义必须一致
    const
    CM_RESTORE = WM_USER + $1000; {自定义的“恢复”消息}
    MYAPPNAME = "My Delphi Program";
    var
    RvHandle : hWnd;
    将下列语句插到程序最前部(在Application.Initialize之前)
    RvHandle := FindWindow(MYAPPNAME, NIL);
    if RvHandle > 0 then
    begin
    PostMessage(RvHandle, CM_RESTORE, 0, 0);
    Exit;
    end;这段程序的意思是如果找到一个类名相同的窗口,则向该窗口发送一个消息,并退出,而本例中原窗口收到该消息后会自动激活或从图标还原,从而达到了避免二次运行且能自动调出前一例程的目的。
      

  5.   

    用findwindow,createmutex,globaladdatom都可以findwindow的例子
    摘帖,
    findwindow,createmutex,globaladdatom都可以在程序启动时将Application的Title特性字段的值暂时改变。 利用Windows API函数FindWindows()查找窗口 
    恢复Application的Title值 
    上述步骤一般在主Form的OnCreate事件中实现,示例如下: procedure TForm1.FormCreate(Sender: TObject); 
    var 
    ZAppName: array[0..127] of char; 
    Hold: String; 
    Found: HWND; 
    begin 
    Hold := Application.Title; 
    Application.Title := 'OnlyOne' 
    + IntToStr(HInstance); // 暂时修改窗口标题 
    StrPCopy(ZAppName, Hold); // 原窗口标题 
    Found := FindWindow(nil, ZAppName); // 查找窗口 
    Application.Title := Hold; // 恢复窗口标题 
    if Found<>0 then begin 
    // 若找到则激活已运行的程序并结束自身 ShowWindow(Found, SW_RESTORE); 
    Application.Terminate; 
    end; 
    end; 
      

  6.   

    用一个控件,一劳永逸转
    {***************************************************************}
    {              XJustOne Component for Delphi 32                 }
    { Version:     2.6                                              }
    { Author:      Gennady Makeev ([email protected])           }
    { Improvement: Aleksey Kuznetsov ([email protected])            }
    { WWW:         http://www.utilmind.com                          }
    {***************************************************************}
    { This component let start only one copy of your application.   }
    {***************************************************************}
    { PROPERTIES:                                                   }
    {   SwitchToPrevious: Boolean - Activate the previous instance  }
    {                               if exist                        }
    { EVENTS:                                                       }
    {   AlreadyExist - occurs if first instance of this application }
    {                  is already loaded                            }
    {***************************************************************}unit JustOne;interfaceuses
      Windows, SysUtils, Classes, Controls, Forms;type
      TXJustOne = class(TComponent)
      private
        FSwitchToPrevious: Boolean;
        FAlreadyExist: TNotifyEvent;
        Mutex: hWnd;
        IsCreated: Boolean;
      public
        procedure Loaded; override;
        constructor Create(aOwner: TComponent); override;
        destructor Destroy; override;
      published
        property SwitchToPrevious: Boolean read FSwitchToPrevious write FSwitchToPrevious;
        property AlreadyExist: TNotifyEvent read FAlreadyExist write FAlreadyExist;
      end;procedure Register;implementationprocedure TXJustOne.Loaded;
    var
      PrevInstHandle: THandle;
      AppTitle: Array[0..$100] of Char;
    begin
      inherited Loaded;
      
      StrPCopy(AppTitle, Application.Title);
      IsCreated := False;
      
      Mutex := CreateMutex(nil, False, AppTitle);
      if (GetLastError = ERROR_ALREADY_EXISTS) or
         (Mutex = 0) then
      begin
       if Assigned(FAlreadyExist) then FAlreadyExist(Self);   if FSwitchToPrevious then
        begin
         SetWindowText(Application.Handle, '');     PrevInstHandle := FindWindow(nil, AppTitle);
         if PrevInstHandle <> 0 then
          if IsIconic(PrevInstHandle) then
           ShowWindow(PrevInstHandle, SW_RESTORE)
          else
           BringWindowToTop(PrevInstHandle);
          SetForegroundWindow(PrevInstHandle);
        end;   Application.ShowMainForm := False;
       Application.Terminate;
      end;
      IsCreated := True;
    end;constructor TXJustOne.Create(aOwner: TComponent);
    begin
      inherited Create(aOwner);
      FSwitchToPrevious := True;
    end;destructor TXJustOne.Destroy;
    begin
      if IsCreated then
       CloseHandle(Mutex);
      inherited Destroy;
    end;procedure Register;
    begin
      RegisterComponents('UtilMind', [TXJustOne]);
    end;end.