TrayIcon控件的使用(100分)

  • 主题发起人 主题发起人 xiaobong
  • 开始时间 开始时间
X

xiaobong

Unregistered / Unconfirmed
GUEST, unregistred user!
[?]
TrayIcon控件的使用
如果Animate=true时,会在系统盒里有动画效果。
但是总是有一帧是灰色的,
在产生动画的效果里会有明显的停顿
怎么解决呢
有人说是源码的bug,但是在BCB5里,找不到源码.pas文件(vcl)
该怎么找到源码呢?
给个其它方法也成
 
有空用一下试试。TrayIcon的源码不是用Delphi写的,是用C++写的。
 
好像没有你所说的情况。一直都是好的。是没有源码
 
TrayIcon的源码是用Delphi写的。
我的一个程序里关于tray方面的代码就是从trayicon里面弄出来的
 
喂,搞错了吧,为什么D6及以前的版本里都没有这个控件,CB5却就有了呢?
//---------------------------------------------------------------------------
// Borland C++Builder
// Copyright (c) 1987, 2000-2002 Borland Corporation. All Rights Reserved.
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "trayicon.h"
#include "samp.h"
#include "trayicon.rh"
#pragma package(smart_init)
#pragma resource "*.res"
//---------------------------------------------------------------------------
__fastcall TTrayIcon::TTrayIcon(TComponent* Owner)
: TComponent(Owner)
{
FIcon = new TIcon();
FTimer = new TTimer(NULL);
FIconIndex = 0;
FIcon->Assign(Application->Icon);
FAppRestore = imDoubleClick;
FOnAnimate =do
OnAnimate;
FPopupMenuShow = imNone;
FVisible = false;
FHide = true;
FTimer->Enabled = false;
FTimer->OnTimer = OnAnimate;
FTimer->Interval = 1000;
if (!ComponentState.Contains(csDesigning))
{
memset(&amp;FData, 0, sizeof(TNotifyIconData));
FData.cbSize = sizeof(TNotifyIconData);
FData.hWnd = AllocateHWnd(DoMessage);
FData.uID = (UINT)this;
FData.hIcon = FIcon->Handle;
FData.uFlags = NIF_ICON | NIF_MESSAGE;
FData.uCallbackMessage = WM_SYSTEM_TRAY_NOTIFY;
FApplicationHook = ApplicationHookProc;
Update();
}
}
//---------------------------------------------------------------------------
__fastcall TTrayIcon::~TTrayIcon()
{
if (!ComponentState.Contains(csDesigning))
{
Shell_NotifyIcon(NIM_DELETE, &amp;FData);
DeallocateHWnd(FData.hWnd);
}
if (FIcon)
delete FIcon;
if (FTimer)
delete FTimer;
}
//---------------------------------------------------------------------------
void __fastcall TTrayIcon::Notification(TComponent *AComponent, TOperation Operation)
{
TComponent::Notification(AComponent, Operation);
if (Operation == opRemove)
{
if (AComponent == FIconList)
FIconList = NULL;
else
if (AComponent == FPopupMenu)
FPopupMenu = NULL;
}
}
//---------------------------------------------------------------------------
void __fastcall TTrayIcon::Loaded()
{
TComponent::Loaded();
if (!FIconList)
{
FAnimate = false;
FIcon->Assign(Application->Icon);
}
else
{
FTimer->Enabled = FAnimate;
FIconList->GetIcon(FIconIndex, FIcon);
}
Update();
}
//---------------------------------------------------------------------------
void __fastcall TTrayIcon::SetVisible(bool Value)
{
FVisible = Value;
if (!ComponentState.Contains(csDesigning))
{
if (FVisible)
{
if (!Shell_NotifyIcon(NIM_ADD, &amp;FData))
throw EOutOfResources(LoadStr(sCannotCreate));
Hide = true;
Application->HookMainWindow(FApplicationHook);
}
else
{
if (!Shell_NotifyIcon(NIM_DELETE, &amp;FData))
throw EOutOfResources(LoadStr(sCannotRemove));
Hide = false;
Application->UnhookMainWindow(FApplicationHook);
}
}
}
//---------------------------------------------------------------------------
void __fastcall TTrayIcon::SetHint(String Hint)
{
// The new hint must be different than the previous hint and less than
// 64 characters to be modified. 64 is an operating system limit.
if ((FHint != Hint) &amp;&amp;
(Hint.Length() < 64))
{
FHint = Hint;
StrPLCopy(FData.szTip, Hint, sizeof(FData.szTip) - 1);
// If there is no hint then
there is no tool tip.
if (Hint.Length())
FData.uFlags = FData.uFlags | NIF_TIP;
else
FData.uFlags = FData.uFlags &amp;
!NIF_TIP;
Update();
}
}
//---------------------------------------------------------------------------
void __fastcall TTrayIcon::SetHide(bool Value)
{
FHide = Value;
}
//---------------------------------------------------------------------------
int __fastcall TTrayIcon::GetAnimateInterval()
{
return FTimer->Interval;
}
//---------------------------------------------------------------------------
void __fastcall TTrayIcon::SetAnimateInterval(int Value)
{
FTimer->Interval = Value;
}
//---------------------------------------------------------------------------
bool __fastcall TTrayIcon::GetAnimate()
{
return FAnimate;
}
//---------------------------------------------------------------------------
void __fastcall TTrayIcon::SetAnimate(bool Value)
{
if (FIconList || ComponentState.Contains(csLoading))
FAnimate = Value;
if (FIconList &amp;&amp;
!ComponentState.Contains(csDesigning))
FTimer->Enabled = Value;
}
//---------------------------------------------------------------------------
void __fastcall TTrayIcon::EndSession()
{
Shell_NotifyIcon(NIM_DELETE, &amp;FData);
}
//---------------------------------------------------------------------------
TShiftState TTrayIcon::ShiftState()
{
TShiftState result;
if (GetKeyState(VK_SHIFT) < 0)
result << ssShift;
if (GetKeyState(VK_CONTROL) < 0)
result << ssCtrl;
if (GetKeyState(VK_MENU) < 0)
result << ssAlt;
return result;
}
//---------------------------------------------------------------------------
void __fastcall TTrayIcon::DoMessage(TMessage &amp;Message)
{
TPoint point;
TShiftState shift;
switch(Message.Msg)
{
case WM_QUERYENDSESSION:
Message.Result = 1;
break;
case WM_ENDSESSION:
EndSession();
break;
case WM_SYSTEM_TRAY_NOTIFY:
switch(Message.LParam)
{
case WM_MOUSEMOVE:
if (FOnClick)
{
shift = ShiftState();
GetCursorPos(&amp;point);
do
MouseMove(shift, point.x, point.y);
}
break;
case WM_LBUTTONDOWN:
shift = ShiftState();
shift << ssLeft;
GetCursorPos(&amp;point);
do
MouseDown(mbLeft, shift, point.x, point.y);
FIsClicked = true;
break;
case WM_LBUTTONUP:
shift = ShiftState();
shift << ssLeft;
GetCursorPos(&amp;point);
if (FOnClick)
do
Click();
do
MouseUp(mbLeft, shift, point.x, point.y);
if (FAppRestore == imLeftClickUp)
Restore();
if (FPopupMenuShow == imLeftClickUp)
ShowMenu();
break;
case WM_LBUTTONDBLCLK:
do
DblClick();
if (FAppRestore == imLeftDoubleClick)
Restore();
if (FPopupMenuShow == imLeftDoubleClick)
ShowMenu();
break;
case WM_RBUTTONDOWN:
shift = ShiftState();
shift << ssRight;
GetCursorPos(&amp;point);
do
MouseDown(mbRight, shift, point.x, point.y);
break;
case WM_RBUTTONUP:
shift = ShiftState();
shift << ssRight;
GetCursorPos(&amp;point);
do
MouseUp(mbRight, shift, point.x, point.y);
if (FAppRestore == imRightClickUp)
Restore();
if (FPopupMenuShow == imRightClickUp)
ShowMenu();
break;
case WM_RBUTTONDBLCLK:
do
DblClick();
if (FAppRestore == imRightDoubleClick)
Restore();
if (FPopupMenuShow == imRightDoubleClick)
ShowMenu();
break;
case WM_MBUTTONDOWN:
shift = ShiftState();
shift << ssMiddle;
GetCursorPos(&amp;point);
do
MouseDown(mbMiddle, shift, point.x, point.y);
break;
case WM_MBUTTONUP:
shift = ShiftState();
shift << ssMiddle;
GetCursorPos(&amp;point);
do
MouseUp(mbMiddle, shift, point.x, point.y);
break;
case WM_MBUTTONDBLCLK:
do
DblClick();
break;
}
}
TComponent::Dispatch(&amp;Message);
}
//---------------------------------------------------------------------------
void __fastcall TTrayIcon::ShowMenu()
{
TPoint point;
GetCursorPos(&amp;point);
if (Screen->ActiveForm &amp;&amp;
(Screen->ActiveForm->Handle != NULL))
SetForegroundWindow(Screen->ActiveForm->Handle);
FPopupMenu->Popup(point.x, point.y);
}
//---------------------------------------------------------------------------
void __fastcall TTrayIcon::DoClick()
{
if (FAppRestore == imClick)
Restore();
if (FPopupMenuShow == imClick)
ShowMenu();
if (FOnClick)
FOnClick(this);
}
//---------------------------------------------------------------------------
void __fastcall TTrayIcon::DoDblClick()
{
if (FAppRestore == imDoubleClick)
Restore();
if (FPopupMenuShow == imDoubleClick)
ShowMenu();
if (FOnDblClick)
FOnDblClick(this);
}
//---------------------------------------------------------------------------
void __fastcall TTrayIcon::DoMouseMove(TShiftState Shift, int X, int Y)
{
if (FOnMouseMove)
FOnMouseMove(this, Shift, X, Y);
}
//---------------------------------------------------------------------------
void __fastcall TTrayIcon::DoMouseDown(TMouseButton Button, TShiftState Shift,
int X, int Y)
{
if (FAppRestore == imMouseDown)
Restore();
if (FPopupMenuShow == imMouseDown)
ShowMenu();
if (FOnMouseDown)
FOnMouseDown(this, Button, Shift, X, Y);
}
//---------------------------------------------------------------------------
void __fastcall TTrayIcon::DoMouseUp(TMouseButton Button, TShiftState Shift,
int X, int Y)
{
if (FAppRestore == imMouseDown)
Restore();
if (FPopupMenuShow == imMouseDown)
ShowMenu();
if (FOnMouseUp)
FOnMouseUp(this, Button, Shift, X, Y);
}
//---------------------------------------------------------------------------
void __fastcall TTrayIcon::DoOnAnimate(TObject *Sender)
{
if (IconIndex < FIconList->Count)
FIconIndex++;
else
FIconIndex = 0;
SetIconIndex(FIconIndex);
Update();
}
//---------------------------------------------------------------------------
// When the application minimizes, hide it, so only the icon in the system
// tray is visible.
//---------------------------------------------------------------------------
void __fastcall TTrayIcon::Minimize()
{
Application->Minimize();
ShowWindow(Application->Handle, SW_HIDE);
if (FOnMinimize)
FOnMinimize(this);
}
//---------------------------------------------------------------------------
// Restore the application by making its window visible again, which is a
// little weird since its window is invisible, having no height or width, but
// that's what determines whether the button appears on the taskbar.
//---------------------------------------------------------------------------
void __fastcall TTrayIcon::Restore()
{
Application->Restore();
ShowWindow(Application->Handle, SW_RESTORE);
SetForegroundWindow(Application->Handle);
if (FOnRestore)
FOnRestore(this);
}
//---------------------------------------------------------------------------
void __fastcall TTrayIcon::Update()
{
if (!ComponentState.Contains(csDesigning))
{
FData.hIcon = FIcon->Handle;
if (Visible == true)
Shell_NotifyIcon(NIM_MODIFY, &amp;FData);
}
}
//---------------------------------------------------------------------------
void __fastcall TTrayIcon::SetIconIndex(int Value)
{
FIconIndex = Value;
if (FIconList)
FIconList->GetIcon(FIconIndex, FIcon);
Update();
}
//---------------------------------------------------------------------------
bool __fastcall TTrayIcon::ApplicationHookProc(TMessage &amp;Message)
{
if (Message.Msg == WM_SYSCOMMAND)
{
if (Message.WParam == SC_MINIMIZE)
Minimize();
if (Message.WParam == SC_RESTORE)
Restore();
}
return false;
}
//---------------------------------------------------------------------------
void __fastcall TTrayIcon::SetDefaultIcon()
{
FIcon->Assign(Application->Icon);
Update();
}
//---------------------------------------------------------------------------
HWND __fastcall TTrayIcon::GetHandle()
{
return FData.hWnd;
}
//---------------------------------------------------------------------------

// Register and ValidCtrCheck need to be removed and placed in another package.

//---------------------------------------------------------------------------
// ValidCtrCheck is used to assure that the components createddo
not have
// any pure virtual functions.
//
static inline void ValidCtrCheck(TTrayIcon *)
{
new TTrayIcon(NULL);
}
//---------------------------------------------------------------------------
 
这个控件不是自带的,要自己装的
 
cb有,但是delphi没有.
 
后退
顶部