就是判断如果这个程序如果已经在运行了,则新运行的程序就退出。

解决方案 »

  1.   

    HANDLE Mutex ;
    bool CheckPrevInst(void)            則關閉
    {
        HWND PrevWnd;
        Mutex = CreateMutex(NULL, false, "ProgramHMutex");
        if(Mutex!=0){
            if(GetLastError()==ERROR_ALREADY_EXISTS){
                PrevWnd = FindWindow("TMainForm",NULL);
                if(PrevWnd!=0){
                    PrevWnd = GetWindow(PrevWnd,GW_OWNER);
                    if(PrevWnd!=0){
                        if(IsIconic(PrevWnd))
                            ShowWindow(PrevWnd,SW_SHOWNORMAL);
                        else
                            SetForegroundWindow(PrevWnd);
                        return true;
                    }
                }
            }
        }
        return false;
    }
      

  2.   

    <1>修改工程的代码,如下:
    program Project1;
    uses
    Forms,Windows,
    Unit1 in 'Unit1.pas' {Form1};
    {$R *.RES}
    begin
    CreateMutex(Nil,False,'MyApp');//企图创建一个名为MyApp命名互斥体
    if GetLastError=ERROR_ALREADY_EXISTS Then//如果失败则表明另一个程序已经运行
    begin
    //向自定义这些消息的所有窗体广播消息,仅仅自己的程序可以响应它,并将它恢复过来
    SendMessage(HWND_BROADCAST,RegisterWindowMessage('MyApp'),//定义了独一无二的系统消息
    0,0);
    Halt(0);//退出
    end;
    Application.Initialize;
    Application.CreateForm(TForm1, Form1);
    Application.Run;
    end.
    <2>主窗体单元文件
    unit Unit1;
    interface
    uses
    Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
    type
    TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    private
    { Private declarations }
    public
    { Public declarations }
    end;
    var
    Form1: TForm1;
    implementation
    {$R *.DFM}
    var
    OldWindowProc:Pointer;//老窗体过程变量
    MyMsg:DWord;//定制系统消息
    function 
    NewWindowProc(WindowHandle:hWnd;TheMessage:LongInt;ParamW:LongInt;
    ParamL:LongInt):LongInt 
    stdcall;
    begin
    if TheMessage=MyMsg then begin
    //表明程序已被恢复并让其恢复窗体
    SendMessage(Application.handle,WM_SYSCOMMAND,SC_RESTORE,0);
    SetForegroundWindow(Application.handle);
    BringWindowToTop(form1.handle);
    setactivewindow(form1.handle);
    //已经处理完这个消息
    result:=0;
    end;
    //调用先前的窗体过程
    result:=CallWindowProc(OldWindowProc,
    WindowHandle,
    TheMessage,
    ParamW,
    ParamL);
    end;
    procedure TForm1.FormCreate(Sender: TObject);
    begin
    //登记定制的系统消息
    MyMsg:=RegisterWindowMessage('MyApp');
    //设置窗体过程,并将老的窗体过程存储起来
    OldWindowProc:=Pointer(SetWindowLong(form1.handle,GWL_WNDPROC,LongInt(@NewWindowProc)));
    end;
    procedure TForm1.FormDestroy(Sender: TObject);
    begin
    //将FORM1的窗体过程设置为原先的过程
    SetWindowLong(Form1.handle,GWL_WNDPROC,LongInt(OldWindowProc));
    end;
    end.
      

  3.   

    这是我的一段代码,放在启动窗体的create事件中。
    还要在最上面定义一个常量,这是限定运行实例的个数。
    const
      max_app=1;
    procedure TFlogon.FormCreate(Sender: TObject);
    var
      sem:integer;
      win:hwnd;
      bre:integer;
    begin
      Application.CreateForm(TDM1, DM1);
      sem:=createsemaphore(nil,0,max_app,'only');
      if sem=0 then
        exit;
      if not releasesemaphore(sem,1,nil) then
        begin
          application.MessageBox(pchar('程序正在运行中。'),'提示信息',MB_ICONinformation+mb_ok);
          application.Terminate;
        end;
    end;