呵呵,好好看看下面的代码吧! 利用api函数Shell_NotifyIcon ; 他是我的程序中的一部分,分享一下: ********************************************** ... MYTRAY:TNotifyIconData; procedure WMMYTRAYCALLBACK(var MSG:TMESSAGE);message WM_MYTRAYCALLBACK; procedure WMSysCommand(var Msg: TWMSysCommand);message WM_SYSCOMMAND; ... procedure TfrmICBSServerMain.FormCreate(Sender: TObject); begin Application.ShowMainForm:=FALSE; with MYTRAY do begin cbSize:=SIZEOF(TNotifyIconData); Wnd:=Handle; uID:=1; uFlags:=NIF_ICON OR NIF_TIP OR NIF_MESSAGE; uCallbackMessage:=WM_MYTRAYCALLBACK; hIcon:=Application.Icon.Handle; szTip:='Server'; end; Shell_NotifyIcon(NIM_ADD,@MYTRAY); end; ...procedure TfrmICBSServerMain.FormDestroy(Sender: TObject); begin Shell_NotifyIcon(NIM_DELETE,@MYTRAY); end; ... procedure TfrmICBSServerMain.WMMYTRAYCALLBACK(var MSG: TMESSAGE); var p:TPoint; begin case MSG.LParam of WM_RBUTTONDOWN: begin GetCursorPos(p); PopupMenu1.Popup(p.x,p.y); end; WM_LBUTTONDOWN: begin Visible:=not Visible; Application.ShowMainForm:=Visible; SetForegroundWindow(Handle); end; end; end;********************************************* 怎么样?欢呼吧!
Shell_NotifyIcon [Now Supported on Windows NT]Sends a message to the system to add, modify, or delete an icon from the taskbar status area.WINSHELLAPI BOOL WINAPI Shell_NotifyIcon( DWORD dwMessage, // message identifier PNOTIFYICONDATA pnid // pointer to structure ); ParametersdwMessageIdentifier of the message to send. This parameter can be one of these values:NIM_ADD Adds an icon to the status area. NIM_DELETE Deletes an icon from the status area. NIM_MODIFY Modifies an icon in the status area. pnidPointer to a NOTIFYICONDATA structure. The content of the structure depends on the value of dwMessage. Return ValuesIf the function succeeds, the return value is nonzero. If the function fails, the return value is zero.
其中 NOTIFYICONDATA 结构如下: Contains information that the system needs to process taskbar status area messages.typedef struct _NOTIFYICONDATA { // nid DWORD cbSize; HWND hWnd; UINT uID; UINT uFlags; UINT uCallbackMessage; HICON hIcon; char szTip[64]; } NOTIFYICONDATA, *PNOTIFYICONDATA; MemberscbSizeSize of the NOTIFYICONDATA structure.hWndHandle of the window that receives notification messages associated with an icon in the taskbar status area. uIDApplication-defined identifier of the taskbar icon.uFlagsArray of flags that indicate which of the other members contain valid data. This member can be a combination of these values:NIF_ICON The hIcon member is valid. NIF_MESSAGE The uCallbackMessage member is valid. NIF_TIP The szTip member is valid. uCallbackMessageApplication-defined message identifier. The system uses the specified identifier for notification messages that it sends to the window identified by hWnd whenever a mouse event occurs in the bounding rectangle of the icon. hIconHandle of the icon to add, modify, or delete. szTipTooltip text to display for the icon.
利用api函数Shell_NotifyIcon ;
他是我的程序中的一部分,分享一下:
**********************************************
...
MYTRAY:TNotifyIconData;
procedure WMMYTRAYCALLBACK(var MSG:TMESSAGE);message WM_MYTRAYCALLBACK;
procedure WMSysCommand(var Msg: TWMSysCommand);message WM_SYSCOMMAND;
...
procedure TfrmICBSServerMain.FormCreate(Sender: TObject);
begin
Application.ShowMainForm:=FALSE;
with MYTRAY do
begin
cbSize:=SIZEOF(TNotifyIconData);
Wnd:=Handle;
uID:=1;
uFlags:=NIF_ICON OR NIF_TIP OR NIF_MESSAGE;
uCallbackMessage:=WM_MYTRAYCALLBACK;
hIcon:=Application.Icon.Handle;
szTip:='Server';
end;
Shell_NotifyIcon(NIM_ADD,@MYTRAY);
end;
...procedure TfrmICBSServerMain.FormDestroy(Sender: TObject);
begin
Shell_NotifyIcon(NIM_DELETE,@MYTRAY);
end;
...
procedure TfrmICBSServerMain.WMMYTRAYCALLBACK(var MSG: TMESSAGE);
var
p:TPoint;
begin
case MSG.LParam of
WM_RBUTTONDOWN:
begin
GetCursorPos(p);
PopupMenu1.Popup(p.x,p.y);
end;
WM_LBUTTONDOWN:
begin
Visible:=not Visible;
Application.ShowMainForm:=Visible;
SetForegroundWindow(Handle);
end;
end;
end;*********************************************
怎么样?欢呼吧!
[Now Supported on Windows NT]Sends a message to the system to add, modify, or delete an icon from the taskbar status area.WINSHELLAPI BOOL WINAPI Shell_NotifyIcon( DWORD dwMessage, // message identifier
PNOTIFYICONDATA pnid // pointer to structure
);
ParametersdwMessageIdentifier of the message to send. This parameter can be one of these values:NIM_ADD Adds an icon to the status area.
NIM_DELETE Deletes an icon from the status area.
NIM_MODIFY Modifies an icon in the status area.
pnidPointer to a NOTIFYICONDATA structure. The content of the structure depends on the value of
dwMessage. Return ValuesIf the function succeeds, the return value is nonzero.
If the function fails, the return value is zero.
Contains information that the system needs to process taskbar status area messages.typedef struct _NOTIFYICONDATA { // nid
DWORD cbSize;
HWND hWnd;
UINT uID;
UINT uFlags;
UINT uCallbackMessage;
HICON hIcon;
char szTip[64];
} NOTIFYICONDATA, *PNOTIFYICONDATA;
MemberscbSizeSize of the NOTIFYICONDATA structure.hWndHandle of the window that receives notification messages associated with an icon in the taskbar status area. uIDApplication-defined identifier of the taskbar icon.uFlagsArray of flags that indicate which of the other members contain valid data. This member can be a combination of these values:NIF_ICON The hIcon member is valid.
NIF_MESSAGE The uCallbackMessage member is valid.
NIF_TIP The szTip member is valid.
uCallbackMessageApplication-defined message identifier. The system uses the specified identifier for notification messages that it sends to the window identified by hWnd whenever a mouse event occurs in the bounding rectangle of the icon. hIconHandle of the icon to add, modify, or delete. szTipTooltip text to display for the icon.