| 首页 | 电脑常识 | 程序设计 | 操作系统 | 语法 | 病毒安全 | 软件教程 | 硬件 | 数据库 | 多媒体 | 认证 | 下载 | 
首页>>程序设计 >>综合
一个应用程序多次点击时,如何只让它只运行一个

一个应用程序多次点击时,如何只让它只运行一个

电脑学习网,xuef.com,最全最新最权威的电脑知识网站.
免费计算机学习教程,电脑入门指南.

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

相 关 文 章
  • 网络函数库

  • 我观MIDAS

  • Delphi 消息机制引入的一个副作用

  • 小知识,如Form淡出、捕捉Form最小化

  • Delphi 与 Word_VBA

  • 分析DFM文件生成程序界面

  • 关于时间函数

  • 合理应用用户登录界面,用户登录时不必创建其他窗体

  • 数据补丁

  • mdb Utils (Access)

  • 学府网电脑学习的乐园
    中国电脑教学网,电脑爱好者的乐园,做最好最全的计算机学习网站.