如何防止同一个程序,被运行两次?给我一个例子
解决方案 »
- 有关pos打印机,分单打印不能切纸问题 ,多谢谢各位了
- 时间问题
- 各位大侠,救救小弟的命吧!
- Oracle与C编程底层软件。哪个比较有前途???
- 谁能告诉我*.MWP要用什么打开?重重大大的有赏!
- DELPHI如何产生按键的消息给特定的进程,或者给全部进程?
- 如何用Image组件显示jpg格式图片?
- 谁有台湾,钱达智,delphi学习笔记电子版?
- 谁能做出动画显示Label的效果,就像动画显示窗体一样(AnimateWindow(form2.Handle,500,AW_BLEND or AW_CENTER);)
- 怎样对动态生成的控件进行事件的截获。
- 终于可以散200了,第一次散200.
- 请问Delphi中的日历?
if getlasterror=error_already_exists then
begin
sendmessage(hwnd_broadcast,registerwindowmessage(pro_ONly'),0,0);
halt(0);
end;
放在 Application.Initialize;之前
unit UOneApp;interfaceconst
MI_QUERYWINDOWHANDLE=1;
MI_RESPONDWINDOWHANDLE=2; MI_ERROR_NONE=0;
MI_ERROR_FAILSUBCLASS=1;
MI_ERROR_CREATINGMUTEX=2; function GetMiError:Integer;implementationuses UMain,Forms,Windows,SysUtils;const UniqueAppStr='MyApplicationsss';
var
MessageId:Integer;
WProc:TFNWndProc;
MutHandle:THandle;
MIError:Integer;function GetMiError:Integer;
begin
Result:= MIError;
end;function NewWndProc(Handle:HWND;Msg:Integer;wParam,lParam:LongInt):LongInt;stdCall;
begin
Result:=0;
if Msg=MessageID then
begin
case wParam of
MI_QUERYWINDOWHANDLE:
begin
if isIconic(Application.Handle) then
begin
Application.MainForm.WindowState:=wsNormal;
Application.Restore;
end;
PostMessage(HWND(lParam),MessageID,MI_RESPONDWINDOWHANDLE,
Application.MainForm.Handle);
end;
MI_RESPONDWINDOWHANDLE:
begin
SetForeGroundWindow(HWND(lParam));
Application.Terminate;
end;
end;
end
else
Result:=CallWindowProc(WProc,Handle,Msg,wParam,lParam);
end;procedure SubClassApplication;
begin
WProc:=TFNWndProc(SetWindowLong(Application.Handle,GWL_WNDPROC,longint(@NewWndProc)));
if WProc=nil then
MIError:=MIError or MI_ERROR_FAILSUBCLASS;
end;procedure DoFirstInstance;
begin
MutHandle:=CreateMutex(nil,false,uniqueappstr);
if MutHandle=0 then
MIError:=MIError or MI_ERROR_CREATINGMUTEX;
end;procedure BroadCastFocusMessage;
var
BSMRecipients:DWORD;
begin
Application.ShowMainForm:=false;
BSMRecipients:=BSM_APPLICATIONS;
BroadCastSystemMessage(BSF_IGNORECURRENTTASK or BSF_POSTMESSAGE,@BSMRecipients,
MessageID,MI_QUERYWINDOWHANDLE,Application.Handle);
end;procedure InitInstance;
begin
SubClassApplication;
MutHandle:=OpenMutex(MUTEX_ALL_ACCESS,False,UniqueAppStr);
if MutHandle=0 then
DoFirstInstance
else
BroadCastFocusMessage;
end;initialization
MessageID:=RegisterWindowMessage(UniqueAppStr);
InitInstance;
finalization
if WProc<>nil then
SetWindowLong(Application.Handle,GWL_WNDPROC,LongInt(WProc));
if MutHandle<>0 then
CloseHandle(MutHandle);end.
WinHandle:THANDLE;
begin
WinHandle:=FindWindow(nil,'某某系统');
if WinHandle=0 then
Application.Initialize;
Application.CreateForm(TForm1,Form1);
Application.Run;
else
begin
showmessage('程序已经运行!') //这句可去掉,直接切换到已运行的实例
windows.setfocus(WinHandle)
windows.setforeground(WinHandle)
end;
end.
我知道,加入主窗体的创建事件中,代码如下:
procedure TForm1.FormCreate(Sender: TObject);
var
ZAppName: array[0..127] of char;
Hold: String;
Found: HWND;
Found: HWND;
begin
Hold := Application.Title;
Application.Title := 'OnlyOne'
+ IntToStr(HInstance); // 暂时修改窗口标题
StrPCopy(ZAppName, Hold); // 原窗口标题
Found := FindWindow(nil, ZAppName); // 查找窗口
Application.Title := Hold; // 恢复窗口标题
if Found<>0 then begin
// 若找到则激活已运行的程序并结束自身
ShowWindow(Found, SW_RESTORE);
Application.Terminate;
end;
end;
Forms,windows;
unit1 in 'unit1.pas' {f_menu},{$R *.RES}
Var
hMutex:HWND;
Ret:Integer;
begin
Application.Initialize;
Application.Title := 'EXE';
hMutex:=CreateMutex(nil,False,'main');
Ret:=GetLastError;
If Ret<>ERROR_ALREADY_EXISTS Then
Begin
Application.CreateForm(Tform1, form1);
Application.Run;
End
Else
Application.MessageBox('本程序只可同时运行一个!','提示框',MB_OK+mb_iconwarning);
ReleaseMutex(hMutex);
end.
s1,s2 : string;begin
//
s1 := Application.ClassName;
s2 := '粮情测控系统';
if FindWindow(PChar(s1),PChar(s2)) <> 0 then
begin
ShowMessage('本程序的另一个实例已经在运行,请单击确定退出');
Exit;
end;
end;
//以上用于防止重复进入本程序
(Lyhua)
NND!
Findwindows的人全是笨蛋!!