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

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

圣哥

Unregistered / Unconfirmed
GUEST, unregistred user!
同一个记事本可以被打开很多次,如何使它只被打开一次呢?<br>有什么api函数可以用吗?<br>若可以,请给出例子,谢谢!!!
 
你是要对自己编的程序实现这种功能--&gt;防止多重启动.很多书有介绍<br>还是要对任一其他EXE文件.--&gt;不知道
 
在左边检索以下ID的贴子:<br>ID:357133 <br>ID:415602<br>ID:510052 <br>ID:284268
 
将这段代码--unit文件<br>加到你的程序中去,OK<br>unit MultInst;<br><br>interface<br><br>uses Forms,windows,Dialogs,SysUtils;<br><br>{function BroadcastSystemmessage(Flags: DWORD;recipients:PDWORD;uiMessage: Unit;wParam:<br>WParam;lParam:LParam):Longint;stdcall;external'user32.dll';<br>&nbsp;}<br>const<br>&nbsp; MI_NO_ERROR &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =0;<br>&nbsp; MI_FAIL_SUBCLASS &nbsp; &nbsp; &nbsp;=1;<br>&nbsp; MI_FAIL_CREATE_MUTEX &nbsp;=2;<br><br>function GetMiError: integer;<br><br>implementation<br>const UniqueAppStr: Pchar='I am the Eggman!';<br>var<br>&nbsp; messageId: integer;<br>&nbsp; WProc:TFNWndProc = nil;<br>&nbsp; MutHandle: THandle = 0;<br>&nbsp; MIError: integer = 0;<br><br>function GetMIError: integer;<br>begin<br>&nbsp; Result := MIError;<br>end;<br><br>function NewWndProc(Handle:HWND;MSG: Integer;WParam,LParam: LongInt):longint;stdcall;<br>begin<br>&nbsp; {如果是注册的消息}<br>&nbsp; if Msg = MessageID then<br>&nbsp; begin<br>&nbsp; &nbsp; {如果主窗口已最小化,就恢复成原先的大小}<br>&nbsp; &nbsp; if IsIconic(application.handle) then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; application.MainForm.WindowState := wsNormal;<br>&nbsp; &nbsp; &nbsp; application.Restore;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; SetForegroundWindow(application.MainForm.Handle);<br>&nbsp; end<br>&nbsp; else<br>&nbsp; &nbsp; Result :=CallWindowProc(WProc,Handle,Msg,Wparam,LParam);<br>end;<br><br>procedure SubClassApplication;<br>begin<br>&nbsp; {替换应用对象的窗口程序以使applicationONMessage对用户仍然可用}<br>&nbsp; WProc:=TFNWndProc(SetWindowLong(application.handle,GWL_WNDPROC,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; longint(@NewWndProc)));<br>&nbsp; if WProc = nil then<br>&nbsp; &nbsp; MIError := MIError or MI_FAIL_SUBCLASS;<br>end;<br><br>procedure DOFirstInstance;<br>begin<br>&nbsp; SubClassApplication;<br>&nbsp; MutHandle := CreateMutex(Nil,false,UniqueAppStr);<br>&nbsp; if MutHandle = 0 then<br>&nbsp; &nbsp; MIError := MIERROR or MI_FAIL_CREATE_MUTEX;<br>end;<br><br>procedure BroadcastFocusMessage;<br>{如果已经有实例存在,就会调用这个过程}<br>var<br>&nbsp; BSMRecipients:DWORD;<br>begin<br>&nbsp; {暂时隐去主form}<br>&nbsp; Application.ShowMainForm := False;<br>&nbsp; {广播一个消息通知其他实例激活自己}<br>&nbsp; BSMRecipients := BSM_Applications;<br>&nbsp; BroadCastSystemMEssage(BSF_IGNORECURRENTTASk OR BSF_POSTMESSAGE,@BSMRecipients,MEssageID,0,0);<br>&nbsp; application.Terminate;<br>end;<br><br>procedure InitInstance;<br>begin<br>&nbsp; MutHandle := OpenMutex(MUTEX_ALL_ACCESS,False,UniqueAppStr);<br>&nbsp; if MutHandle = 0 then<br>&nbsp; &nbsp; {互斥对象还没有创建,说明没有实例存在}<br>&nbsp; &nbsp; DoFirstInstance<br>&nbsp; else<br>&nbsp; &nbsp; BroadcastFocusMEssage;<br>end;<br><br>initialization<br>&nbsp; MessageID := RegisterWindowMessage(UniqueAppStr);<br>&nbsp; InitInstance;<br>finalization<br>&nbsp; if WProc &lt;&gt; nil then<br>&nbsp; &nbsp; {恢复原来的窗口过程}<br>&nbsp; &nbsp; SetWindowLong(Application.handle,GWL_WNDPROC,LongInt(WProc));<br>end.
 
多人接受答案了。
 

Similar threads

D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部