一个应用程序多次点击时,如何只让它只运行一个
2004-10-09 09:14
323 查看
///////////////////// (一)项目文件 test.dpr //////////////////////
program SerialGet;
uses
Forms,
UMain in 'UMain.pas' {frmMain},
ULogin in 'UForm2.pas' {Form2},
UDataModule in 'UDataModule.pas' {DataModule1: TDataModule},
{$R *.res}
begin
Application.Initialize;
if CreateMutex then //创建句柄,判断此应用程序是否在运行
begin
Application.CreateForm(TfrmMain, frmMain);
Application.CreateForm(Tform2, form2);
Application.CreateForm(TDataModule1, DataModule1);
Application.Run;
end else
begin
DestroyMutex; //释放句柄
end;
end.
//////////////// (二)登陆窗体 UMain.pas UMain.dfm //////////////////
unit UMain;
interface
uses ......
type
... ... ...
private
public
end;
var
frmMain: TfrmMain;
function CreateMutex: Boolean; // 全项目公用函数
procedure DestroyMutex; // 全项目公用函数
implementation
uses UDataModule; //引用数据模块
var Mutex: hWnd;
{$R *.dfm}
procedure DestroyMutex;
begin
if Mutex <> 0 then CloseHandle(Mutex);
end;
function CreateMutex: Boolean;
var
PrevInstHandle: THandle;
AppTitle: PChar;
begin
AppTitle := StrAlloc(100);
StrPCopy(AppTitle, Application.Title);
Result := True;
Mutex := Windows.CreateMutex(nil, False, AppTitle);
if (GetLastError = ERROR_ALREADY_EXISTS) or (Mutex = 0) then begin
Result := False;
SetWindowText(Application.Handle, '');
PrevInstHandle := FindWindow(nil, AppTitle);
if PrevInstHandle <> 0 then begin
if IsIconic(PrevInstHandle) then
ShowWindow(PrevInstHandle, SW_RESTORE)
else
BringWindowToTop(PrevInstHandle);
SetForegroundWindow(PrevInstHandle);
end;
if Mutex <> 0 then Mutex := 0;
end;
StrDispose(AppTitle);
end;
program SerialGet;
uses
Forms,
UMain in 'UMain.pas' {frmMain},
ULogin in 'UForm2.pas' {Form2},
UDataModule in 'UDataModule.pas' {DataModule1: TDataModule},
{$R *.res}
begin
Application.Initialize;
if CreateMutex then //创建句柄,判断此应用程序是否在运行
begin
Application.CreateForm(TfrmMain, frmMain);
Application.CreateForm(Tform2, form2);
Application.CreateForm(TDataModule1, DataModule1);
Application.Run;
end else
begin
DestroyMutex; //释放句柄
end;
end.
//////////////// (二)登陆窗体 UMain.pas UMain.dfm //////////////////
unit UMain;
interface
uses ......
type
... ... ...
private
public
end;
var
frmMain: TfrmMain;
function CreateMutex: Boolean; // 全项目公用函数
procedure DestroyMutex; // 全项目公用函数
implementation
uses UDataModule; //引用数据模块
var Mutex: hWnd;
{$R *.dfm}
procedure DestroyMutex;
begin
if Mutex <> 0 then CloseHandle(Mutex);
end;
function CreateMutex: Boolean;
var
PrevInstHandle: THandle;
AppTitle: PChar;
begin
AppTitle := StrAlloc(100);
StrPCopy(AppTitle, Application.Title);
Result := True;
Mutex := Windows.CreateMutex(nil, False, AppTitle);
if (GetLastError = ERROR_ALREADY_EXISTS) or (Mutex = 0) then begin
Result := False;
SetWindowText(Application.Handle, '');
PrevInstHandle := FindWindow(nil, AppTitle);
if PrevInstHandle <> 0 then begin
if IsIconic(PrevInstHandle) then
ShowWindow(PrevInstHandle, SW_RESTORE)
else
BringWindowToTop(PrevInstHandle);
SetForegroundWindow(PrevInstHandle);
end;
if Mutex <> 0 then Mutex := 0;
end;
StrDispose(AppTitle);
end;
相关文章推荐
- 一个应用程序多次点击时,如何只让它只运行一个
- C# 应用程序开发中,如何防止一个C#开发的EXE被多次运行
- C#应用程序只允许运行一个实例,多次运行的时候激活当前实例,显示其界面
- c#检查程序是否已经运行,如何防止多次运行同一个程序?
- 如何判断一个android应用程序第一次运行?
- 如何在终端使用后台运行模式启动一个Linux应用程序
- C# 训练场(五)如何保证应用程序只运行一次(只开一个窗口)
- (转)如何使应用程序只运行一个实例
- 如何判断listview同一条目被多次点击 listview每个条目中有一个button 点击button可以删除条目中的数据
- Java中,如何将一个测试类多次运行。
- 如何用创建命名对象来判断应用程序是否已有一个实例在运行?
- ASP.NET MVC Tip #17 – 如何运行一个ASP.NET MVC应用程序
- 解决点击一个按钮如何打开一个已安装好的应用程序
- 关于"如何让应用程序只有一个实例在运行"的孟宪会的解决方法的疑问
- 如何使应用程序只运行一个实例
- 如何使应用程序只有一个实例在运行
- (三十一)工具方法:获得应用程序包名/安装apk/如何获得当前栈顶的Activity/android如何实现点击一个按钮进入指定网页
- 如何让应用程序只有一个实例在运行?
- 如何使应用程序只运行一个实例
- 如何使应用程序只运行一个实例