ログイン: パスワード:   新規登録  パスワード紛失
 
メインメニュー
検索
オンライン状況
登録ユーザ: 0
ゲスト: 6
テーマ選択

(4 テーマ)
2重起動を防止する のバックアップソース(No.2)

RIGHT:[[Delphi]]
*2重起動を防止する [#ka3ce0ec]

2重起動を防止するにはプロジェクトソースに次のコードを記述する。~
プロジェクトソースは [プロジェクト] - [ソース表示] で 表示できる。 

 program Sample;
 
 uses
   Forms,
   Windows,
   Messages,
   Unit1 in 'Unit1.pas' {Form1},
 
 {$R *.RES}
 
 const
   MutexName = 'Sample Mutex';      //Mutexを識別するユニークな名称
 
 var
  hMutex: THANDLE;
  hWndPrev, hWndApp: HWND;
 
 begin
   (*---- 二重起動を防止する処理 ----*)
   hMutex := OpenMutex(MUTEX_ALL_ACCESS, False, MutexName);
   if hMutex <> 0 then begin
     (*---- すでに Mutex が登録されている ----*)
     CloseHandle(hMutex);
     hWndPrev := FindWindow('TForm1', nil);     //メインフォームを探す
     if hWndPrev <> 0 then begin
       (*---- 起動済みのフォームを表示させる ----*)
       SetForegroundWindow(hWndPrev);
       hWndApp := GetWindowLong(hWndPrev, GWL_HWNDPARENT);
       if (hWndApp <> 0) and IsIconic(hWndApp) then begin
         SendMessage(hWndApp, WM_SYSCOMMAND, SC_RESTORE, -1);
       end;
     end;
     Exit;
   end;
   CreateMutex(nil, False, MutexName);
   (*---- ここまで ----*)
 
   Application.Initialize;
   Application.CreateForm(TForm1, Form1);
   Application.Run;
 
   ReleaseMutex(hMutex);          //作成したMutexを開放する。
 
 end.

2重起動を防止するコードはアプリケーションの初期化前なので、 Application変数などは使用できないので注意が必要。


トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
PR