用Application.ShowMainForm := False 完全可以,你可能是少加了什么东西。
unit TrayIcon;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Menus,shellapi;
type
//----------------------------------------------------------------------
PNotifyIconData = ^TNotifyIconDataA;
TNotifyIconDataA = record
cbSize : DWORD;
Wnd : HWND;
uID : UINT;
uFlags : UINT;
uCallbackMessage : UINT;
hIcon : HICON;
szTip : array [0..63] of AnsiChar;
end;
//----------------------------------------------------------------------
TForm1 = class(TForm)
PopupMenu1: TPopupMenu;
open1: TMenuItem;
close1: TMenuItem;
N1: TMenuItem;
about1: TMenuItem;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure open1Click(Sender: TObject);
procedure close1Click(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
//-------------------------------------------------------------
IconData: TNotifyIconData;
procedure ShowIcon;
procedure IconOnClick(var message:TMessage); message WM_USER+1;
Procedure WMSysCommand(Var message : TMessage) ; Message WM_SYSCOMMAND ;
//-------------------------------------------------------------
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.IconOnClick( var message: Tmessage);
var p : TPoint;
begin
if (message.lParam = WM_LBUTTONDOWN) then
begin
ShowWindow(Handle, SW_SHOW );
end;
if (message.lParam = WM_RBUTTONDOWN) then
begin
GetCursorPos(p);
popupmenu1.Popup( p.x ,p.y );
end;
end;
Procedure TForm1.WMSysCommand(Var Message : TMessage) ;
begin
if (Message.WParam = SC_MINIMIZE) then
begin
ShowIcon;
end
else
Inherited;
end;
procedure TForm1.ShowIcon;
begin
IconData.cbSize := SizeOf( IconData );
IconData.Wnd := Handle;
IconData.uID := 1;
IconData.uFlags := NIF_ICON
or NIF_MESSAGE or NIF_TIP;
IconData.uCallBackMessage := WM_USER+1;
IconData.hIcon := application.Icon.Handle;
IconData.szTip := 'LANChat';
Shell_NotifyIcon( NIM_ADD, @IconData );
ShowWindow(Handle, SW_HIDE);
hide;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Shell_NotifyIcon( NIM_DELETE, @IconData );
end;
procedure TForm1.open1Click(Sender: TObject);
begin
Form1.Show;
end;
procedure TForm1.close1Click(Sender: TObject);
begin
Form1.close;
end;
procedure TForm1.FormShow(Sender: TObject);
begin
showwindow(application.handle,sw_hide);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
showicon;
end;
end.
============================
dpr文件:
program Project1;
uses
Forms,
TrayIcon in 'TrayIcon.pas' {Form1};
{$R *.RES}
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.ShowMainForm := false;
Application.Run;
end.