急!在线等!创建托盘图标。立即结贴!

Melody2002 2003-08-03 04:01:35
看过C++Builder的托盘图标的代码,但是不会用DELPHI翻译,一运行就出异常。
希望大家能给出代码,简单一点。谢谢。
...全文
49 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
huojiehai 2003-08-03
  • 打赏
  • 举报
回复
unit MainFrm;

interface

{ 记住在uses部分中包括 ShellAPI }
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ShellAPI, ExtCtrls;

{ 自定义消息,当小图标捕捉到鼠标事件时 Windows 向回调函数发送此消息 }
const MY_MESSAGE = WM_USER + 100;

type
TMainForm = class(TForm)
Timer1: TTimer;
procedure OnIconNotify(var Message: TMessage);
message MY_MESSAGE;

procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormPaint(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
MainForm: TMainForm;

implementation

{$R *.dfm}

{ 当小图标捕捉到鼠标事件时进入此过程 }
procedure TMainForm.OnIconNotify(var Message: TMessage);
var
Busy : Boolean;
begin
Busy := False;
if not Busy then
begin
Busy := True;
if Message.LParam=WM_LBUTTONDOWN then
if Application.MessageBox('真的要退出日志定时清楚程序吗?', '提示', MB_YESNO)=IDYES then
Close;
Busy := False;
end;
end;

{ 当主Form建立时通知windows加入小图标 }
procedure TMainForm.FormCreate(Sender: TObject);
var
nid: TNotifyIconData;
begin
nid.cbSize := sizeof(nid); // nid 变量的字节数
nid.Wnd := Handle; // 主窗口句柄
// nid.uID := -1; // 内部标识,可设为任意数
nid.hIcon := Application.Icon.Handle; // 要加入的图标句柄,可任意指定
nid.szTip := '定时清楚日志'; //提示字符串
nid.uCallbackMessage := MY_MESSAGE; //回调函数消息

nid.uFlags := NIF_ICON or NIF_TIP or NIF_MESSAGE; // 指明哪些字段有效
if not Shell_NotifyIcon(NIM_ADD,@nid) then
begin
ShowMessage('Failed!');
Application.Terminate;
end;
{ 将程序的窗口样式设为TOOL窗口,可避免在任务条上出现 }
SetWindowLong(Application.Handle, GWL_EXSTYLE, WS_EX_TOOLWINDOW);
end;

{ 程序被关闭时通知windows去掉小图标 }
procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
var
nid: TNotifyIconData;
begin
nid.cbSize := sizeof(nid); // nid变量的字节数
// nid.uID := -1; // 内部标识,与加入小图标时的数一致
nid.Wnd := Handle; //主窗口句柄
Shell_NotifyIcon(NIM_DELETE, @nid); //去掉小图标
end;

{ 主窗口初始化完毕并显示时将激活 Paint 重画事件, 此时将主窗口隐藏 }
procedure TMainForm.FormPaint(Sender: TObject);
begin
Hide;
end;

end.

huojiehai 2003-08-03
  • 打赏
  • 举报
回复
//制作托盘
unit tp;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, jpeg, ExtCtrls,ShellApi, Menus, StdCtrls;
const
ICON_ID = 1;
My_ICONEVENT = WM_USER + 1;
type
TMyTest = class(TForm)
Image1: TImage;
pop1: TPopupMenu;
showform: TMenuItem;
N1: TMenuItem;
Memo1: TMemo;
Button1: TButton;
Timer1: TTimer;
procedure FormCreate(Sender: TObject);
procedure N1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure showformClick(Sender: TObject);
private
procedure InitIcon;
procedure UnInstallIcon;
procedure IConOnclick(var Myss:TMessage);message My_ICONEVENT;
procedure WMSysCommand(var Sysss: TWMSysCommand);message WM_SYSCOMMAND;
public
end;
var
MyTest: TMyTest;
implementation
{$R *.dfm}
procedure TMyTest.WMSysCommand(var Sysss: TWMSysCommand); //拦截系统消息
begin
with Sysss do
begin
if (CmdType and $FFF0 = SC_MINIMIZE) or (CmdType and $FFF0 = SC_CLOSE) then
begin
ShowWindow(MyTest.Handle,SW_HIDE);
Exit;
end;
inherited;
end;
end;
procedure TMyTest.ICononclick(var Myss:TMessage);
var p: TPoint;
begin
if (Myss.lParam = WM_RBUTTONDOWN) or (Myss.lParam = WM_LBUTTONDBLCLK) then
begin
GetCursorPos(p);
pop1.Popup(p.x,p.y);
end;
end;
procedure TMyTest.InitIcon;
Var MyNotify:TNotifyIconData;
begin
MyNotify.cbSize:=Sizeof(MyNotify);
MyNotify.Wnd:=handle;
MyNotify.uID := ICON_ID;
MyNotify.uFlags:=NIF_ICON or NIF_MESSAGE or NIF_TIP;
MyNotify.uCallbackMessage:=My_ICONEVENT;
MyNotify.hIcon :=Image1.Picture.Icon.Handle;
MyNotify.szTip :='MyTest';
Shell_NotifyIcon(NIM_ADD,@MyNotify);
end;
procedure TMyTest.FormCreate(Sender: TObject);
begin
InitIcon;
SetWindowLong(Application.Handle, GWL_EXSTYLE, WS_EX_TOOLWINDOW);
//设置应用程序状态栏目不显示
end;
procedure TMyTest.N1Click(Sender: TObject);
begin
close;
end;
procedure TMyTest.UnInstallIcon; //卸载图标
var IconData: TNotifyIconData;
begin
IconData.cbSize := SizeOf(IconData);
IconData.Wnd := Handle;
IconData.uID := ICON_ID;
Shell_NotifyIcon(NIM_DELETE,@IconData);
end;
procedure TMyTest.FormDestroy(Sender: TObject);
begin
UnInstallIcon;
end;
procedure TMyTest.Timer1Timer(Sender: TObject);
begin
Timer1.Enabled:=False;
ShowWindow(MyTest.Handle,SW_HIDE);
end;
procedure TMyTest.showformClick(Sender: TObject);
begin
ShowWindow(MyTest.Handle,SW_SHOWNORMAL);
SetForegroundWindow(application.handle);//激活应用程序为前台,此处可以省掉
end;
end.

1,184

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧