一般用两种方法实现。 1. 用findwindow函数根据窗口的标题和类名查找程序的主窗口是否存在。 如在dpr中,建立主窗口之前 if findwindow('tform1', 'form1') = 0 then //建立主窗口并运行程序 但是 2. 用CreateMutex函数建立一个互斥量。 如在dpr中 CreateMutex(0, true, 'my program'); if GetLastError <> ERROR_ALREADY_EXISTS then begin Application.Initialize; Application.CreateForm(TfMain, fMain); Application.Run; end;我个人认为用CreateMutex的方法比较好。用findwindow的话在调试的时候会有一些麻烦。
修改dpr文件,下面是我以前编的一个程序,看看对你有没有帮助。 program onlyone;uses Forms, Windows, main in 'main.pas' {Form1};{$R *.RES}var MutexHandle: THandle; hPrevInst: Boolean;begin //检查互斥句炳名 MutexHandle := CreateMutex(nil, TRUE, 'MysampleAppMutex'); if MutexHandle <> 0 then begin if GetLastError = ERROR_ALREADY_EXISTS then begin MessageBox(0, 'Instance of this application is already running.', 'Application already running', mb_IconHand); hPrevInst := TRUE; CloseHandle(MutexHandle); Halt; // 'Halt' 退出 end else begin //没找到 hPrevInst := FALSE; end; end else begin hPrevInst := FALSE; end; Application.Initialize; Application.Title := 'My sample app'; Application.CreateForm(TForm1, Form1); Application.Run; end.
修改dpr文件,下面是我的一个例子程序 program tool;uses Forms, windows, main in 'main.pas' {mainform}, setit in 'setit.pas' {setform}, about in 'about.pas' {aboutForm};{$R *.RES} var MutexHandle: THandle; //定义互斥句柄 //防止二次运行 begin // whether existed MutexHandle := CreateMutex(nil, TRUE, 'My toolbar'); if MutexHandle <> 0 then begin if GetLastError = ERROR_ALREADY_EXISTS then begin MessageBox(0, '程序已经运行...', '不好意思', mb_IconHand); CloseHandle(MutexHandle); Halt; // 'Halt' Is stop running the actual application. end; end; Application.Initialize; Application.Title := 'My toolbar'; Application.CreateForm(Tmainform, mainform); Application.CreateForm(Tsetform, setform); Application.CreateForm(TaboutForm, aboutForm); Application.Run; end.
修改dpr文件,下面是我的一个例子程序 program tool; uses Forms, windows, main in 'main.pas' {mainform}, setit in 'setit.pas' {setform}, about in 'about.pas' {aboutForm}; {$R *.RES} var MutexHandle: THandle; //定义互斥句柄 //防止二次运行 begin // whether existed MutexHandle := CreateMutex(nil, TRUE, 'My toolbar'); if MutexHandle <> 0 then begin if GetLastError = ERROR_ALREADY_EXISTS then begin MessageBox(0, '程序已经运行...', '不好意思', mb_IconHand); CloseHandle(MutexHandle); Halt; // 'Halt' Is stop running the actual application. end; end; Application.Initialize; Application.Title := 'My toolbar'; Application.CreateForm(Tmainform, mainform); Application.CreateForm(Tsetform, setform); Application.CreateForm(TaboutForm, aboutForm); Application.Run; end.
一般用两种方法实现。 1. 用findwindow函数根据窗口的标题和类名查找程序的主窗口是否存在。 如在dpr中,建立主窗口之前 if findwindow('tform1', 'form1') = 0 then //建立主窗口并运行程序 但是 2. 用CreateMutex函数建立一个互斥量。 如在dpr中 CreateMutex(0, true, 'my program'); if GetLastError <> ERROR_ALREADY_EXISTS then begin Application.Initialize; Application.CreateForm(TfMain, fMain); Application.Run; end;我个人认为用CreateMutex的方法比较好。用findwindow的话在调试的时候会有一些麻烦。
一般用两种方法实现。 1. 用findwindow函数根据窗口的标题和类名查找程序的主窗口是否存在。 如在dpr中,建立主窗口之前 if findwindow('tform1', 'form1') = 0 then //建立主窗口并运行程序
一般用两种方法实现。 1. 用findwindow函数根据窗口的标题和类名查找程序的主窗口是否存在。 如在dpr中,建立主窗口之前 if findwindow('tform1', 'form1') = 0 then //建立主窗口并运行程序 2. 用CreateMutex函数建立一个互斥量。 如在dpr中 CreateMutex(0, true, 'my program'); if GetLastError <> ERROR_ALREADY_EXISTS then begin Application.Initialize; Application.CreateForm(TfMain, fMain); Application.Run; end;我个人认为用CreateMutex的方法比较好。用findwindow的话在调试的时候会有一些麻烦。
1. 用findwindow函数根据窗口的标题和类名查找程序的主窗口是否存在。
如在dpr中,建立主窗口之前
if findwindow('tform1', 'form1') = 0 then
//建立主窗口并运行程序
但是
2. 用CreateMutex函数建立一个互斥量。
如在dpr中
CreateMutex(0, true, 'my program');
if GetLastError <> ERROR_ALREADY_EXISTS then begin
Application.Initialize;
Application.CreateForm(TfMain, fMain);
Application.Run;
end;我个人认为用CreateMutex的方法比较好。用findwindow的话在调试的时候会有一些麻烦。
program onlyone;uses
Forms,
Windows,
main in 'main.pas' {Form1};{$R *.RES}var
MutexHandle: THandle;
hPrevInst: Boolean;begin
//检查互斥句炳名
MutexHandle := CreateMutex(nil, TRUE, 'MysampleAppMutex');
if MutexHandle <> 0 then
begin
if GetLastError = ERROR_ALREADY_EXISTS then
begin
MessageBox(0, 'Instance of this application is already running.',
'Application already running', mb_IconHand); hPrevInst := TRUE;
CloseHandle(MutexHandle);
Halt; // 'Halt' 退出
end
else
begin
//没找到
hPrevInst := FALSE;
end;
end
else
begin
hPrevInst := FALSE;
end; Application.Initialize;
Application.Title := 'My sample app';
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
program tool;uses
Forms,
windows,
main in 'main.pas' {mainform},
setit in 'setit.pas' {setform},
about in 'about.pas' {aboutForm};{$R *.RES}
var
MutexHandle: THandle; //定义互斥句柄
//防止二次运行
begin
// whether existed
MutexHandle := CreateMutex(nil, TRUE, 'My toolbar');
if MutexHandle <> 0 then
begin
if GetLastError = ERROR_ALREADY_EXISTS then
begin
MessageBox(0, '程序已经运行...',
'不好意思', mb_IconHand);
CloseHandle(MutexHandle);
Halt; // 'Halt' Is stop running the actual application.
end;
end;
Application.Initialize;
Application.Title := 'My toolbar';
Application.CreateForm(Tmainform, mainform);
Application.CreateForm(Tsetform, setform);
Application.CreateForm(TaboutForm, aboutForm);
Application.Run;
end.
program tool;
uses
Forms,
windows,
main in 'main.pas' {mainform},
setit in 'setit.pas' {setform},
about in 'about.pas' {aboutForm};
{$R *.RES}
var
MutexHandle: THandle; //定义互斥句柄
//防止二次运行
begin
// whether existed
MutexHandle := CreateMutex(nil, TRUE, 'My toolbar');
if MutexHandle <> 0 then
begin
if GetLastError = ERROR_ALREADY_EXISTS then
begin
MessageBox(0, '程序已经运行...',
'不好意思', mb_IconHand);
CloseHandle(MutexHandle);
Halt; // 'Halt' Is stop running the actual application.
end;
end;
Application.Initialize;
Application.Title := 'My toolbar';
Application.CreateForm(Tmainform, mainform);
Application.CreateForm(Tsetform, setform);
Application.CreateForm(TaboutForm, aboutForm);
Application.Run;
end.
1. 用findwindow函数根据窗口的标题和类名查找程序的主窗口是否存在。
如在dpr中,建立主窗口之前
if findwindow('tform1', 'form1') = 0 then
//建立主窗口并运行程序
但是
2. 用CreateMutex函数建立一个互斥量。
如在dpr中
CreateMutex(0, true, 'my program');
if GetLastError <> ERROR_ALREADY_EXISTS then begin
Application.Initialize;
Application.CreateForm(TfMain, fMain);
Application.Run;
end;我个人认为用CreateMutex的方法比较好。用findwindow的话在调试的时候会有一些麻烦。
1. 用findwindow函数根据窗口的标题和类名查找程序的主窗口是否存在。
如在dpr中,建立主窗口之前
if findwindow('tform1', 'form1') = 0 then
//建立主窗口并运行程序
1. 用findwindow函数根据窗口的标题和类名查找程序的主窗口是否存在。
如在dpr中,建立主窗口之前
if findwindow('tform1', 'form1') = 0 then
//建立主窗口并运行程序
2. 用CreateMutex函数建立一个互斥量。
如在dpr中
CreateMutex(0, true, 'my program');
if GetLastError <> ERROR_ALREADY_EXISTS then begin
Application.Initialize;
Application.CreateForm(TfMain, fMain);
Application.Run;
end;我个人认为用CreateMutex的方法比较好。用findwindow的话在调试的时候会有一些麻烦。
· 在程序启动时,使用EnumWindows枚举所有的顶层窗口,并向每个窗口发送(使用SendMessage)那个自定义消息并检查返回结果。Windows的缺省消息处理函数对于自定义消息返回0。该方法的优点是非常精确,不会出现错误。RegisterWindowMessage确保你所注册的消息的唯一性。同时你可以注册多个自定义消息来执行其他功能,如使用正在运行的程序拷贝来打开用户双击的文件等。
hMutex = CreateMutex(NULL, TRUE, "Any String");
if( hMutex == NULL )
{
//System Error return
}
else
{
if( !(GetLastError() == ERROR_ALREADY_EXISTS && hMutex != NULL) )
// One Instance is running just return
}
或者用窗口类,将你的主窗口命名一个特殊的窗口类名,然后用ENUMWINDOW找是否系统中已经有这个名字的窗口类
因为无法建立同名的Event,所以第二个实例会建立失败......这是<<Windows核心编程>>里使用的办法