当前位置:首页>开发>正文

delphi怎样使程序最小化时变成右下角的一个小图标

2023-04-27 13:27:16 互联网 未知 开发

delphi怎样使程序最小化时变成右下角的一个小图标?

delphi怎样使程序最小化时变成右下角的一个小图标

Shell_NotifyIcon这个API可以实现系统托盘图标功能,然后你在自定义一下最小化的消息就可以了,图标功能有个例子

托盘就是在任务栏右下角的小图标。创建、更改、删除只需要一个Windows API就能实现。这个函数叫做Shell_NotifyIcon()。这个函数和其他处理Windows 外壳的函数都包含在ShellAPI单元中。本例程在窗口创建时在任务栏的右下角创建一个托盘图标。图标由两个ICON交替出现。当窗口最小化时任务栏中只有托盘图标,当左键单击托盘图标窗口恢复。图标停止交替。

unit Unit1

interface

uses

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

Menus, Dialogs,

ExtCtrls, ImgList

const

MI_ICONEVENT = WM_USER 1

ICON_ID = 10

type

TForm1 = class(TForm)

Timer1: TTimer

PopupMenu2: TPopupMenu

N1: TMenuItem

ImageList1: TImageList

procedure N1Click(Sender: TObject)

procedure FormCreate(Sender: TObject)

procedure FormDestroy(Sender: TObject)

procedure FormClose(Sender: TObject var Action: TCloseAction)

procedure Timer1Timer(Sender: TObject)

private

procedure modifyicon

procedure IconOnClick(var message: TMessage) MESSAGE MI_ICONEVENT

procedure tranmessage(var m:TWMSYSCOMMAND) message WM_SYSCOMMAND

public

{ Public declarations }

end

var

Form1: TForm1

ydic:array [0..1] of ticon

icint:integer

xsyc:BOOLEAN

implementation

{$R *.DFM}

procedure TForm1.N1Click(Sender: TObject)

begin

APPLICATION.Terminate

end

procedure TForm1.FormCreate(Sender: TObject)

var

IconData: TNotifyIconData

begin

xsyc:=true

ydic[0]:=ticon.Create

ydic[1]:=ticon.Create

ydic[0].Handle :=loadicon(hinstance,icon0)//资源文件(ICONS.RES)

ydic[1].Handle :=loadicon(hinstance,icon1)

IconData.cbSize:=SizeOf(IconData )

IconData.Wnd:= form1.Handle

IconData.uID:= ICON_ID

IconData.uFlags := NIF_ICON or NIF_MESSAGE or NIF_TIP

IconData.uCallBackMessage := MI_ICONEVENT

IconData.hIcon :=ydic[0].Handle

IconData.szTip := 我笑、我笑、笑笑笑

icint:=0

Shell_NotifyIcon( NIM_ADD, @IconData )

ShowWindow(Application.Handle, SW_HIDE )

end

procedure TForm1.FormDestroy(Sender: TObject)

var

IconData: TNotifyIconData

begin //卸载图标

IconData.cbSize := SizeOf( IconData )

IconData.Wnd :=form1.Handle

IconData.uID := ICON_ID

Shell_NotifyIcon( NIM_DELETE, @IconData )

end

procedure TForm1.FormClose(Sender: TObject var Action: TCloseAction)

var

IconData: TNotifyIconData

begin //卸载图标

IconData.cbSize := SizeOf( IconData )

IconData.Wnd :=form1.Handle

IconData.uID := ICON_ID

Shell_NotifyIcon( NIM_DELETE, @IconData )

end

procedure TForm1.Timer1Timer(Sender: TObject)

begin

modifyicon

end

procedure TForm1.modifyicon

var

IconData: TNotifyIconData

begin

IconData.cbSize := SizeOf( IconData )

IconData.Wnd :=form1.Handle

IconData.uID := ICON_ID

icint:=(icint 1) mod 2

IconData.uFlags := NIF_ICON or NIF_MESSAGE or NIF_TIP

IconData.uCallBackMessage := MI_ICONEVENT

icondata.hIcon:= ydic[icint].Handle

IconData.szTip := 我笑、我笑、笑笑笑

shell_notifyicon(nim_modify,@icondata)

end

procedure TForm1.IconOnClick(var message: TMessage)

var

a:tpoint

begin //单击事件

if (message.lParam = WM_RBUTTONDOWN) then

begin

getcursorpos(a)

PopupMenu2.Popup(a.x,a.y)

end

else if (message.lParam = WM_LBUTTONDOWN) then

begin

timer1.Enabled:=FALSE

if xsyc then

sendmessage(form1.Handle,wm_syscommand,sc_minimize,0)

else

begin

ShowWindow(Application.Handle,SW_RESTORE)//SW_maximize

setforegroundwindow(form1.handle)

xsyc:=true

end

end

end

procedure TForm1.tranmessage(var m: TWMSYSCOMMAND)

begin

if m.CmdType=SC_MINIMIZE then

begin

timer1.Enabled :=true

sendmessage(Application.Handle,WM_SYSCOMMAND,SC_MINimize,0)

ShowWindow(Application.Handle, SW_HIDE )

xsyc:=false

end

else

inherited

end

end.

最小化消息重载可以这样

procedure pgame(var msg:twmsyscommand)message wm_syscommand

procedure Tmainfrm.pgame(var msg: twmsyscommand) //窗口最小化

begin

if msg.CmdType=SC_MINIMIZE then

begin

//处理代码

end

inherited

end