1、系统托盘组件 TTrayIcon 简介
--------------------------------------
TTrayIcon 的主要属性:
TrayIcon.Icon
指定托盘图标, 有几种用法:
1、设计时选择;
2、把一个 TIcon 对象给它;
3、使用当前程序图标: TrayIcon1.Icon := Application.Icon;
4、TrayIcon1.SetDefaultIcon; 这也是使用当前程序图标.
TrayIcon.Icons 与 TrayIcon1.IconIndex
TrayIcon.Icons 对应一个 TImageList, 用 TrayIcon.IconIndex 来指定使用 TImageList 中的第一个图标.
这结合 TrayIcon 的其他功能做出图标动画都没有问题.
TrayIcon.PopupMenu
TrayIcon.PopupMenu 对应一个 TPopupMenu, 托盘图标一般少不了右键菜单.
TrayIcon.Visible
隐藏或显示系统托盘的图标; 虽简单、但关键, 因默认是 False.
TrayIcon.Hint
随鼠标指针的提示文本, 这是老式的, 下面有新型的.
TrayIcon.BalloonHint、BalloonTitle、BalloonFlags、BalloonTimeout
这都是新型的 Hint 相关的.
TrayIcon.BalloonHint: Hint 文本
TrayIcon.BalloonTitle: Hint 标题
TrayIcon.BalloonFlags: Hint 图标样式
TrayIcon.BalloonTimeout: Hint 停留时间
需要用 TrayIcon 的 ShowBalloonHint 方法启动显示
TrayIcon.Animate 与 TrayIcon1.AnimateInterval
TrayIcon 的新 Hint(BalloonHint)可设定动画.
TrayIcon.Animate: 这决定是否启用动画
TrayIcon1.AnimateInterval: 动画间隔时间
TTrayIcon 的主要事件:
OnAnimate: 写动画代码的事件
OnBalloonClick: 点击 BalloonHint 时...
OnClick: 单击图标时...
OnDblClick
OnMouseDown
OnMouseMove
OnMouseUp
TTrayIcon 还有两个方法, 前面都已提到了:
TTrayIcon.SetDefaultIcon;
TTrayIcon.ShowBalloonHint;
==========================================================================================
-----------------------------------------------------------------------------------
使用 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
=========================================================================================
代码文件:
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, StdCtrls, AppEvnts; type TForm1 = class(TForm) TrayIcon1: TTrayIcon; Button1: TButton; procedure TrayIcon1Click(Sender: TObject); procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean); procedure Button1Click(Sender: TObject); end; var Form1: TForm1; implementation {$R *.dfm} {关闭} procedure TForm1.Button1Click(Sender: TObject); begin Application.Terminate; end; {点击关闭按钮时让窗体最小化到系统托盘} procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean); begin Tag := Ord(WindowState); WindowState := wsMinimized; TrayIcon1.SetDefaultIcon; TrayIcon1.Visible := True; Hide; CanClose := False; end; {点击系统托盘中的图标恢复窗口} procedure TForm1.TrayIcon1Click(Sender: TObject); begin TrayIcon1.Visible := False; Show; WindowState := TWindowState(tag); SetForegroundWindow(Handle); 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 OnCloseQuery = FormCloseQuery PixelsPerInch = 96 TextHeight = 13 object Button1: TButton Left = 232 Top = 40 Width = 75 Height = 25 Caption = 'Button1' TabOrder = 0 OnClick = Button1Click end object TrayIcon1: TTrayIcon OnClick = TrayIcon1Click Left = 160 Top = 104 end end
==================================================================================================
4、2012.08.03--我自己测试发现----------
--------------------------------------------------
============================================================================
点击最小化的时候 窗体 最小化 原理-----和--代码
procedure TfrmSmsEmailMain.FormResize(Sender: TObject); begin {点击最小化时候,最小化到系统托盘} case frmSmsEmailMain.WindowState of wsMinimized: begin Tag := Ord(WindowState); WindowState := wsMinimized; Hide; end; end; //或代码 if frmSmsEmailMain.WindowState = wsMinimized then begin Tag := Ord(WindowState); WindowState := wsMinimized; Hide; end; end;
本文来自博客园,作者:del88,转载请注明原文链接:https://www.cnblogs.com/del88/archive/2012/08/30/2663223.html