随笔 - 2146  文章 - 97 评论 - 11767 trackbacks - 253


问题来源: http://www.cnblogs.com/del/archive/2008/01/17/1043226.html#1548952

使用 Delphi 提供的 TTrayIcon 类, 两三行程序甚至不写程序(设计时选择几下)即可实现.

设计时的准备工作:
1、添加 TTrayIcon、TPopupMenu 控件, TPopupMenu 用于托盘的右键菜单.
2、随意给 TPopupMenu 添加些菜单项.

代码文件:

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    TrayIcon1: TTrayIcon;
    PopupMenu1: TPopupMenu;
    aaa1: TMenuItem;
    bbb1: TMenuItem;
    ccc1: TMenuItem;
    procedure FormCreate(Sender: TObject);
    procedure TrayIcon1DblClick(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  {指定图标, 这里是用程序相同的图标; 也可在设计时选择, 还可以指定一个图标组, 再用 IconIndex 切换}
  TrayIcon1.Icon := Application.Icon;
  {指定右键菜单}
  TrayIcon1.PopupMenu := PopupMenu1;
  {使之可见}
  TrayIcon1.Visible := True;
end;

{给图标添加到双击事件}
procedure TForm1.TrayIcon1DblClick(Sender: TObject);
begin
  ShowMessage('OnDblClick');
end;

end.


窗体文件:

object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 206
  ClientWidth = 339
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object TrayIcon1: TTrayIcon
    OnDblClick = TrayIcon1DblClick
    Left = 152
    Top = 88
  end
  object PopupMenu1: TPopupMenu
    Left = 216
    Top = 88
    object aaa1: TMenuItem
      Caption = 'aaa'
    end
    object bbb1: TMenuItem
      Caption = 'bbb'
    end
    object ccc1: TMenuItem
      Caption = 'ccc'
    end
  end
end

posted on 2009-06-05 12:15  万一  阅读(...)  评论(...编辑  收藏