delphi下怎样实现把exe程序,放到托盘(屏幕右下角)上 谢谢!
解决方案 »
- 在NT服务程序中使用TWebBrowser控件异常(高手请多多帮忙........)
- 我也散个分吧!
- TEDIT怎么样才能得到焦点而又不会被全选?
- 关于查询排序显示的一个问题,急~~
- 再问一个有关TstringList的问题
- 新手初学
- 请教FORMAT的用法
- csdn上的朋友大家好,我的信箱是263的,要收费了,我得换一个,请各位给推荐一个哪个网的信箱好,而且免费
- http://www.csdn.net/Expert/topic/490/490503.shtm? up 有分!!!!
- 同一幅图,一幅一JPG存储,另一幅以数据流形式存在数据库中,到底哪一种读取速度快???
- Delph5中可以用的.ocx控件为什么在Delphi7中用不了?
- 紧急求助!!!!!怎样用两个DBGrid实现一个显示一览表,一个显示鼠标选中的记录?
TNotifyIcon 控件1.01 (Build use Delphi 3.0) 说明:作用:往通知区加图标,并可显示,隐藏,修改这个图标. 属性(properties):NotifyIcon:TIcon 欲加在通知区的图标IsVisible:boolean NotifyIcon是否显示的属性Title:string 通知区图标上的提示(最多64个字符)PopupMenu:TPopupMenu 点击通知区图标弹出的菜单PopupStyle:TPopupStyle 弹出菜单的方式TPopupStyle=Set of (Left_Click,Right_Click,Left_DbClick,Right_DbClick);方法(methods):ShowIcon 将图标显示在通知区上HideIcon 将通知区上的图标隐藏ModifyIcon 修改通知区上的图标(若IsVisible=false,则不显示出来)Create(AOwner: TComponent); override; 构造方法Destroy; override; 析构方法事件(Events):OnIconMouseDown:procedure(Sender:TObject;x,y:Word;WhoButton:TWhoButton) of Object;(在Mouse点击通知区上的图标时发生,x,y为Mouse在屏幕上的坐标,WhoButton=b_Left为点击左键,WhoButton=b_Right为点击右键,)OnIconDoubleClick:procedure(Sender:TObject;x,y:Word;WhoButton:TWhoButton) of Object;(在Mouse双击通知区上的图标时发生,x,y为Mouse在屏幕上的坐标,WhoButton=b_Left为双击左键,WhoButton=b_Right为双击右键,)关于Demo:这个演示程序给出了TNotifyIcon的基本用法.包含文件:NotifyIcon.dcrNotifyIcon.pasDemoUnit.pasDemoUnit.dfmPopUnit.pasPopUnit.dfmDemo.dprReadme.txt声明:TNotifyIcon 控件 V 1.011.这是一个免费控件.2.如果你使用它,请发一个E-Mail给作者,谢谢.3我在Delphi3.0 & 4.0 上使用成功4.若要传播它,请完全分发上述8个文件作者 南昌大学计算系95(1) 付昱纲 1998.8.17 21:50E-mail [email protected] NotifyIcon;interfaceusesWindows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,DsgnIntf,ShellApi,ExtCtrls,Menus;constWM_MY_Notify=WM_USER+100;typeTPopupStyle=Set of (Left_Click,Right_Click,Left_DbClick,Right_DbClick);TWhoButton=(b_Left,b_Right);TMouseEvent=procedure(Sender:TObject;x,y:Word;WhoButton:TWhoButton)of Object;//---------class TNotifyIcon---------TNotifyIcon = class(TCustomControl)private{ Private declarations }FIcon:TIcon;FPda:PNOTIFYICONDATA;FTitle:string;FIconVisible:boolean;FPopupMenu:TPopupMenu;FPopupStyle:TPopupStyle;FOnIconMouseDown:TMouseEvent;FOnIconDoubleClick:TMouseEvent;procedure SetIcon(Icon:TICON);procedure SetTitle(NewTitle:string);function IsShowing:boolean;procedure ShowIt(Accept:boolean);procedure NotifyIconClick(var msg : TMessage);Message WM_My_Notify;protected{ Protected declarations }public{ Public declarations }property IsVisible:boolean read IsShowing write ShowIt;constructor Create(AOwner: TComponent); override;procedure ShowIcon;procedure HideIcon;destructor Destroy; override;procedure ModifyIcon(NewIcon:TIcon);procedure Paint;override;published{ Published declarations }property Height default 33;property Width default 33;property NotifyIcon:TIcon read FIcon write SetIcon;property Title:string read FTitle write SetTitle ;property OnIconDoubleClick:TMouseEventread FOnIconDoubleClick write FOnIconDoubleClick;property OnIconMouseDown:TMouseEventread FOnIconMouseDown write FOnIconMouseDown;property PopupMenu:TPopupMenu read FPopupMenu write FPopupMenu;property PopupStyle:TPopupStyle read FPopupStylewrite FPopupStyle default [];end;procedure Register;implementationprocedure Register;beginRegisterComponents('MyControl', [TNotifyIcon]);end;procedure TNotifyIcon.ShowIt(Accept:boolean);beginif Accept=true then ShowIconelse HideIcon;end;procedure TNotifyIcon.Paint;beginif (csDesigning in ComponentState) thenbeginWidth:=33;Height:=33;With Canvas dobeginBrush.Color:=clInfoBk;Ellipse(0,0,Self.Width,Self.Height);Font.Color:=clBlue;Brush.Style:=bsClear;FloodFill(5,5,clInfoBk,fsBorder);Brush.Color:=clInfoBk;TextOut(3,Self.Height div 2-6,'Notify');endend;end;procedure TNotifyIcon.NotifyIconClick(var msg : TMessage);var p:TPoint;begintrycase msg.LParam ofWM_LBUTTONDOWN:beginGetCursorPos(p);if Left_Click in FPopupStyle thenbeginSetForegroundWindow(ParentWindow);FPopupMenu.Popup(p.x,p.y);end;if Assigned(FOnIconMouseDown) thenbeginFOnIconMouseDown(Self,p.x,p.y,b_Left);end;end;WM_RBUTTONDOWN:beginGetCursorPos(p);if Right_Click in FPopupStyle thenbeginSetForegroundWindow(ParentWindow);FPopupMenu.Popup(p.x,p.y);end;if Assigned(FOnIconMouseDown) thenbeginFOnIconMouseDown(Self,p.x,p.y,b_Right);end;end;WM_LBUTTONDBLCLK:beginGetCursorPos(p);if Left_DbClick in FPopupStyle thenbeginSetForegroundWindow(ParentWindow);FPopupMenu.Popup(p.x,p.y);end;if Assigned(FOnIconDoubleClick) thenbeginFOnIconDoubleClick(Self,p.x,p.y,b_Left);end;end;
鼠标右键点击这些小图标时会弹出菜单,通过选择这些菜单可以灵活地实现程序的各项功能。
下面本文以一个具体的例子,详细介绍一下利用Delphi实现系统状态栏图标的步骤和方法。
首先,介绍一下本实例要实现的功能:程序开始运行时会在系统状态栏生成一个小图标,同时会打开应用程序窗口,并在任务栏上显示相应的程序窗口;当用户关闭应用程序窗口时,该窗口
和任务栏上相应的应用程序窗口都会消失,但应用程序并没有退出;当用户用鼠标左健单击该图标时,会再次打开应用程序窗口,同时在任务栏上显示应用程序窗口;当用户用鼠标右键单击
系统状态栏中应用程序的小图标时,会弹出菜单,选择菜单项“退出”可以完全退出应用程序。
一、 实现步骤
1. 创建一个应用程序,在主窗体上增加一个TpopupMenu组件。并为该弹出菜单组件增加菜单项Exit,标题为“退出”。
2. 在Uses中添加ShellAPI,因为在系统状态栏中增加图标时需调用ShellAPI函数Shell_NotifyIconA。该函数需要2个参数,其中一个是TnotifyIconDataA结构,需在主窗体中增加
TnotifyIconDataA类型全局变量ntida。
3. 定义消息mousemsg,并编写主窗体的mousemessage消息处理函数,此函数说明在图标上用鼠标左键单击时,会打开应用程序窗口;用鼠标右键单击时,会弹出一个菜单。
下面给出步骤2和3的实现代码:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls, Menus, shellapi;
const
mousemsg = wm_user + 1; //自定义消息,用于处理用户在图标上点击鼠标的事件
iid = 100; //用户自定义数值,在TnotifyIconDataA类型全局变量ntida中使用。
type
TForm1 = class(TForm)
......
private
//自定义消息处理函数,处理鼠标点击图标事件
procedure mousemessage(var message: tmessage); message mousemsg;
public
{ Public declarations }
end;
var
Form1: TForm1;
ntida: TNotifyIcondataA; //用于增加和删除系统状态图标
implementation
{$R *.DFM}
procedure TForm1.mousemessage(var message: tmessage);
var
mousept: TPoint; //鼠标点击位置
begin
inherited;
if message.LParam = wm_rbuttonup then begin //用鼠标右键点击图标
getcursorpos(mousept); //获取光标位置
popupmenu1.popup(mousept.x, mousept.y); //在光标位置弹出菜单
end;
if message.LParam = wm_lbuttonup then begin //用鼠标左键点击图标
//显示应用程序窗口
ShowWindow(Handle, SW_SHOW);
//在任务栏上显示应用程序窗口
ShowWindow(Application.handle, SW_SHOW);
SetWindowLong(Application.Handle, GWL_EXSTYLE,
not (GetWindowLong(Application.handle, GWL_EXSTYLE)
or WS_EX_TOOLWINDOW AND NOT WS_EX_APPWINDOW));
end;
message.Result := 0;
end
4. 编辑TForm1.FormCreate(Sender: TObject)
应用程序开始运行时,在系统状态栏上生成图标显示,代码如下:
procedure TForm1.FormCreate(Sender: TObject);
begin
ntida.cbSize := sizeof(tnotifyicondataa); //指定ntida的长度
ntida.Wnd := handle; //取应用程序主窗体的句柄
ntida.uID := iid; //用户自定义的一个数值,在uCallbackMessage参数指定的消息中使用
ntida.uFlags := nif_icon + nif_tip + nif_message;//指定在该结构中uCallbackMessage、hIcon、szTip参数都有效
ntida.uCallbackMessage := mousemsg;//指定的窗口消息
ntida.hIcon := Application.Icon.handle;//指定系统状态栏显示应用程序的图标句柄
ntida.szTip := 'Icon'; //当鼠标停留在系统状态栏该图标上时,出现该提示信息
shell_notifyicona(NIM_ADD, @ntida); //在系统状态栏增加一个新图标
end;
5. 编辑Tform1.OnClose
当用户关闭应用程序窗口时,该窗口和任务栏上相应的应用程序窗口都消失,但程序并没有退出。代码如下:
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caNone; //不对窗体进行任何操作
ShowWindow(Handle, SW_HIDE); //隐藏主窗体
//隐藏应用程序窗口在任务栏上的显示
ShowWindow(Application.Handle, SW_HIDE);
SetWindowLong(Application.Handle, GWL_EXSTYLE,
GetWindowLong(Application.handle, GWL_EXSTYLE)
or WS_EX_TOOLWINDOW AND NOT WS_EX_APPWINDOW);
end;
6. 编辑弹出菜单Exit
当用户点击该菜单时完全退出应用程序。代码如下:
procedure TForm1.ExitClick(Sender: TObject);
begin
//为ntida赋值,指定各项参数
ntida.cbSize := sizeof(tnotifyicondataa);
ntida.wnd := handle;
ntida.uID := iid;
ntida.uFlags := nif_icon + nif_tip + nif_message;
ntida.uCallbackMessage := mousemsg;
ntida.hIcon := Application.Icon.handle;
ntida.szTip := 'Icon';
shell_notifyicona(NIM_DELETE, @ntida); //删除已有的应用程序图标
Application.Terminate; //中断应用程序运行,退出应用程序
end;
二、 技术要点
1.程序中在增加或删除系统状态栏图标时,需调用ShellAPI函数Shell_NotifyIconA,该函数有两个参数,其中一个是TnotifyIconDataA结构,在前面的程序注释中已经对其进行了介绍;另
一个参数是dwMessage,通过不同的取值表示是增加图标、修改图标或删除图标。
2.通过调用一组API函数,实现在任务栏上显示或隐藏应用程序窗口。这些函数分别为ShowWindow、SetWindowLong和GetWindowLong。其中,ShowWindow用于设置指定窗口的显示状态;
SetWindowLong和GetWindowLong分别用于改变和检索指定窗体的一个属性。
你的那个 cooltray...的控件寄给我吧
谢谢
email: [email protected]