📄 balloontip.cpp
字号:
/*
BalloonTip.cpp
Crea un cuadro de mensaje que aparece en cualquier parte de un formulario.
*/
#include "stdafx.h"
#include "BalloonTip.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/*----------------------------------------------------------------------------
Message map
----------------------------------------------------------------------------*/
BEGIN_MESSAGE_MAP(CBalloonTip, CFrameWnd)
//{{AFX_MSG_MAP(CBalloonTip)
ON_WM_TIMER()
ON_WM_LBUTTONDOWN()
ON_WM_RBUTTONDOWN()
ON_WM_CREATE()
ON_WM_PAINT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/*-----------------------------------------------------------------------------
Function : CBalloonTip::CBalloonTip()
Created: Nov 1, 2001
Abstract : Constructor
Parameters :
1. strMessage -> Message to be shown in the balloon
2. lf -> LOGFONT structure from which the the message font
will be created.
3. bBalloonUp -> Is the balloon up or upside down?
Return Value : none
Exceptions : none
Revisions : none
----------------------------------------------------------------------------*/
CBalloonTip::CBalloonTip(CString strMessage, LOGFONT lf, BOOL bBalloonUp)
{
m_strMessage = strMessage;
m_bBalloonUp = bBalloonUp;
VERIFY(m_fontBalloonText.CreateFontIndirect(&lf));
}
/*-----------------------------------------------------------------------------
Function : CBalloonTip::~CBalloonTip()
Created: Nov 1, 2001
Abstract : Destructor
Parameters : none
Return Value : none
Exceptions : none
Revisions : none
----------------------------------------------------------------------------*/
CBalloonTip::~CBalloonTip()
{
}
/*-----------------------------------------------------------------------------
Function : CBalloonTip::Show()
Created: Nov 1, 2001
Abstract : Creates the CBalloonTip on the heap, forces heap creation
Parameters :
1. pt -> Point of the balloon tip
2. size -> Size of the window(width and height)
3. strMessage -> Message to be shown in the balloon
4. nSecs -> Seconds for which the balloon will be shown
5. lf -> LOGFONT structure from which the the message font
will be created.
6. bBalloonUp -> Is balloon up or upside down?
Return Value : void
Exceptions : none
Revisions : none
----------------------------------------------------------------------------*/
CBalloonTip* CBalloonTip::Show(CWnd* wnd, CString strMessage, UINT nSecs, BOOL bBalloonUp)
{
LOGFONT lf;
::ZeroMemory(&lf, sizeof(lf));
lf.lfHeight = 16;
lf.lfWeight = FW_BOLD;
lf.lfUnderline = FALSE;
::strcpy(lf.lfFaceName, _T("Arial"));
// Get the edit box co-ordinates in screen co-ordinates
CRect rect;
wnd->GetWindowRect(&rect);
// Point where the balloon will be show, middle of edit box
CPoint pt = rect.CenterPoint();
return Show(pt, CSize(350, 120), strMessage, lf, nSecs, bBalloonUp);
}
CBalloonTip* CBalloonTip::Show(CPoint pt, CSize size, CString strMessage, LOGFONT lf, UINT nSecs, BOOL bBalloonUp)
{
if (CBalloonTip::nBalloonInstances != 0) // No window creation if already one instance of the class
{
return NULL;
}
CBalloonTip* pBalloonTip = new CBalloonTip(strMessage, lf, bBalloonUp);
CBalloonTip::nBalloonInstances = 1; // Only one instance of window possible.
UINT nRectLeft;
UINT nRectRight;
UINT nRectTop;
UINT nRectBottom;
// Rectangle co-ordinates w.r.t balloon tip point
// Balloon tip at CPoint pt shold remain there without change in window size
if (bBalloonUp)
{
nRectLeft = pt.x -(size.cx * 0.65);
nRectRight = pt.x +(size.cx * 0.35);
nRectTop = pt.y - size.cy;
nRectBottom = pt.y;
}
else
{
nRectLeft = pt.x -(size.cx * 0.35);
nRectRight = pt.x +(size.cx * 0.65);
nRectTop = pt.y;
nRectBottom = pt.y +(size.cy);
}
pBalloonTip->Create(CRect(nRectLeft, nRectTop, nRectRight, nRectBottom));
pBalloonTip->MakeVisisble(nSecs);
return pBalloonTip;
}
/*-----------------------------------------------------------------------------
Function : CBalloonTip::MakeVisible()
Created: Nov 1, 2001
Abstract : Shows the balloon and sets the timer for it's eventual destruction.
Parameters :
1. nSecs -> Seconds for the balloon to be shown
Return Value : none
Exceptions : none
Revisions : none
----------------------------------------------------------------------------*/
void CBalloonTip::MakeVisisble(UINT nSecs)
{
ASSERT(nSecs > 0);
SetTimer(ID_TIMER, (nSecs * 1000), NULL);
CRect rect;
GetWindowRect(&rect);
// Caption bar height to offeset when the balloon is shown
int nCaptionBarSize = ::GetSystemMetrics(SM_CYCAPTION);
int nVerticalBorderSize = ::GetSystemMetrics(SM_CYSIZEFRAME);
if (m_bBalloonUp)
{
// Account for the missing title bar and border thickness in the 3rd parameter
// as the balloon shifts up by the height of title bar and window tickness
SetWindowPos(
&wndTopMost,
m_rectWindow.left,
(m_rectWindow.top + nCaptionBarSize +(2 * nVerticalBorderSize)),
m_rectWindow.right,
m_rectWindow.bottom,
SWP_SHOWWINDOW | SWP_NOACTIVATE
);
}
else
{
// Account for the missing title bar and border thickness in the 3rd parameter
// as the balloon shifts up by the height of window tickness.
SetWindowPos(
&wndTopMost,
m_rectWindow.left,
m_rectWindow.top - nVerticalBorderSize,
m_rectWindow.right,
m_rectWindow.bottom,
SWP_SHOWWINDOW | SWP_NOACTIVATE
);
}
}
/*-----------------------------------------------------------------------------
Function : CBalloonTip::OnTimer()
Created: Nov 1, 2001
Abstract : Gets called by MFC for an WM_TIMER message
Parameters :
1. nIDEvent -> Timer ID
Return Value : void
Exceptions : none
Revisions : none
----------------------------------------------------------------------------*/
void CBalloonTip::OnTimer(UINT nIDEvent)
{
KillTimer(ID_TIMER);
DestroyWindow();
}
/*-----------------------------------------------------------------------------
Function : CBalloonTip::OnLButtonDown()
Created: Nov 1, 2001
Abstract : Gets called by MFC for an WM_LBUTTONDOWN message
Parameters :
See MFC documentation
Return Value : void
Exceptions : none
Revisions : none
----------------------------------------------------------------------------*/
void CBalloonTip::OnLButtonDown(UINT nFlags, CPoint point)
{
KillTimer(ID_TIMER);
DestroyWindow();
}
/*-----------------------------------------------------------------------------
Function : CBalloonTip::OnRButtonDown()
Created: Nov 1, 2001
Abstract : Gets called by MFC for an WM_RBUTTONDOWN message
Parameters :
See MFC documentation
Return Value : void
Exceptions : none
Revisions : none
----------------------------------------------------------------------------*/
void CBalloonTip::OnRButtonDown(UINT nFlags, CPoint point)
{
KillTimer(ID_TIMER);
DestroyWindow();
}
/*-----------------------------------------------------------------------------
Function : CBalloonTip::OnCreate()
Created: Nov 1, 2001
Abstract : Gets called by MFC for an WM_CREATE message
Parameters :
See MFC documentation
Return Value : int
Exceptions : none
Revisions : none
----------------------------------------------------------------------------*/
int CBalloonTip::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -