+ -
当前位置:首页 → 问答吧 → 急急急 拦截任务栏图标点击事件。

急急急 拦截任务栏图标点击事件。

时间:2011-12-19

来源:互联网

在窗口最小化后有个图标在任务栏上。
在点击任务栏上的图标是怎样拦截点击事件。
请高手解答。
先谢谢了。

作者: ynquan   发布时间: 2011-12-19

自定义消息可以完成下面是代码Delphi(Pascal) code
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Menus,ShellApi;

const
  MY_MESSAGE = WM_USER + 100;

type
  TfrmMain = class(TForm)
    PopupMenu1: TPopupMenu;
    mnuIcon: TMenuItem;
    procedure mnuIconClick(Sender: TObject);
  private
    { Private declarations }
    procedure IconOnClick(var message:TMessage); message MY_MESSAGE;
  public
    { Public declarations }
  end;

var
  frmMain: TfrmMain;
  NT:TNOTIFYICONDATA;

implementation

{$R *.dfm}

procedure TfrmMain.IconOnClick( var message: Tmessage);
var
    p : TPoint;
begin
    if (message.lParam = WM_LBUTTONDOWN) then
        ;
    if (message.lParam = WM_RBUTTONDOWN) then
    begin
        GetCursorPos(p);
        self.PopupMenu1.Popup( p.x ,p.y );
    end;
end;

procedure TfrmMain.mnuIconClick(Sender: TObject);
begin
    if Pos('缩为小图标',self.mnuIcon.Caption)>0 then
    begin
      with NT do begin
        cbSize:=Sizeof(NT);
        // nid变量的字节数
        Wnd:=Handle;
        // 主窗口句柄
        UID:=0;
        // 内部标识,可设为任意数
        uFlags:=NIF_MESSAGE or NIF_ICON or NIF_TIP;
        uCallBackMessage:=MY_MESSAGE;
        hIcon:=Icon.Handle;
        // 要加入的图标句柄,可任意指定
        szTip:='Delphi'#0;
        // 提示字符串
        hIcon := Application.Icon.Handle;
      end;
      Application.Minimize;
      ShowWindow(Application.Handle,SW_HIDE);
      Shell_NotifyIcon(NIM_ADD,@NT);
      self.mnuIcon.Caption:='正常显示';
    end
    else
    begin
      Shell_NotifyIcon(NIM_DELETE,@NT);
      ShowWindow(Application.Handle,SW_SHOW);
      Application.Restore;
      self.mnuIcon.Caption:='缩为小图标';
    end;
end;

end.

作者: changsn   发布时间: 2011-12-19