CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Delphi >  VCL组件开发及应用

怎么样做到只能打开一个程序实例?

楼主cjdbd()2003-06-01 12:29:48 在 Delphi / VCL组件开发及应用 提问

编译成可执行程序后,  
  可以打开好多个。  
   
   
  请问怎么做才能做到只能打开一个程序实例 问题点数:0、回复次数:12Top

1 楼cjdbd()回复于 2003-06-01 12:47:37 得分 0

请问怎么做到呢?  
   
  我运行了一次dd.exe,打开这个dd  
  我又运行一次dd.exe   ,又打开这个dd  
   
  那么就出现了两个dd了?  
   
  我现在只想让它运行一个??  
  Top

2 楼cjdbd()回复于 2003-06-01 13:42:08 得分 0

请问各位到底怎么才能实现呢?Top

3 楼cjdbd()回复于 2003-06-01 13:50:48 得分 0

请问能不能实现呢????  
  还是没有办法实现?Top

4 楼delphimo((误入歧途))回复于 2003-06-01 13:53:45 得分 0

搜索一下帖子太多了!  
  下次记着给分,大家很势利阿!Top

5 楼cjdbd()回复于 2003-06-01 13:58:30 得分 0

我也很想给的啊。  
  但是我都给完了。  
  要到明天才能有分。  
  我是有分必给的。Top

6 楼pilicat(Delphi迷)回复于 2003-06-01 14:04:46 得分 0

能帮就帮你一把,请参阅下文:  
   
  http://lzw222.myrice.com/1-BianChengJiQiao/BCJQ-WZ/BCJQ20.htm  
  Top

7 楼ahyf(笨笨)回复于 2003-06-01 15:10:35 得分 0

参考这个  
  Var  
        hWindow   :   HWND;  
  begin  
      Application.Initialize;  
      hWindow:=FindWindow(nil,pchar('myup-eHR人力资源管理系统'));  
      if   isWindow(hWindow)   then  
      Begin  
            hwindow:=getwindow(hwindow,GW_OWNER);  
            hwindow:=getlastactivepopup(hWindow);  
            if   isiconic(hwindow)   then  
                    openicon(hwindow);  
            setforegroundwindow(hwindow);  
            ShowMessage('程序已经运行');  
            application.terminate;  
            exit;  
      end  
      else  
      Begin  
              Application.CreateForm(TTuhrMain,   TuhrMain);  
      Application.CreateForm(TF_ChangeUser,   F_ChangeUser);  
      Application.CreateForm(TLogin,   Login);  
      Application.CreateForm(TAboutBox,   AboutBox);  
      Application.Title:='myup-eHR人力资源管理系统';  
              Application.Run;  
      end;  
  end.Top

8 楼byc6352(雪原)回复于 2003-06-01 16:04:50 得分 0

在程序开始运行时,用:  
  FindWindow(nil,pchar(‘主程序窗体标题’))  
  查找操作系统中有无本程序在运行,如果有:  
  application.terminate;  
  exit;  
  这段代码可放在项目文件开始或主程序窗体创建中。  
       
  Top

9 楼yayx(yayx)回复于 2003-06-01 17:06:30 得分 0

用互斥对象  
  这个以前太多了   你又不给分   所以你自己搜索一下吧Top

10 楼meica(赵一人)回复于 2003-06-01 17:11:57 得分 0

用控件,如justone等Top

11 楼eastnofail(Char *)回复于 2003-06-01 17:26:36 得分 0

用互斥对象:mutex,用windows的api函数createMutex函数创建一个互斥对象,每次程序一开始运行的时候判断这个mutex是否存在,如果存在则用findwindow找出程序的窗口,然后显示出来,如果不存在,则.....  
   
  你看看msdn,上面有craetemutex的用法Top

12 楼luo521(黑暗中的舞者)回复于 2003-06-01 17:33:10 得分 0

去看delphi5开发人员指南  
  把下面的程序加入到你的程序里就行了  
  unit   MultInst;  
   
  interface  
   
  const  
      MI_QUERYWINDOWHANDLE       =   1;  
      MI_RESPONDWINDOWHANDLE   =   2;  
   
      MI_ERROR_NONE                     =   0;  
      MI_ERROR_FAILSUBCLASS     =   1;  
      MI_ERROR_CREATINGMUTEX   =   2;  
   
  //   Call   this   function   to   determine   if   error   occurred   in   startup.  
  //   Value   will   be   one   or   more   of   the   MI_ERROR_*   error   flags.  
  function   GetMIError:   Integer;  
   
  implementation  
   
  uses   Forms,   Windows,   SysUtils;  
   
  const  
      UniqueAppStr   =   'DDG.I_am_the_Eggman!';  
   
  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   this   is   the   registered   message...  
      if   Msg   =   MessageID   then  
      begin  
          case   wParam   of  
              MI_QUERYWINDOWHANDLE:  
                  //   A   new   instance   is   asking   for   main   window   handle   in   order  
                  //   to   focus   the   main   window,   so   normalize   app   and   send   back  
                  //   message   with   main   window   handle.  
                  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:  
                  //   The   running   instance   has   returned   its   main   window   handle,  
                  //   so   we   need   to   focus   it   and   go   away.  
                  begin  
                      SetForegroundWindow(HWND(lParam));  
                      Application.Terminate;  
                  end;  
          end;  
      end  
      //   Otherwise,   pass   message   on   to   old   window   proc  
      else  
          Result   :=   CallWindowProc(WProc,   Handle,   Msg,   wParam,   lParam);  
  end;  
   
  procedure   SubClassApplication;  
  begin  
      //   We   subclass   Application   window   procedure   so   that  
      //   Application.OnMessage   remains   available   for   user.  
      WProc   :=   TFNWndProc(SetWindowLong(Application.Handle,   GWL_WNDPROC,  
          Longint(@NewWndProc)));  
      //   Set   appropriate   error   flag   if   error   condition   occurred  
      if   WProc   =   nil   then  
          MIError   :=   MIError   or   MI_ERROR_FAILSUBCLASS;  
  end;  
   
  procedure   DoFirstInstance;  
  //   This   is   called   only   for   the   first   instance   of   the   application  
  begin  
      //   Create   the   mutex   with   the   (hopefully)   unique   string  
      MutHandle   :=   CreateMutex(nil,   False,   UniqueAppStr);  
      if   MutHandle   =   0   then  
          MIError   :=   MIError   or   MI_ERROR_CREATINGMUTEX;  
  end;  
   
  procedure   BroadcastFocusMessage;  
  //   This   is   called   when   there   is   already   an   instance   running.  
  var  
      BSMRecipients:   DWORD;  
  begin  
      //   Prevent   main   form   from   flashing  
      Application.ShowMainForm   :=   False;  
      //   Post   message   to   try   to   establish   a   dialogue   with   previous   instance  
      BSMRecipients   :=   BSM_APPLICATIONS;  
      BroadCastSystemMessage(BSF_IGNORECURRENTTASK   or   BSF_POSTMESSAGE,  
          @BSMRecipients,   MessageID,   MI_QUERYWINDOWHANDLE,  
          Application.Handle);  
  end;  
   
  procedure   InitInstance;  
  begin  
      SubClassApplication;       //   hook   application   message   loop  
      MutHandle   :=   OpenMutex(MUTEX_ALL_ACCESS,   False,   UniqueAppStr);  
      if   MutHandle   =   0   then  
          //   Mutex   object   has   not   yet   been   created,   meaning   that   no   previous  
          //   instance   has   been   created.  
          DoFirstInstance  
      else  
          BroadcastFocusMessage;  
  end;  
   
  initialization  
      MessageID   :=   RegisterWindowMessage(UniqueAppStr);  
      InitInstance;  
  finalization  
      //   Restore   old   application   window   procedure  
      if   WProc   <>   Nil   then  
          SetWindowLong(Application.Handle,   GWL_WNDPROC,   LongInt(WProc));  
      if   MutHandle   <>   0   then   CloseHandle(MutHandle);     //   Free   mutex  
  end.  
   
  Top

相关问题

  • 怎样使程序只运行一个实例,并且当用户打开文件产生程序实例时自动用本程序的第一个实例打开
  • 关于程序实例
  • 第二次运行程序时如何做到只显示第一次程序而非第二次再打开?
  • 我不但要程序只能运行一个实例,还要它在当用户在资源管理器中打开它所注册的文档时,激活当前实例打开该文档,怎么办?
  • 如何让程序单实例运行?
  • 只运行一个JAVA程序实例
  • 求关于xml解析程序实例,和生成xml程序的实例
  • 怎样让VB程序只运行一个程序实例呢
  • 如何在程序等待一个程序的多个实例?
  • 打开应用程序慢

关键词

  • application
  • hwindow
  • 程序
  • muthandle
  • createform
  • 运行
  • 打开
  • 做到
  • findwindow
  • 请问

得分解答快速导航

  • 帖主:cjdbd

相关链接

  • Delphi类图书
  • Delphi类源码下载
  • Delphi控件下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo