var
  hMutex : Thandle;
  WaitResult : word;
  BroadcastList : DWORD;
begin
     MessageID := RegisterWindowMessage('Check For Choice Previous Inst');
// register a message to use later on
     hMutex := createMutex(nil,false,pchar('App_Choice')); // grab a mutex
handle
     WaitResult := WaitForSingleObject(hMutex,10); // wait to see
if we can have exclusive use of the mutex
     if ( waitResult = WAIT_TIMEOUT ) then // if we can't then broadcast
the message to make the owner of the mutex respond     { request that the running application takes focus }
       begin
          BroadcastList := BSM_APPLICATIONS;
          BroadcastSystemMessage(
BSF_POSTMESSAGE,@BroadcastList,MessageID,0,0); //32 bit - broadcast the
message to all apps - only a prev inst will hear it.
       end
     else
      begin
      { do the normal stuff}
      Application.Title := 'Choice Organics Purchase & Sales System';
      Application.CreateForm(TMainForm, MainForm);
      Application.Run;
      ReleaseMutex(hMutex); // release the mutex as a politeness      end;
      CloseHandle(hMutex); // close the mutex handle
end.This goes in the MainFormprocedure Tmainform.OnAppMessage(var Msg : TMsg ; Var Handled : Boolean);
begin
{ If it's the special message then focus on this window}
if Msg.Message = MessageID then // if we get the broadcast message from an
another instance of this app that is trying to start up
   begin
      show;
      WindowState := wsMaximized;
      BringToFront;
      SetFocus;
      Handled := true;   end;
end;//And this goes in the TMainForm.FormCreate ;-Application.OnMessage:= OnAppMessage;

解决方案 »

  1.   

    《delphi 5编程实例与技巧》
    里面的16.6.24有详细的例子。
      

  2.   


     program DEL3test; uses
     Forms,Windows,SysUtils,
     DEL3unit in 'DEL3unit.pas' {Form1}; {$R *.RES} Var
        hMutex:HWND;
        Ret:Integer;
     begin
      Application.Initialize;
      Application.Title := 'aaaaaa';
      hMutex:=CreateMutex(nil,False,'aaaaaa');
      Ret:=GetLastError;
      If Ret<>ERROR_ALREADY_EXISTS Then 
       Begin
        Application.CreateForm(TForm1, Form1);
        Application.Run;
       End
      Else
       Application.MessageBox('Run Twice!','Notes!',MB_OK);
      ReleaseMutex(hMutex);
     end.
      

  3.   

    以下是项目文件:
    ...
    ...
    $R *.RES}
      Var
        hMutex:HWND;
        Ret:Integer;
    begin  Application.Initialize;
      Application.Title := '信息系统';
      hMutex:=CreateMutex(nil,False,'信息系统');
      Ret:=GetLastError;
      If Ret<>ERROR_ALREADY_EXISTS Then
       Begin
       Application.CreateForm(TajMAIN, );
       ...
       ...
       Application.Run;
       End
      Else
       Application.MessageBox('已经运行!','注意!',MB_OK);
       ReleaseMutex(hMutex);end.
      

  4.   

    这是我的form1的代码,禁止程序的二次运行的代码应该放在哪里?
    unit printpage;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, ExtCtrls, StdCtrls, TFlatEditUnit, TFlatButtonUnit, DB, DBTables;type
      TForm2 = class(TForm)
        Label1: TLabel;
        Label2: TLabel;
        FlatEdit1: TFlatEdit;
        FlatEdit2: TFlatEdit;
        FlatButton1: TFlatButton;
        FlatButton2: TFlatButton;
        Image1: TImage;
        Image2: TImage;
        Query1: TQuery;
        DataSource1: TDataSource;
        procedure FlatButton2Click(Sender: TObject);
        procedure FlatButton1Click(Sender: TObject);  private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementationuses printset, check;{$R *.dfm}procedure TForm1.FlatButton2Click(Sender: TObject);
    begin
    halt;
    end;procedure TForm1.FlatButton1Click(Sender: TObject);begin
    Query1.Close;
    Query1.SQl.Clear;
    Query1.SQl.Add('select * from "poweruser.db" where 工号='+''''+FlatEdit1.Text+''' and 密码='+''''+FlatEdit2.Text+'''');
    Query1.Prepare;
    Query1.Open;
    if Query1.RecordCount = 0 then
      begin
      Query1.Close;
      Query1.SQl.Clear;
      Query1.SQl.Add('select * from "user.db" where 工号='+''''+FlatEdit1.Text+''' and 密码='+''''+FlatEdit2.Text+'''');
      Query1.Prepare;
      Query1.Open;
        if Query1.RecordCount <> 0 then
           begin
           Form3.ShowModal;
           end
           else
           begin
           showmessage('登录失败,请重新输入');
           end;
      end
    else
    begin
    Form2.ShowModal;
    end;
    end;end.
      

  5.   

    工程文件中,
      Application.Initialize;
    前后