Delphi

本类阅读TOP10

·分布式网络考试系统原型分析及实现
·游戏外挂设计技术探讨①
·使用HOOK随心监视Windows
·Delphi 水晶报表打包解决
·试题库开发中非文本数据的处理
·如何将几个DBGRID里的内容导入同一个EXCEL表中....的问题
·如何使用Delphi设计强大的服务器程序
·工人线程中关闭窗体的实现
·用DLL方式封装MDI子窗体。
·支持XP下托盘栏气球提示的托盘单元

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
一个应用程序多次点击时,如何只让它只运行一个

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

/////////////////////  (一)项目文件  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;




相关文章

相关软件