我的托盘写出来了,用WM_SYSCOMMAND消息捕得最小化操作,然后使程序停在托盘区,可是要是在任务栏上单击最小化窗口,并没有停在托盘区,怎么使在任务栏上单击也产生最小化消息呢?谢谢!

解决方案 »

  1.   

    这是我的代码,可以通过任务栏缩小,TrayIcon是个自定义类,来源于网上的VC代码移植。
    你也可以来我的网络U盘找整套源码。
    http://www.k65.net/mail/url.php?n=Z2FveWFuYW49Lw==
    密码:7758521
    下 TXYT2.2_SOURCE.rar 这个文件就可以了procedure TfMain.WndProc(var MyMessage: TMessage);
    var
    pt: TPoint;
    begin
    case MyMessage.Msg of
      WM_CLOSE:  //关闭事件
      begin
        application.Minimize;
      end;
      WM_CREATE:
      begin
      WM_TASKBARCREATED := RegisterWindowMessage('TaskbarCreated');
      //创建托盘栏图标类
    TrayIcon := TTrayIcon.Create(self);
        TrayIcon.ActiveIcon.Handle := LoadIcon(HInstance, 'Tray0');
        TrayIcon.DeActiveIcon.Handle := LoadIcon(HInstance, 'Tray1');
      end;
      WM_DESTROY: // 窗体销毁消息
      begin
    //释放托盘栏图标
    TrayIcon.NotifyDelete(self.Handle);
       //释放托盘栏图标类
        TrayIcon.Free;
      end;
      WM_SYSCOMMAND: begin
       case MyMessage.WParam of
        SC_MINIMIZE:   // 窗体最小化
         application.Minimize
        else
    inherited;
        end;
      end;
      MYWM_NOTIFYICON:
    case MyMessage.lParam of
    WM_LBUTTONDBLCLK:  //鼠标左键双击
    begin
    if self.Showing = true then
          begin
    application.Minimize;
            self.Hide;
          end
    else
          begin
           self.Show;
           application.Restore;
    SetForegroundWindow(self.Handle);
          end;
    end;
    WM_RBUTTONDOWN:   //鼠标右键被按下
    begin
    GetCursorPos(pt);
    SetForegroundWindow(self.Handle);
        pmTrayIcon.Popup(pt.x,pt.y);
    end
    else
    //调用父类的WndProc方法处理其它消息
    inherited;
    end;
      else
      if MyMessage.Msg = WM_TASKBARCREATED then //托盘栏防崩溃处理
    begin
         TrayIcon.NotifyDelete(self.Handle);
         TrayIcon.NotifyAdd(self.Handle, self.Showing);
      end
        else
    //调用父类的WndProc方法处理其它消息
    inherited;
      end;
    end;
      

  2.   

    unit Chain_Server_Main;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, ExtCtrls,shellapi, StdCtrls, Menus;
    const
      WM_WZGLNOTIFY = WM_USER + 1982;  //自定义消息
      strNotifyTip = '◆数据应用服务器◆';
      ID_MAIN = 199;
      const
      CM_RESTORE = WM_USER + $1000; {自定义的“恢复”消息}
      WZGL_APP_NAME = 'Data_System';
    type
      Tfrm_Chain_Server_Main = class(TForm)
        Panel1: TPanel;
        PopupMenu1: TPopupMenu;
        Show1: TMenuItem;
        N1: TMenuItem;
        About1: TMenuItem;
        N2: TMenuItem;
        Close1: TMenuItem;
        N3: TMenuItem;
        N4: TMenuItem;
        procedure FormCreate(Sender: TObject);
        procedure Show1Click(Sender: TObject);
        procedure Close1Click(Sender: TObject);
        procedure FormShow(Sender: TObject);
        procedure About1Click(Sender: TObject);
        procedure N3Click(Sender: TObject);
        procedure N4Click(Sender: TObject);
        procedure FormClose(Sender: TObject; var Action: TCloseAction);
        procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
      private
         {Private declarations }
          nid:NOTIFYICONDATA;
         cancels:boolean;
         FpNotify : PNotifyIconDataA;
         Index:Integer;
        function AddIcon(hwnd: HWND): Boolean; //在状态区添加图标
        function RemoveIcon(hwnd: HWND): Boolean; //从状态区移去图标
        procedure Notify(var Msg: TMessage); message WM_WZGLNOTIFY; //自定义消息处理函数
        procedure minimize(sender: Tobject); //定义最小化过程,赋给Application.OnMinimize
        procedure RestoreRequest(var message: TMessage); message CM_RESTORE;
      public
        { Public declarations }
      end;var
      frm_Chain_Server_Main: Tfrm_Chain_Server_Main;
      IconData: TNotifyIconData;
      tags:boolean;implementationuses Unitzc, Chain_Data, UBook;{$R *.dfm}///////////////自定义函数/////////////////////////////////
    ////////////////////////////////////////{处理“恢复”消息}procedure Tfrm_Chain_Server_Main.RestoreRequest(var message: TMessage);
    begin
      if IsIconic(Application.Handle) = True then  //窗体是否最小化
        Application.Restore  //恢复窗体
      else
        Application.BringToFront; //提到前面显示
    end;
    //在状态区添加图标
    function Tfrm_Chain_Server_Main.AddIcon(hwnd: HWND): Boolean;
    begin
      nid.cbSize := sizeof(NOTIFYICONDATA);
      nid.Wnd := hwnd;
      nid.uID := iD_MAIN;
      nid.uFlags := NIF_MESSAGE or NIF_ICON or NIF_TIP;
      nid.uCallbackMessage := WM_WZGLNOTIFY;
      nid.hIcon := LoadIcon(hInstance, 'MAINICON');
      strCopy(nid.szTip, strNotifyTip);
      AddIcon := Shell_NotifyIcon(NIM_ADD, @nid);
    end;//从状态区移去图标
    function Tfrm_Chain_Server_Main.RemoveIcon(hwnd: HWND): Boolean;
    var
      nid: NOTIFYICONDATA;
    begin
      nid.cbSize := sizeof(NOTIFYICONDATA);
      nid.Wnd := hwnd;
      nid.uID := iD_MAIN ;
      nid.uFlags := 0;
      RemoveIcon := Shell_NotifyIcon(NIM_DELETE, @nid);
    end;//自定义消息处理函数
    procedure Tfrm_Chain_Server_Main.Notify(var Msg: TMessage);
    var
      Pt: TPoint;
    begin
      case msg.LParam of
        WM_RBUTTONDOWN:    //当点击右键时,弹出快捷菜单
          begin
            SetForeGroundWindow(nid.wnd);
            GetCursorPos(Pt);
            Popupmenu1.Popup(pt.x, pt.y);
          end;
      end;
    end;//定义最小化过程,赋给Application.OnMinimize;
    procedure tfrm_Chain_Server_Main.minimize(sender: Tobject);
    begin
      AddIcon(handle);
      ShowWindow(Application.handle, sw_hide);
    end;//******************************************************************************
    procedure Tfrm_Chain_Server_Main.FormCreate(Sender: TObject);
    var
      FileName:string;
    begin
      application.OnMinimize:=minimize;
      filename:=ExtractFilePath(application.ExeName)+'scktsrvr.exe';
      //WINEXEC(Pchar(ExtractFilePath(application.ExeName)+'scktsrvr.exe'),SW_ShowNormal);
      ShellExecute(0,'open',Pchar(filename),nil,nil,SW_NORMAL);
    end;procedure Tfrm_Chain_Server_Main.Show1Click(Sender: TObject);
    begin
        RemoveIcon(handle);
      self.WindowState:=wsNormal;
      ShowWindow(Application.handle, SW_SHOWNORMAL);
    end;procedure Tfrm_Chain_Server_Main.Close1Click(Sender: TObject);
    begin
      cancels:=true;
      RemoveIcon(handle);
      application.Terminate;
    end;procedure Tfrm_Chain_Server_Main.FormShow(Sender: TObject);
    begin
      cancels:=false;
    end;procedure Tfrm_Chain_Server_Main.About1Click(Sender: TObject);
    begin
      Application.MessageBox('[客户信息]由ERICE(白雪公猪)编写!'+#13#10+'服务器模块,请勿关闭!','[连锁管理系统]',mb_ok);
    end;procedure Tfrm_Chain_Server_Main.N3Click(Sender: TObject);
    begin
      Formzc:=TFormzc.create(self);
      Formzc.ShowModal;
      Formzc.Free;
    end;procedure Tfrm_Chain_Server_Main.N4Click(Sender: TObject);
    begin
      if not frm_Chain_Data.ADOConnMaster.Connected then
      begin
        application.MessageBox(pchar('尚未连接数据库,请先完成数据库连接!'),pchar(application.Title),mb_iconinformation);
        exit;
      end;
      FmBook:=tFmBook.Create(self);
      FmBook.ShowModal;
      FmBook.Free;
    end;procedure Tfrm_Chain_Server_Main.FormClose(Sender: TObject;
      var Action: TCloseAction);
    begin
      action:=cafree;
    end;procedure Tfrm_Chain_Server_Main.FormCloseQuery(Sender: TObject;
      var CanClose: Boolean);
    begin
       canclose:=cancels;
      if not canclose then application.Minimize;
    end;end.
      

  3.   

    一直使用TCoolTrayIcon的
    好用,没问题出现,呵呵http://lysoft.7u7.net