新问题就是当我的程序最小化的托盘时,用户二次打开程序(用户常犯这样的毛病),使托盘的程序醒来,而不是仅出一个提示!,winamp就是这样做的,如何实现,谢谢!

解决方案 »

  1.   

    程序写成如下结构var
      hMutex:hwnd;
      ret:integer;
      BroadcastList : DWORD;
      sName:String;{$R *.res}begin
      Application.Initialize;  sName:=...;//这个名称必须是唯一的,可以用GUID字符串
      BringTopMessage := RegisterWindowMessage(PChar(sName));//注册消息  hMutex:=CreateMutex(nil,false,PChar(sName));  ret:=GetLastError;
      if ret=ERROR_ALREADY_EXISTS then//程序已经运行
      begin
        BroadcastList := BSM_APPLICATIONS;
        BroadcastSystemMessage(BSF_POSTMESSAGE,@BroadcastList,BringTopMessage,0,0);//广播一个消息
      end
      else begin
        Application.CreateForm(TMDIFrame, MDIFrame);//你的主程序界面
      Application.Run;
        ReleaseMutex(hMutex);
      end;
      CloseHandle(hMutex);
    end.另外,在你的程序主窗体中作如下处理
    var
      BringTopMessage:UInt;//该变量定义在Interface段写如下函数
    procedure TMDIFrame.ApplicationMessage(var Msg: TMsg; var Handled: Boolean);
    begin
      if Msg.Message = BringTopMessage then
      begin
        //在这里写代码唤醒你的托盘程序
        Handled := true;
      end;
    end;//在主窗体的Create事件中加入如下代码
    procedure TMDIFrame.FormCreate(Sender: TObject);
    begin
      Application.OnMessage:=ApplicationMessage;
    end;这样就没有问题了
      

  2.   

    程序写成如下结构var
      hMutex:hwnd;
      ret:integer;
      BroadcastList : DWORD;
      sName:String;{$R *.res}begin
      Application.Initialize;  sName:=...;//这个名称必须是唯一的,可以用GUID字符串
      BringTopMessage := RegisterWindowMessage(PChar(sName));//注册消息  hMutex:=CreateMutex(nil,false,PChar(sName));  ret:=GetLastError;
      if ret=ERROR_ALREADY_EXISTS then//程序已经运行
      begin
        BroadcastList := BSM_APPLICATIONS;
        BroadcastSystemMessage(BSF_POSTMESSAGE,@BroadcastList,BringTopMessage,0,0);//广播一个消息
      end
      else begin
        Application.CreateForm(TMDIFrame, MDIFrame);//你的主程序界面
      Application.Run;
        ReleaseMutex(hMutex);
      end;
      CloseHandle(hMutex);
    end.另外,在你的程序主窗体中作如下处理
    var
      BringTopMessage:UInt;//该变量定义在Interface段写如下函数
    procedure TMDIFrame.ApplicationMessage(var Msg: TMsg; var Handled: Boolean);
    begin
      if Msg.Message = BringTopMessage then
      begin
        //在这里写代码唤醒你的托盘程序
        Handled := true;
      end;
    end;//在主窗体的Create事件中加入如下代码
    procedure TMDIFrame.FormCreate(Sender: TObject);
    begin
      Application.OnMessage:=ApplicationMessage;
    end;这样就没有问题了
      

  3.   

    《Delphi5开发人员指南》第13章中有非常详细的介绍。还有个mutex.pas单元直接引用就行。
      

  4.   

    不管是用哪种方式判断已经存在一个实例,关键是要在.dpr里写判别代码,如果发现已经存在实例后就退出,这时主窗体还未创建,即使你用控件实现托盘图标,由于窗体都没创建,此时是不会显示出第2个托盘图标的。
      

  5.   

    对,楼上说的很好!
    主要是判断的代码是要写在dpr里。
      

  6.   

    方法很多的,Google上N多的
    都是Mutex等的http://lysoft.7u7.net
      

  7.   

    用Mutex的话,如果程序死掉了,用任务管理器强行终止后,互斥量可能无法清除,再运行程序就运行不起来了。
      

  8.   

    我用Mutex,程序强制结束,XP系统下会自动清除 Mutex.