我写的程序点窗口最小化的时候在右下脚任务栏上显示图标的效果,在点击图标后没触发消息事件,大家帮忙看看怎么回事:
... ... 
 private
    procedure AppMinimize(Sender: TObject);
    { Private declarations }
  public
    procedure OnIconNotify(var Msg: TMessage);message mymessage; 
//这个放“private”效果也一样
  end;var
  nd:TNotifyIconData;
procedure Tfrm_backsybdate.FormCreate(Sender: TObject);
begin
   Application.OnMinimize := AppMinimize;
end;//最小化时缩为任务栏右下小图标,执行时正常
procedure Tfrm_backsybdate.AppMinimize(Sender: TObject);
begin
   nd.cbSize := sizeof(TNotifyIconData);
   nd.Wnd := application.handle;
   nd.uID := 10;
   nd.uFlags := NIF_MESSAGE or NIF_ICON or NIF_TIP;
   nd.uCallbackMessage := mymessage;
   nd.hIcon := Application.Icon.Handle;
   StrPLCopy(nd.szTip, 'Sybase DataBase BackUp Tool', 63);
   Shell_NotifyIcon(NIM_ADD, @nd);
   showwindow(application.handle,sw_hide);                 
end;
//图标产生在右下后点鼠标的事件,执行的时候没触发
procedure Tfrm_backsybdate.OnIconNotify(var Msg: TMessage);
var
  nddel:TNotifyIconData;
begin
    case msg.lParam of
      ...
      WM_LBUTTONDBLCLK:
      begin
        nddel.cbSize := sizeof(TNotifyIconData);
        nddel.Wnd := application.handle;
        nddel.uID := 16;
        nddel.uFlags := NIF_MESSAGE or NIF_ICON or NIF_TIP;
        nddel.uCallbackMessage := mymessage;
        nddel.hIcon := Application.Icon.Handle;
        StrPLCopy(nddel.szTip, 'Sybase DataBase BackUp Tool', 63);
        Shell_NotifyIcon(NIM_DELETE, @nddel);
        frm_backsybdate.Visible:=true;
      end;
      ...
    end; //end case
end;对消息处理不是很清楚,大家知道的帮忙,送分报答

解决方案 »

  1.   

    首先
    nd.uCallbackMessage := mymessage;
    中的mymessage应该是一个常量
    一般定义成
    const
     mymessage = wm_user+100;//不一定是100,由你定
    定义一个过程
    private
      procedure ECHO_mymessage(var msg:TMessage);message mymessage;
    实现它
    procedure Tfrm_backsybdate.ECHO_mymessage(var msg:TMessage);message mymessage;         
    begin
     //现在处理消息把,比如下面
      case msg.lparam of
         WM_LBUTTONDOWN://鼠标左键按下
         begin
           //你的代码
         end;
      end;
    end;
      

  2.   

    楼上的对,给你一个我用到的,可在www.xmflyfish.com/awind/adkiller.rar下载:
    网上可找到的[秋风网页广告拦截器]源码
    ...
    const WM_NID = WM_USER + 200;
    ...
      private
        { Private declarations }
        procedure IconOnClick(var Msg: TMessage); message WM_NID;
    ...
    procedure TfrmAdKiller.IconOnClick(var Msg: TMessage);
    var
      mp: TPoint;
    begin
      if Msg.LParam = WM_LBUTTONDBLCLK then
        {显示窗体,删除图标}
      else if Msg.LParam = WM_RBUTTONUP then
      begin
        {弹出右键菜单}
        GetCursorPos(mp);
        SetForegroundWindow(self.Handle); {解决弹出右键菜单后不操作不消失问题}
        pmMain.Popup(mp.X, mp.Y);
      end
      else
        Inherited;
    end;
      

  3.   

    procedure MyIconNotify(var Msg: TMessage); message MYWM_NOTIFYICON; // 自定义消息procedure TForm1.MyIconNotify(var Msg: TMessage);
    var
      nID: UINT;
      uMouseMsg: UINT;
    begin
      nID := UINT(Msg.wParam);
      uMouseMsg := UINT(Msg.lParam);
      if uMouseMsg = WM_LBUTTONDBLCLK then
      begin
        if not LbMin then
          ShowWindow(Self.Handle, SW_HIDE)
        else
          ShowWindow(Self.Handle, SW_SHOWNORMAL);
      end;
      LbMin := not LbMin;
    end;function AddTaskBarIcon(hWinWnd: HWND; uID: UINT; hicon: HICON; msg: UINT; lpszTip: string): LongBool;
    var
      bResult: LongBool;
      NotifyData: NOTIFYICONDATA;
    begin
      NotifyData.cbSize := sizeof(NOTIFYICONDATA);
      NotifyData.Wnd := hWinWnd;
      NotifyData.uID := uID;
      NotifyData.uFlags := NIF_ICON or NIF_TIP or NIF_MESSAGE;
      NotifyData.hIcon := hicon;
      NotifyData.uCallbackMessage := msg;
      if lpszTip <> '' then
        lstrcpyn(NotifyData.szTip, PChar(lpszTip), sizeof(NotifyData.szTip))
      else
        lstrcpyn(NotifyData.szTip, PChar('Test'), sizeof(NotifyData.szTip));
      bResult := Shell_NotifyIcon(NIM_ADD, @NotifyData);
      if hicon <> 0 then
        DestroyIcon(hicon);
      Result := bResult;
    end;
      

  4.   

    建立托盘和删除托盘图标 TNotifyIconData变量要一致。
    ------------------------------------
    CSDN论坛新助手 CSDN's forum Explorer》
      

  5.   

    const那段我有,procedure Tfrm_backsybdate.OnIconNotify(var Msg: TMessage);
    //这里后面一定要有“message mymessage;”吗?//我写成这样时,编译不通过
    procedure Tfrm_backsybdate.OnIconNotify(var Msg: TMessage); message mymessage;
    //提示错误:Unknown directive:'message'
      

  6.   

    我说的是
    procedure Tfrm_backsybdate.OnIconNotify(var Msg: TMessage); 
    这个没反应
    procedure Tfrm_backsybdate.OnIconNotify(var Msg: TMessage);
    var
      nddel:TNotifyIconData;
    begin
       showmessage('hello!'); //就是鼠标产生消息后,这个也没效果,说明这个过程没对消息进行处理
       case msg.lParam of
          ...
          WM_LBUTTONDBLCLK:
          begin
            nddel.cbSize := sizeof(TNotifyIconData);
            nddel.Wnd := application.handle;
            nddel.uID := 16;
            nddel.uFlags := NIF_MESSAGE or NIF_ICON or NIF_TIP;
            nddel.uCallbackMessage := mymessage;
            nddel.hIcon := Application.Icon.Handle;
            StrPLCopy(nddel.szTip, 'Sybase DataBase BackUp Tool', 63);
            Shell_NotifyIcon(NIM_DELETE, @nddel);
            frm_backsybdate.Visible:=true;
          end;
          ...
        end; //end case
    end;
      

  7.   

    非常对不起,我没太注意看你的程序,以下是我改过的代码,能正确运行
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs,ShellApi, AppEvnts, ImgList;
    const
      mymessage = wm_user+100;
    type
      Tfrm_backsybdate = class(TForm)
        ImageList1: TImageList;
        procedure FormCreate(Sender: TObject);
      private
        procedure AppMinimize(Sender: TObject);
      public
        procedure OnIconNotify(var Msg: TMessage);message mymessage;
      end;var
      frm_backsybdate: Tfrm_backsybdate;
      nd:TNotifyIconData;
    implementation{$R *.dfm}
    procedure Tfrm_backsybdate.OnIconNotify(var Msg: TMessage);
    begin
      application.Restore;
      SetForegroundWindow(Application.Handle);
      Shell_NotifyIcon(NIM_DELETE, @nd);
      inherited;
    end;
    procedure Tfrm_backsybdate.AppMinimize(Sender: TObject);
    begin
       nd.cbSize := sizeof(nd);
       nd.Wnd := frm_backsybdate.handle;
       nd.uID := 0;
       nd.uFlags :=NIF_ICON or NIF_TIP  or NIF_MESSAGE;
       nd.uCallbackMessage := mymessage;
       nd.hIcon := Application.Icon.Handle;
       StrPLCopy(nd.szTip, 'Sybase DataBase BackUp Tool', 63);
       Shell_NotifyIcon(NIM_ADD, @nd);   showwindow(application.handle,sw_hide);
       inherited;
    end;
    procedure Tfrm_backsybdate.FormCreate(Sender: TObject);
    begin
      Application.OnMinimize := AppMinimize;
    end;
    end.
      

  8.   

    主要问题在这里:
    nd.Wnd := application.handle;
    改成
    nd.Wnd := frm_backsybdate.handle;
    就可以
    另外没有必要再弄出个nddel:TNotifyIconData;
    直接接着用nd就可以了
      

  9.   

    另外Shell_NotifyIcon这个API函数也可以实现气泡窗口的,但不能用TNotifyIconData这个结构要自定义一个结构
      

  10.   

    怎么揭贴啊,bookong(不空) 说的正确
      

  11.   

    这是我根据大名鼎鼎的CoolTray控件代码修改的,^_^虽然不是原创但也是心血啊。有兴趣的参考一下
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, AppEvnts, ImgList, ExtCtrls, Menus,ShellApi, StdCtrls;
    const
      ghy_tray=wm_user+2;  // dwState to TNewNotifyIconData structure
      NIS_HIDDEN      = $00000001;          // 隐藏图标
      NIS_SHAREDICON  = $00000002;          // 共享图标  //dwInfoFlags to TNewNotifyIconData structure
      NIIF_NONE       = $00000000;          // 无图标
      NIIF_INFO       = $00000001;          // "消息"图标
      NIIF_WARNING    = $00000002;          // "警告"图标
      NIIF_ERROR      = $00000003;          // "错误"图标  //uFlags to TNewNotifyIconData structure
      NIF_ICON        = $00000002;
      NIF_INFO        = $00000010;
      NIF_MESSAGE     = $00000001;
      NIF_STATE       = $00000008;
      NIF_TIP         = $00000004;type
      TTimeoutOrVersion = record
        case Integer of          // 0: Before Win2000; 1: Win2000 and up
          0: (uTimeout: UINT);
          1: (uVersion: UINT);   // Only used when sending a NIM_SETVERSION message
      end;
      TNotifyIconDataEx = record
        cbSize: DWORD;
        hWnd: HWND;
        uID: UINT;
        uFlags: UINT;
        uCallbackMessage: UINT;
        hIcon: HICON;
        szTip: array[0..127] of AnsiChar;  // Previously 64 chars, now 128
        dwState: DWORD;
        dwStateMask: DWORD;
        szInfo: array[0..255] of AnsiChar;
        TimeoutOrVersion: TTimeoutOrVersion;
        szInfoTitle: array[0..63] of AnsiChar;
        dwInfoFlags: DWORD;
    {$IFDEF _WIN32_IE_600}
        guidItem: TGUID;  // Reserved for WinXP; define _WIN32_IE_600 if needed
    {$ENDIF}
      end;  TBalloonHintIcon = (bitNone, bitInfo, bitWarning, bitError);
      TBalloonHintTimeOut = 10..60;   // Windows defines 10-60 secs. as min-max
      TBehavior = (bhWin95, bhWin2000);
      THintString = AnsiString;       // 128 bytes, last char should be #0  TForm1 = class(TForm)
        ImageList1: TImageList;
        Timer1: TTimer;
        PopupMenu1: TPopupMenu;
        N1: TMenuItem;
        N2: TMenuItem;
        N3: TMenuItem;
        Button1: TButton;
        ApplicationEvents1: TApplicationEvents;
        procedure ApplicationEvents1Minimize(Sender: TObject);
        procedure ApplicationEvents1Restore(Sender: TObject);
        procedure FormClose(Sender: TObject; var Action: TCloseAction);
        procedure FormCreate(Sender: TObject);
        procedure Timer1Timer(Sender: TObject);
        procedure N1Click(Sender: TObject);
        procedure N2Click(Sender: TObject);
        procedure N3Click(Sender: TObject);
        procedure Button1Click(Sender: TObject);
      private
      procedure WMSysCommand(var msg: TMessage);message wm_syscommand;
      { Private declarations }
      public
      procedure mytray(var Msg: TMessage); message ghy_tray;
      { Public declarations }
      end;
    var
      Form1: TForm1;
      tray:TNotifyIconDataEx;
      ico1,ico2:ticon;
      tags:boolean;
      ico_index:integer;
    implementation
    {$R *.dfm}
    procedure Tform1.WMSysCommand(var msg: TMessage);
    begin
      if msg.WParam = SC_MINIMIZE then
      begin
        showwindow(application.handle,sw_hide);
      end;
      inherited;
    end;procedure tform1.mytray(var Msg: TMessage);
    var
      pt:tpoint;
    begin
      GetCursorPos(pt);
      case msg.lParam of
        WM_LBUTTONDOWN:
        begin
        //鼠标左键被按下
          if tags=true then
          begin
            application.Minimize;
            form1.Hide;      end
          else
          begin
            application.Restore;
            SetForegroundWindow(Application.Handle);
          end;
        end;    WM_LBUTTONUP:
        begin
        //释放鼠标左键
        end;    wm_rbuttondown:
        begin
        //鼠标右键被按下
          SetForegroundWindow(Form1.Handle);
          form1.PopupMenu1.Popup(pt.x,pt.y);
        end
        else//调用父类的WndProc方法处理其它消息
        inherited;
        end;
    end;procedure TForm1.ApplicationEvents1Minimize(Sender: TObject);
    begin
      Timer1.Enabled:=true;
      tags:=false;
      ShowWindow(Application.Handle,SW_HIDE);  tray.hIcon:=ico2.Handle;  Shell_NotifyIconA(NIM_modify,@tray);
    end;
    procedure TForm1.ApplicationEvents1Restore(Sender: TObject);
    begin
      ico_index:=2;
      tags:=true;
      form1.show;
      self.Timer1.Enabled:=false;  tray.hIcon:=ico1.Handle;
      Shell_NotifyIconA(NIM_modify,@tray);
    end;
    procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
    begin  tray.hIcon:=ico1.Handle;  Shell_NotifyIconA(NIM_delete,@tray);
    end;
    procedure TForm1.FormCreate(Sender: TObject);
    begin  ico1:=ticon.Create;
      ico2:=ticon.create;  tags:=true;  imagelist1.GetIcon(0,ico1);
      imagelist1.GetIcon(1,ico2);  tray.cbSize:=sizeof(tray);
      tray.hWnd:=form1.Handle;
      tray.uID:=0;
      tray.uFlags:=NIF_ICON + NIF_MESSAGE + NIF_TIP + NIF_INFO;
      tray.uCallbackMessage:=ghy_tray;
      tray.hIcon:=ico1.Handle;
      tray.szTip:='PLC实时监控';
      tray.dwState:=0;
      tray.dwStateMask:=0;
      tray.szInfo:='这是气球提示'+chr(0);
      tray.szInfoTitle:='气球提示'+chr(0);
      tray.dwInfoFlags:=NIIF_INFO;
      tray.TimeoutOrVersion.uTimeout:= 1000;
      Shell_NotifyIconA(NIM_ADD,@tray);end;procedure TForm1.Timer1Timer(Sender: TObject);
    begin
      ico_index:=ico_index+1;
      if ico_index>3 then
      begin
        ico_index:=1;
      end;  imagelist1.GetIcon(ico_index,ico2);  tray.hIcon:=ico2.Handle;
      tray.uFlags:=NIF_ICON + NIF_MESSAGE + NIF_TIP;
      Shell_NotifyIconA(NIM_modify,@tray);
    end;procedure TForm1.N1Click(Sender: TObject);
    begin
      showmessage('you click open');
    end;procedure TForm1.N2Click(Sender: TObject);
    begin
      showmessage('you click save');
    end;procedure TForm1.N3Click(Sender: TObject);
    begin
      form1.Close;
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin  tray.dwInfoFlags:=NIIF_WARNING;
      tray.uFlags:=NIF_ICON + NIF_MESSAGE + NIF_TIP + NIF_INFO;
      Shell_NotifyIconA(NIM_modify,@tray);
    end;end.
      

  12.   

    我的绝对没问题。
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, ShellApi;const
      mymessage = wm_user+100;type
      TForm1 = class(TForm)
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
        procedure AppMinimize(Sender: TObject);
        procedure OnIconNotify(var Msg: TMessage); message mymessage;
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}
    var
      nd:TNotifyIconData;//最小化时缩为任务栏右下小图标,执行时正常
    procedure TForm1.AppMinimize(Sender: TObject);
    begin
       nd.cbSize := sizeof(TNotifyIconData);
       nd.Wnd := handle;
       nd.uID := 16;
       nd.uFlags :=NIF_ICON or NIF_TIP  or NIF_MESSAGE;
       nd.uCallbackMessage := mymessage;
       nd.hIcon := Application.Icon.Handle;
       StrPLCopy(nd.szTip, 'Sybase DataBase BackUp Tool', 63);
       Shell_NotifyIcon(NIM_ADD, @nd);
       showwindow(application.handle,sw_hide);
    //   inherited;
    end;
    //图标产生在右下后点鼠标的事件,执行的时候没触发
    procedure TForm1.OnIconNotify(var Msg: TMessage);
    var
      nddel:TNotifyIconData;
    begin
        case msg.lParam of
          WM_LBUTTONDBLCLK:
          begin
            nddel.cbSize := sizeof(TNotifyIconData);
            nddel.Wnd := handle;
            nddel.uID := 10;
            nddel.uFlags := NIF_MESSAGE or NIF_ICON or NIF_TIP;
            nddel.uCallbackMessage := mymessage;
            nddel.hIcon := Application.Icon.Handle;
            StrPLCopy(nddel.szTip, 'Sybase DataBase BackUp Tool', 63);
            Shell_NotifyIcon(NIM_DELETE, @nddel);
            Visible:=true;
            showwindow(application.handle,sw_RESTORE);
          end;
        end; //end case
      inherited;
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
      Application.OnMinimize := AppMinimize;
    end;end.