long PASCAL WndProc(HWND hwnd, UINT msg, WPARAM wParam, LONG lParam );
int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev_inst, LPSTR cmd_line, int cmd_show )
{ HWND hwnd;
MSG msg;
WNDCLASS wnd_class;
// Register the Window Class and Procedure
if (AOwner <> nil) and (AOwner is TForm) then
FParentWindow := TForm(AOwner).Handle
else
FParentWindow := 0;
FWindowHandle := AllocateHWnd(WndProc);
FillDataStructure;
end;
destructor TSysTray.Destroy;
begin
try
if (not (csDesigning in ComponentState) and FActive)
or ((csDesigning in ComponentState) and FShowDesigning) then
DeleteIcon;
finally
FIcon.Free;
DeallocateHWnd(FWindowHandle);
end;
inherited Destroy;
end;
function TSysTray.AddIcon: boolean;
begin
FillDataStructure;
Result := Shell_NotifyIcon(NIM_ADD, @FIconData);
end;
function TSysTray.DeleteIcon: boolean;
begin
Result := Shell_NotifyIcon(NIM_DELETE, @FIconData);
end;
function TSysTray.ModifyIcon: boolean;
begin
FillDataStructure;
if FActive then
Result := Shell_NotifyIcon(NIM_MODIFY, @FIconData)
else
Result := true;
end;
procedure TSysTray.SetIcon(Icon: TIcon);
begin
FIcon.Assign(Icon);
ModifyIcon;
end;
procedure TSysTray.SetHint(Hint: string);
begin
if Length(Hint) >= 64 then Hint := Copy(Hint, 1, 63);
FHint := Hint;
ModifyIcon;
end;
procedure TSysTray.SetActive(Value: boolean);
begin
if Value <> FActive then
begin
FActive := Value;
if not (csDesigning in ComponentState) then
begin
case Value of
true: AddIcon;
false: DeleteIcon;
end;
end;
end;
end;
procedure TSysTray.SetShowDesigning(Value: boolean);
begin
if (csDesigning in ComponentState) then
begin
if Value <> FShowDesigning then
begin
FShowDesigning := Value;
case Value of
true: AddIcon;
false: DeleteIcon;
end;
end;
end;
end;
procedure TSysTray.FillDataStructure;
begin
With FIconData do
begin
uCallbackMessage:=WM_SYSTRAY;
cbSize := SizeOf(FIconData);
uID := IDI_TRAYICON;
wnd := FWindowHandle;
hIcon := FIcon.Handle;
StrCopy(FIconData.szTip, PChar(FHint));
uFlags := NIF_ICON + NIF_TIP + NIF_MESSAGE;
end;
end;
procedure TSysTray.WndProc(var Msg: TMessage);
var
P: TPoint;
begin
if (Msg.WParam <> IDI_TRAYICON) then exit;
if Assigned(FPopupMenu) then
FPopupMenu.Alignment := FPopupAlign;
GetCursorPos(p);
case Msg.LParam of
WM_LBUTTONDOWN:
begin
if (pmLeftClick in FPopupMode) and Assigned(FPopupMenu) then
begin
SetForegroundWindow(FParentWindow);
FPopupMenu.Popup(p.x,p.y);
end;
if Assigned(FOnIconMouseDown) then
begin
SetForegroundWindow(FParentWindow);
FOnIconMouseDown(Self, mbLeft, p.x, p.y);
end;
end;
WM_RBUTTONDOWN:
begin
if (pmRightClick in FPopupMode) and Assigned(FPopupMenu) then
begin
SetForegroundWindow(FParentWindow);
FPopupMenu.Popup(p.x,p.y);
end;
if Assigned(FOnIconMouseDown) then
begin
SetForegroundWindow(FParentWindow);
FOnIconMouseDown(Self, mbRight, p.x, p.y);
end;
end;
WM_LBUTTONDBLCLK:
begin
if (pmLeftDblClick in FPopupMode) and Assigned(FPopupMenu) then
begin
SetForegroundWindow(FParentWindow);
FPopupMenu.Popup(p.x,p.y);
end;
if Assigned(FOnIconDoubleClick) then
begin
SetForegroundWindow(FParentWindow);
FOnIconDoubleClick(Self, mbLeft, p.x, p.y);
end;
end;
WM_RBUTTONDBLCLk:
begin
if (pmRightDblClick in FPopupMode) and Assigned(FPopupMenu) then
begin
SetForegroundWindow(FParentWindow);
FPopupMenu.Popup(p.x,p.y);
end;
if Assigned(FOnIconDoubleClick) then
begin
SetForegroundWindow(FParentWindow);
FOnIconDoubleClick(Self, mbRight, p.x, p.y);
end;
end;
if (AOwner <> nil) and (AOwner is TForm) then
FParentWindow := TForm(AOwner).Handle
else
FParentWindow := 0;
FWindowHandle := AllocateHWnd(WndProc);
FillDataStructure;
end;
destructor TSysTray.Destroy;
begin
try
if (not (csDesigning in ComponentState) and FActive)
or ((csDesigning in ComponentState) and FShowDesigning) then
DeleteIcon;
finally
FIcon.Free;
DeallocateHWnd(FWindowHandle);
end;
inherited Destroy;
end;
function TSysTray.AddIcon: boolean;
begin
FillDataStructure;
Result := Shell_NotifyIcon(NIM_ADD, @FIconData);
end;
function TSysTray.DeleteIcon: boolean;
begin
Result := Shell_NotifyIcon(NIM_DELETE, @FIconData);
end;
function TSysTray.ModifyIcon: boolean;
begin
FillDataStructure;
if FActive then
Result := Shell_NotifyIcon(NIM_MODIFY, @FIconData)
else
Result := true;
end;
procedure TSysTray.SetIcon(Icon: TIcon);
begin
FIcon.Assign(Icon);
ModifyIcon;
end;
procedure TSysTray.SetHint(Hint: string);
begin
if Length(Hint) >= 64 then Hint := Copy(Hint, 1, 63);
FHint := Hint;
ModifyIcon;
end;
procedure TSysTray.SetActive(Value: boolean);
begin
if Value <> FActive then
begin
FActive := Value;
if not (csDesigning in ComponentState) then
begin
case Value of
true: AddIcon;
false: DeleteIcon;
end;
end;
end;
end;
procedure TSysTray.SetShowDesigning(Value: boolean);
begin
if (csDesigning in ComponentState) then
begin
if Value <> FShowDesigning then
begin
FShowDesigning := Value;
case Value of
true: AddIcon;
false: DeleteIcon;
end;
end;
end;
end;
procedure TSysTray.FillDataStructure;
begin
With FIconData do
begin
uCallbackMessage:=WM_SYSTRAY;
cbSize := SizeOf(FIconData);
uID := IDI_TRAYICON;
wnd := FWindowHandle;
hIcon := FIcon.Handle;
StrCopy(FIconData.szTip, PChar(FHint));
uFlags := NIF_ICON + NIF_TIP + NIF_MESSAGE;
end;
end;
procedure TSysTray.WndProc(var Msg: TMessage);
var
P: TPoint;
begin
if (Msg.WParam <> IDI_TRAYICON) then exit;
if Assigned(FPopupMenu) then
FPopupMenu.Alignment := FPopupAlign;
GetCursorPos(p);
case Msg.LParam of
WM_LBUTTONDOWN:
begin
if (pmLeftClick in FPopupMode) and Assigned(FPopupMenu) then
begin
SetForegroundWindow(FParentWindow);
FPopupMenu.Popup(p.x,p.y);
end;
if Assigned(FOnIconMouseDown) then
begin
SetForegroundWindow(FParentWindow);
FOnIconMouseDown(Self, mbLeft, p.x, p.y);
end;
end;
WM_RBUTTONDOWN:
begin
if (pmRightClick in FPopupMode) and Assigned(FPopupMenu) then
begin
SetForegroundWindow(FParentWindow);
FPopupMenu.Popup(p.x,p.y);
end;
if Assigned(FOnIconMouseDown) then
begin
SetForegroundWindow(FParentWindow);
FOnIconMouseDown(Self, mbRight, p.x, p.y);
end;
end;
WM_LBUTTONDBLCLK:
begin
if (pmLeftDblClick in FPopupMode) and Assigned(FPopupMenu) then
begin
SetForegroundWindow(FParentWindow);
FPopupMenu.Popup(p.x,p.y);
end;
if Assigned(FOnIconDoubleClick) then
begin
SetForegroundWindow(FParentWindow);
FOnIconDoubleClick(Self, mbLeft, p.x, p.y);
end;
end;
WM_RBUTTONDBLCLk:
begin
if (pmRightDblClick in FPopupMode) and Assigned(FPopupMenu) then
begin
SetForegroundWindow(FParentWindow);
FPopupMenu.Popup(p.x,p.y);
end;
if Assigned(FOnIconDoubleClick) then
begin
SetForegroundWindow(FParentWindow);
FOnIconDoubleClick(Self, mbRight, p.x, p.y);
end;
end;
procedure TForm1.WMMyTrayIconCallBack(var Msg: TMessage);
//处理点击托盘图标的事件,根据 WM_MOUSEMOVE 消息的不同情况产生不同的回应
var
CursorPos : TPoint;
begin
TrayBarPopMenu.AutoPopup:=False;
case Msg.lParam of
//左键按下
WM_LBUTTONDOWN : begin
application.MainForm.BringToFront; //窗体置前
end;
//左键双击
WM_LBUTTONDBLCLK : begin //窗体隐含或显示
Application.MainForm.Visible := not Application.MainForm.Visible;
SetForegroundWindow(Application.Handle);
end;
//右键按下
WM_RBUTTONDOWN : begin //显示弹出菜单
GetCursorPos(CursorPos);
TrayBarPopMenu.Popup(CursorPos.x,CursorPos.y);
end;
end//case
end;
procedure TForm1.n_MainFromDispOrHideClick(Sender: TObject);
begin
if Application.MainForm.Visible then
begin
if FormStep1.Showing Then FormStep1.Hide;
if FormStep2.Showing Then FormStep2.Hide;
Application.MainForm.Hide;
n_MainFromDispOrHide.Caption :='显示主窗体';
end
else
begin
Application.MainForm.Show;
n_MainFromDispOrHide.Caption :='隐藏主窗体';
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
SetWindowLong(Application.Handle,GWL_EXSTYLE,WS_EX_TOOLWINDOW);//程序不在任务栏上显示
TrayShow(Application.MainForm);//显示图标
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Shell_NotifyIcon(NIM_DELETE, @MyTrayIcon);//删除托盘图标
end;