📄 balloontip.cpp
字号:
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
{
return -1;
}
// Remove caption and thick frame for proper shading
ModifyStyle(WS_CAPTION | WS_THICKFRAME, 0);
CRect t_Rect;
GetClientRect(&t_Rect); // Get the client rectangle to set the balloon region
if (m_bBalloonUp)
{
// Calculation of Text window, where to write text in the balloon
m_rectText.left = t_Rect.Width() * 0.10;
m_rectText.right = t_Rect.Width() * 0.90;
m_rectText.top = t_Rect.Height() * 0.10;
m_rectText.bottom = t_Rect.Height()* 0.70;
// Create an elliptical region out of the client rectangle
m_rgnRoundRect.CreateRoundRectRgn(
t_Rect.left,
t_Rect.top,
t_Rect.right,
t_Rect.bottom -(t_Rect.Height() * 0.25),
abs(t_Rect.left - t_Rect.right) * 0.18,
t_Rect.Height() * 0.25
);
// Start the process of creating the balloon tip
CPoint ptTri[4];
ptTri[0].x =(t_Rect.left) +(t_Rect.Width() * 0.93);
ptTri[0].y =(t_Rect.bottom) -(t_Rect.Height() * 0.40);
ptTri[1].x =(t_Rect.left) +(t_Rect.Width() * 0.65);
ptTri[1].y = t_Rect.bottom;
ptTri[2].x =(t_Rect.left) +(t_Rect.Width() * 0.70);
ptTri[2].y =(t_Rect.bottom) -(t_Rect.Height() * 0.40);
ptTri[3].x =(t_Rect.left) +(t_Rect.Width() * 0.93);
ptTri[3].y =(t_Rect.bottom) -(t_Rect.Height() * 0.40);
m_rgnTip.CreatePolygonRgn(ptTri, 3, ALTERNATE);
}
else
{
// Calculation of Text window, where to write text in the balloon
m_rectText.left = t_Rect.Width() * 0.10;
m_rectText.right = t_Rect.Width() * 0.90;
m_rectText.top = t_Rect.Height() * 0.30;
m_rectText.bottom = t_Rect.Height() * 0.90;
// Create an elliptical region out of the client rectangle
m_rgnRoundRect.CreateRoundRectRgn(
t_Rect.left,
t_Rect.bottom -(t_Rect.Height() * 0.75),
t_Rect.right,
t_Rect.bottom,
abs(t_Rect.left - t_Rect.right) * 0.18,
t_Rect.Height() * 0.25
);
// Start the process of creating the balloon tip
CPoint ptTri[4];
ptTri[0].x =(t_Rect.left) +(t_Rect.Width() * 0.07);
ptTri[0].y =(t_Rect.Height() * 0.40);
ptTri[1].x =(t_Rect.left) +(t_Rect.Width() * 0.35);
ptTri[1].y = t_Rect.bottom - t_Rect.Height();
ptTri[2].x =(t_Rect.left) +(t_Rect.Width() * 0.30);
ptTri[2].y = t_Rect.Height() * 0.40;
ptTri[3].x =(t_Rect.left) +(t_Rect.Width() * 0.07);
ptTri[3].y = t_Rect.Height() * 0.40;
m_rgnTip.CreatePolygonRgn(ptTri, 3, ALTERNATE);
}
// Create the combined region with ellipse and tip
CRgn rgnComb;
rgnComb.CreateRectRgn(t_Rect.left, t_Rect.top, t_Rect.right, t_Rect.bottom);
int iRetComb = rgnComb.CombineRgn(&m_rgnTip, &m_rgnRoundRect, RGN_OR);
if (iRetComb == ERROR)
{
::AfxMessageBox("ERROR in Combining Region");
return -1;
}
SetWindowRgn(rgnComb.operator HRGN(), TRUE);
return 0;
}
/*-----------------------------------------------------------------------------
Function : CBalloonTip::OnPaint()
Created: Nov 1, 2001
Abstract : Gets called by MFC for a WM_PAINT message
Parameters :
See MFC documentation
Return Value : void
Exceptions : none
Revisions : none
----------------------------------------------------------------------------*/
void CBalloonTip::OnPaint()
{
CPaintDC dc(this); // device context for painting
CRect t_Rect;
GetClientRect(&t_Rect);
// Get the total balloon, inclding all cut out regions
CRgn rgnComb;
rgnComb.CreateRectRgn(t_Rect.left, t_Rect.top, t_Rect.right, t_Rect.bottom);
// Create a balloon tip from this total region
int iRetComb = rgnComb.CombineRgn(&m_rgnTip, &m_rgnRoundRect, RGN_OR);
if (iRetComb == ERROR)
{
::AfxMessageBox("ERROR in Combining Region");
return;
}
CBrush brOutlineBrush;
brOutlineBrush.CreateSolidBrush(RGB(0, 0, 0)); // blue
CBrush brFillBrush;
brFillBrush.CreateSolidBrush(RGB(249, 254, 188)); // light yellow
dc.FillRgn(&rgnComb, &brFillBrush); // Fill the balloon
dc.FrameRgn(&rgnComb, &brOutlineBrush, 2, 1); // Outline the balloon
int nBkMode = dc.SetBkMode(TRANSPARENT);
COLORREF clrPrevious = dc.SetTextColor(RGB(255, 0, 0));
CFont *pFont = dc.SelectObject(&m_fontBalloonText);
CSize czTextWidth = dc.GetTextExtent(m_strMessage);
if (czTextWidth.cx < m_rectText.Width())
{
dc.DrawText(m_strMessage, m_rectText, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
}
else
{
dc.DrawText(m_strMessage, m_rectText, DT_CENTER | DT_WORDBREAK);
}
// Restore the DC to its oroginal state
dc.SelectObject(pFont);
dc.SetBkColor(nBkMode);
dc.SetTextColor(clrPrevious);
}
/*-----------------------------------------------------------------------------
Function : CBalloonTip::PreCreateWindow(CREATESTRUCT& cs)
Created: Dec 07, 2001
Abstract : Called by MFC when a before WM_CREATE message is received by the
window.
We override this function so that the window so created is not
visible in the Taskbar. We make this window the child of an
invisible parent m_wndInvisibleParent.
Parameters : Look in MFC documentation
Return Value : BOOL
Exceptions : none
Revisions : none
----------------------------------------------------------------------------*/
BOOL CBalloonTip::PreCreateWindow(CREATESTRUCT& cs)
{
if (!CWnd::PreCreateWindow(cs))
{
return FALSE;
}
// Create invisible parent window
if (!::IsWindow(m_wndInvisibleParent.m_hWnd))
{
// Try creating the invisible parent window
PCSTR pstrOwnerClass = ::AfxRegisterWndClass(0);
BOOL bError = m_wndInvisibleParent.CreateEx(
0,
pstrOwnerClass,
_T(""),
WS_POPUP,
0,
0,
0,
0,
NULL,
0
);
if (bError == FALSE)
{
return FALSE;
}
}
// Set the invisible window as the parent of "this" window
cs.hwndParent = m_wndInvisibleParent.m_hWnd;
return TRUE;
}
/*-----------------------------------------------------------------------------
Function : CBalloonTip::Create()
Created: Dec 07, 2001
Abstract : Called by the static CBalloonTip to create a Windows(R) window
Parameters : rect-> a rectangle in screen co ordinates where the balloon
will be displayed.
Return Value : BOOL
Exceptions : none
Revisions : none
----------------------------------------------------------------------------*/
BOOL CBalloonTip::Create(CRect rect)
{
m_rectWindow = rect;
PCSTR pstrOwnerClass = ::AfxRegisterWndClass(0);
BOOL bResult = CFrameWnd::Create(pstrOwnerClass, NULL, WS_OVERLAPPED, m_rectWindow);
return bResult;
}
/*-----------------------------------------------------------------------------
Function : CBalloonTip::Hide()
Created: Dec 08, 2001
Abstract : Provided to destroy a balloon before it self destructs. If it is
already hidden, it will have no effect.
Parameters : CBalloonTip*
Return Value : void
Exceptions : none
Revisions : none
----------------------------------------------------------------------------*/
void CBalloonTip::Hide(CBalloonTip* pBalloonTip)
{
if (CBalloonTip::nBalloonInstances != 0)
{
pBalloonTip->ShowWindow(SW_HIDE);
pBalloonTip->KillTimer(ID_TIMER);
pBalloonTip->DestroyWindow();
}
}
/*-----------------------------------------------------------------------------
int CBalloonTip::nBalloonInstances;
Abstract: Used for preventing more than one instance of the class CBalloonTip
-----------------------------------------------------------------------------*/
int CBalloonTip::nBalloonInstances = 0;
/*-----------------------------------------------------------------------------
Function : CBalloonTip::PostNcDestroy()
Created: Dec 08, 2001
Abstract : Last function the window receives when it is destroyed.
Used for resetting the counter CBalloonTip::nBalloonInstances to 0,
so that a new instance of this class can be created.
Parameters : void
Return Value : void
Exceptions : none
Revisions : none
----------------------------------------------------------------------------*/
void CBalloonTip::PostNcDestroy()
{
CBalloonTip::nBalloonInstances = 0;
CFrameWnd::PostNcDestroy();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -