?如何使程序只执行一次,第二次执行时自动激活或从图标还原。(100分)

  • 主题发起人 主题发起人 圣哥
  • 开始时间 开始时间

圣哥

Unregistered / Unconfirmed
GUEST, unregistred user!
同一个记事本可以被打开很多次,如何使它只被执行依一次呢?
 
防止程序运行多个例程? &nbsp;<br>More than one instance of program?<br>回答<br>&nbsp;This is copied direct from my *.dpr file. You can work it for your own<br>use.<br>var<br>&nbsp; hMutex : Thandle;<br>&nbsp; WaitResult : word;<br>&nbsp; BroadcastList : DWORD;<br>begin<br>&nbsp; &nbsp; &nbsp;MessageID := RegisterWindowMessage('Check For Choice Previous Inst');<br>// register a message to use later on<br>&nbsp; &nbsp; &nbsp;hMutex := createMutex(nil,false,pchar('App_Choice')); // grab a mutex<br>handle<br>&nbsp; &nbsp; &nbsp;WaitResult := WaitForSingleObject(hMutex,10); // wait to see<br>if we can have exclusive use of the mutex<br>&nbsp; &nbsp; &nbsp;if ( waitResult = WAIT_TIMEOUT ) then // if we can't then broadcast<br>the message to make the owner of the mutex respond<br>&nbsp; &nbsp; &nbsp;{ request that the running application takes focus }<br>&nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BroadcastList := BSM_APPLICATIONS;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BroadcastSystemMessage(<br>BSF_POSTMESSAGE,@BroadcastList,MessageID,0,0); //32 bit - broadcast the<br>message to all apps - only a prev inst will hear it.<br>&nbsp; &nbsp; &nbsp; &nbsp;end<br>&nbsp; &nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; { do the normal stuff}<br>&nbsp; &nbsp; &nbsp; Application.Title := 'Choice Organics Purchase &amp; Sales System';<br>&nbsp; &nbsp; &nbsp; Application.CreateForm(TMainForm, MainForm);<br>&nbsp; &nbsp; &nbsp; Application.Run;<br>&nbsp; &nbsp; &nbsp; ReleaseMutex(hMutex); // release the mutex as a politeness<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; CloseHandle(hMutex); // close the mutex handle<br>end.<br>This goes in the MainForm<br>procedure Tmainform.OnAppMessage(var Msg : TMsg ; Var Handled : Boolean);<br>begin<br>{ If it's the special message then focus on this window}<br>if Msg.Message = MessageID then // if we get the broadcast message from an<br>another instance of this app that is trying to start up<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; show;<br>&nbsp; &nbsp; &nbsp; WindowState := wsMaximized;<br>&nbsp; &nbsp; &nbsp; BringToFront;<br>&nbsp; &nbsp; &nbsp; SetFocus;<br>&nbsp; &nbsp; &nbsp; Handled := true;<br>&nbsp; &nbsp;end;<br>end;<br>//And this goes in the TMainForm.FormCreate ;-<br>Application.OnMessage:= OnAppMessage;<br>
 
同上 <br>--简/明 &nbsp; <br>&nbsp; &nbsp; 第二次执行时自动 从systray图标还原 (*.DPR<br>{$R *.RES}<br>VAR<br>P:HWND; <br>begin<br>P := FindWindow (nil,'录放');<br>if P&lt;&gt; 0 then &nbsp;begin<br> Messagebox(0,'程序已经在运行! ','录放器',mb_ok or MB_ICONWARNING);<br> ShowWindow(P,SW_RESTORE); //这一行<br>//第二次执行时自动激活或从图标还原<br>&nbsp; halt(0); &nbsp; end;<br>Application.Title := '录放';// Title<br>///////////////////////////////////////////<br>&nbsp; Application.Initialize;<br>&nbsp; Application.CreateForm(TForm1, Form1);<br>&nbsp; Application.Run;<br>end.
 
后退
顶部