使用OpenMutex方法,去防止程式重複執行的問題 |
尚未結案
|
Morris
一般會員 發表:1 回覆:5 積分:6 註冊:2002-03-13 發送簡訊給我 |
|
supman
尊榮會員 發表:29 回覆:770 積分:924 註冊:2002-04-22 發送簡訊給我 |
您好:
試試看用FindWidow的方法
function IsExecute(filename:string):boolean; var MyWnd : Thandle; begin MyWnd:=FindWindow(nil, pchar(filename)); result:=MyWnd<>0; end; procedure TForm1.FormCreate(Sender: TObject); begin if IsExecute('project1') then application.terminate end;參考這篇http://delphi.ktop.com.tw/topic.php?topic_id=20777 |
wameng
版主 發表:31 回覆:1336 積分:1188 註冊:2004-09-16 發送簡訊給我 |
參考以下程式碼。
function BroadcastSystemMessage(Flags: DWORD; Recipients: PDWORD; uiMessage: UINT; wParam: WPARAM; lParam: LPARAM): Longint; stdcall; external 'user32.dll'; const MI_NO_ERROR = 0; MI_FAIL_SUBCLASS = 1; MI_FAIL_CREATE_MUTEX = 2; function GetMIError: Integer; var Form1: TForm1; implementation {$R *.DFM} const UniqueAppStr = 'TITLE1'; var MessageID: Integer; WProc: TFNWndProc = nil; MutHandle: THandle = 0; MIError: Integer = 0; function GetMIError: Integer; begin Result := MIError; end; function NewWndProc(Handle: HWND; Msg: Integer; wParam, lParam: Longint): Longint; stdcall; begin if Msg = MessageID then begin Result := 0; if IsIconic(Application.Handle) then begin Application.MainForm.WindowState := wsNormal; Application.Restore; end; SetForegroundWindow(Application.MainForm.Handle); end else Result := CallWindowProc(WProc, Handle, Msg, wParam, lParam); end; procedure InitInstance; var BSMRecipients: DWORD; begin MutHandle := OpenMutex(MUTEX_ALL_ACCESS, False, UniqueAppStr); if MutHandle = 0 then begin WProc := TFNWndProc(SetWindowLong(Application.Handle, GWL_WNDPROC, Longint(@NewWndProc))); if WProc = nil then MIError := MIError or MI_FAIL_SUBCLASS; MutHandle := CreateMutex(nil, False, UniqueAppStr); if MutHandle = 0 then MIError := MIError or MI_FAIL_CREATE_MUTEX; end else begin Application.ShowMainForm := False; BSMRecipients := BSM_APPLICATIONS; BroadcastSystemMessage(BSF_IGNORECURRENTTASK or BSF_POSTMESSAGE, @BSMRecipients, MessageID, 0, 0); Application.Terminate; end; end; initialization MessageID := RegisterWindowMessage(UniqueAppStr); InitInstance; finalization if WProc <> nil then SetWindowLong(Application.Handle, GWL_WNDPROC, Longint(WProc)); end.在主視窗中引用就可以了。 |
Morris
一般會員 發表:1 回覆:5 積分:6 註冊:2002-03-13 發送簡訊給我 |
|
Morris
一般會員 發表:1 回覆:5 積分:6 註冊:2002-03-13 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |