Delphi的托盘编程   。现在很多程序都用这个,比如傲游,迅雷等,主要代码如下:

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, ShellAPI, AppEvnts, StdCtrls, Menus;

const WM_NID = WM_User + 1000; //声明一个常量

private

{ Private declarations } // 定义两个函数

procedure SysCommand(var SysMsg: TMessage); message WM_SYSCOMMAND;

procedure WMNID(var msg:TMessage); message WM_NID;

public

end;

var

Form1: TForm1;

NotifyIcon: TNotifyIconData; // 全局变量

implementation

{$R *.dfm}

procedure TForm1.WMNID(var msg:TMessage);

var

mousepos: TPoint;

begin

GetCursorPos(mousepos); //获取鼠标位置

case msg.LParam of

WM_LBUTTONUP: // 在托盘区点击左键后

begin

Form1.Visible := not Form1.Visible; // 显示主窗体与否

Shell_NotifyIcon(NIM_DELETE, @NotifyIcon); // 显示主窗体后删除托盘区的图标

SetWindowPos(Application.Handle, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW); // 在任务栏显示程序

end;

WM_RBUTTONUP: PopupMenu1.Popup(mousepos.X, mousepos.Y); // 弹出菜单

end;

end;

procedure TForm1.FormDestroy(Sender: TObject);

begin

Shell_NotifyIcon(NIM_DELETE, @NotifyIcon); // 删除托盘图标

end;

procedure TForm1.SysCommand(var SysMsg: TMessage);

begin

case SysMsg.WParam of

SC_MINIMIZE: // 当最小化时

begin

SetWindowPos(Application.Handle, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_HIDEWINDOW);

Hide; // 在任务栏隐藏程序

// 在托盘区显示图标

with NotifyIcon do

begin

cbSize := SizeOf(TNotifyIconData);

Wnd := Handle;

uID := 1;

uFlags := NIF_ICON or NIF_MESSAGE or NIF_TIP;

uCallBackMessage := WM_NID;

hIcon := Application.Icon.Handle;

szTip := '托盘程序';

end;

Shell_NotifyIcon(NIM_ADD, @NotifyIcon); // 在托盘区显示图标

end;

else

inherited;

end;

end;

{以下三个函数为托盘右键菜单,可自行添加功能}

procedure TForm1.N1Click(Sender: TObject);

begin

ShowMessage('Delphi托盘小程序!');

end;

procedure TForm1.N2Click(Sender: TObject);

begin

Form1.Visible := true; // 显示窗体

SetWindowPos(Application.Handle, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW);

Shell_NotifyIcon(NIM_DELETE, @NotifyIcon); // 删除托盘图标

end;

procedure TForm1.N3Click(Sender: TObject);

begin

Shell_NotifyIcon(NIM_DELETE, @NotifyIcon);

Application.Terminate;

end;

end.

delphi 托盘程序 转的更多相关文章

  1. Delphi 托盘程序实现 转

    unit MainUnit; interface uses  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, F ...

  2. Delphi自定义消息应用及delphi托盘实现

    Delphi自定义消息应用及delphi托盘实现interfaceuses   Windows, Messages, SysUtils, Variants, Classes, Graphics, Co ...

  3. Delphi托盘类 收集

    收集的两个托盘程序: 1. 托盘区就是在windows的状态栏下方显示时钟.输入法状态的地方, 要把你的程序显示在托盘区: 下面是一个托盘类,只要把下面粘贴到文本文件中,改成TrayIcon.pas, ...

  4. 第一个Delphi小程序

    第一次应工作需呀,接触这个语言,今晚在自己的电脑搭建好环境,写的第一个超简单的Delphi小程序! var temp:Integer; //求个位数 procedure TForm1.BitBtn1C ...

  5. Delphi:程序自己删除自己,适用于任何windows版本(含源码)

    Delphi:程序自己删除自己,适用于任何windows版本(含源码) function Suicide: Boolean; var   sei: TSHELLEXECUTEINFO;   szMod ...

  6. Delphi应用程序的调试(五)其他调试工具

    Delphi应用程序的调试(五)其他调试工具 Delphi7中提供了一些附加调试工具来帮助用户检查程序错误.从性能上讲,其中一些工具属于高级调试工具.尽管高级调试工具不像其他工具那样常用,但对于经验丰 ...

  7. Delphi应用程序的调试(二)使用断点

    Delphi应用程序的调试(二)使用断点 使用断点(Using Breakpoints) 当用户从Delphi IDE 运行程序时,程序全速运行,只会在设置了断点的地方停住. New Term 断点( ...

  8. Qt托盘程序

    使用QSystemTrayIcon类可以实现托盘程序.在这里使用QMainWindow做实例: mainwindow.h头文件 #ifndef MAINWINDOW_H #define MAINWIN ...

  9. WinForm设置任务栏托盘程序

    程序设计界面如下图所示: 1.在程序初始化加载的时候设置程序图标,具体code如下: private void Form1_Load(object sender, EventArgs e) { //t ...

随机推荐

  1. htaccess URL重写rewrite与重定向redirect(转)

    1. 将 .htm 页面映射到 .php 1 Options +FollowSymlinks 2 RewriteEngine on 3 RewriteRule ^(.*)\.htm$ $1.php [ ...

  2. Quartz contention when running in load balanced environment--reference

    1.8.3 appears to have addressed this issue with a single application server. However, we're seeing t ...

  3. Where Jboss7.1 take war application to deploy--reference

    Question i've deployed the jboss-as-helloworld-errai application in my standalone jboss7.1 instance, ...

  4. JavaScript—window对象使用

    window对象是JavaScript浏览器对象模型中的顶层对象,包含多个常用方法和属性: 1. 打开新窗口 window.open(pageURL,name,parameters) 其中:pageU ...

  5. MVC3中 ViewBag、ViewData和TempData的使用和区别(不是自己写的)

    (网上抄的,并未消化)在MVC3开始,视图数据可以通过ViewBag属性访问,在MVC2中则是使用ViewData.MVC3中保留了ViewData的使用.ViewBag 是动态类型(dynamic) ...

  6. 顶部图片放大回弹效果Scrollview ---- 各应用中常见的自定义View 解析

    原理并不难.  代码量也不大.  非常简洁 .  先来个效果图 再上一波代码. public class SpecialScrollView extends ScrollView implements ...

  7. iOS中常用的四种数据持久化技术

    iOS中的数据持久化方式,基本上有以下四种:属性列表 对象归档 SQLite3和Core Data 1.属性列表涉及到的主要类:NSUserDefaults,一般 [NSUserDefaults st ...

  8. Asp.net IsPostBack

    Page.IsPostBack是一个标志:当前请求是否第一次打开.调用方法为:Page.IsPostBack或者IsPostBack或者this.IsPostBack或者this.Page.IsPos ...

  9. JS屏蔽右键菜单,复制,粘帖xxxxx........

    //屏蔽右键菜单 document.oncontextmenu = function (event) { if (window.event) { event = window.event; } try ...

  10. C++多重继承虚表的内存分布

    接前面虚表的内存分布,今天重点看多重继承的虚表内存分布,简单的说,继承几个类便有几个虚表,如下代码 class Drive : public Base1, public Base2, public B ...