📄 xinfotip.cpp
字号:
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
#include "stdafx.h" // PCH
#include "XInfoTip.h" // Class interface
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define CX_ICON 3
#define CX_ICON_MARGIN 5 // Width of margin between icon and tip text
#define DEFAULT_SHOW_DELAY 10 // Default show delay (ms)
#define TIMER_HIDE 2000 // Hide timer (ms)
/////////////////////////////////////////////////////////////////////
//
// CXInfoTip::CXInfoTip()
//
// DESCRIPTION
//
// Constructor
//
/////////////////////////////////////////////////////////////////////
CXInfoTip::CXInfoTip()
{
// Register the class
m_szClass = AfxRegisterWndClass(0);
m_hIcon = NULL;
// Set the delay defaults
m_nShowDelay = DEFAULT_SHOW_DELAY;
m_IconSize = CSize(0, 0);
m_ptOrigin = CPoint(0, 0);
}
/////////////////////////////////////////////////////////////////////
//
// CXInfoTip::~CXInfoTip()
//
// DESCRIPTION
//
// Deconstructor
//
/////////////////////////////////////////////////////////////////////
CXInfoTip::~CXInfoTip()
{
if (m_ToolMap.size())
m_ToolMap.clear();
}
// Message map
BEGIN_MESSAGE_MAP(CXInfoTip, CWnd)
//{{AFX_MSG_MAP(CXInfoTip)
ON_WM_PAINT()
ON_WM_CREATE()
ON_WM_TIMER()
ON_WM_DESTROY()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////
//
// CXInfoTip::Create()
//
// DESCRIPTION
//
// Creates the tip window
//
// RETURNS
//
// [BOOL] - TRUE on success, FALSE on failure
//
// PARAMETERS
//
// [pParentWnd] - Pointer to parent window
//
/////////////////////////////////////////////////////////////////////
BOOL CXInfoTip::Create(CWnd* pParentWnd)
{
BOOL bSuccess;
// Must have a parent
ASSERT(pParentWnd != NULL);
bSuccess = CreateEx(NULL, m_szClass, NULL, WS_POPUP, 0, 0, 0, 0, pParentWnd->GetSafeHwnd(), NULL, NULL);
// Use default GUI font for default font
m_pFont = CFont::FromHandle((HFONT)::GetStockObject(SYSTEM_FONT));
return bSuccess;
}
/////////////////////////////////////////////////////////////////////
//
// CXInfoTip::Show()
//
// DESCRIPTION
//
// Shows the tip window
//
// RETURNS
//
// [void]
//
// PARAMETERS
//
// [szText] - Tip text
// [pt] - Coordinates to display tip window
// or NULL to use the current cursor position
//
/////////////////////////////////////////////////////////////////////
void CXInfoTip::Show(CString szText, CPoint *pt /* = NULL */)
{
if (pt != NULL)
m_ptOrigin = *pt;
else
GetCursorPos(&m_ptOrigin);
m_szText = szText;
// Start the show timer
m_nTimer = SetTimer(timerShow, m_nShowDelay, NULL);
}
/////////////////////////////////////////////////////////////////////
//
// CXInfoTip::OnPaint()
//
// DESCRIPTION
//
// Paint the window
//
// RETURNS
//
// [void]
//
/////////////////////////////////////////////////////////////////////
void CXInfoTip::OnPaint()
{
CPaintDC dc( this ); // device context for painting
CRect rc;
CBrush WindowBrush;
CFont *pSysFont;
CRect r;
// Get the client rectangle
GetClientRect(rc);
// Create the brushes
WindowBrush.CreateSolidBrush(RGB(0xff,0xff,0xe1));
GetToolTipsRect(&dc,&r);
// Draw the frame
dc.FillRect(&r,&WindowBrush);
dc.FrameRect(&r, &CBrush(RGB(0,0,0)));
// Adjust the area for the icon
rc.DeflateRect(CX_ICON, 0, 0, 0);
if (m_hIcon != NULL)
rc.left = rc.left + m_IconSize.cx + CX_ICON_MARGIN ;
// Set the font
pSysFont = (CFont *)dc.SelectObject(m_pFont);
// Draw the tip text
dc.SetBkMode( TRANSPARENT );
dc.DrawText(m_szText, &rc, DT_TOP | DT_LEFT);
// Draw the icon
if (m_hIcon != NULL)
{
int cy = (rc.Height() - m_IconSize.cy)/2;
dc.DrawIcon(CX_ICON,cy, m_hIcon);
}
// Clean up GDI
dc.SelectObject(pSysFont);
}
/////////////////////////////////////////////////////////////////////
//
//
/////////////////////////////////////////////////////////////////////
BOOL CXInfoTip::GetToolTipsRect(CDC* pDC,CRect* rect)
{
CRect rcWnd(0,0,0,0);
CFont *pSysFont;
ASSERT(pDC != NULL);
ASSERT(rect != NULL);
pSysFont = (CFont *)pDC->SelectObject(m_pFont);
pDC->DrawText(m_szText, &rcWnd, DT_CALCRECT);
pDC->SelectObject(pSysFont);
if (m_hIcon != NULL)
rcWnd.right = rcWnd.right + m_IconSize.cx + CX_ICON_MARGIN + CX_ICON;
if (rcWnd.Height() < m_IconSize.cy)
rcWnd.bottom = rcWnd.top + m_IconSize.cy;
*rect = rcWnd;
return TRUE;
}
/////////////////////////////////////////////////////////////////////
//
// CXInfoTip::OnCreate()
//
// DESCRIPTION
//
// Window creation
//
// RETURNS
//
// [int] - Zero on success, -1 otherwise
//
// PARAMETERS
//
// [lpCreateStruct] - Pointer to creation structure
//
/////////////////////////////////////////////////////////////////////
int CXInfoTip::OnCreate( LPCREATESTRUCT lpCreateStruct )
{
if ( CWnd::OnCreate( lpCreateStruct ) == -1 )
return -1;
return 0;
}
/////////////////////////////////////////////////////////////////////
//
// CXInfoTip::OnTimer()
//
// DESCRIPTION
//
// Timer event
//
// RETURNS
//
// [void]
//
// PARAMETERS
//
// [nIDEvent] - Timer identifier
//
/////////////////////////////////////////////////////////////////////
void CXInfoTip::OnTimer( UINT nIDEvent )
{
CSize WindowSize;
CDC *pDC;
CPoint ptCursor;
CRect r;
switch (nIDEvent)
{
/////////////////////////////////////////////////////////////////////
// Show the tip window
/////////////////////////////////////////////////////////////////////
case timerShow:
KillTimer(m_nTimer);
pDC = GetDC();
GetToolTipsRect(pDC,&r);
ReleaseDC(pDC);
int x,y;
x = m_ptOrigin.x - r.Width() ;
y = m_ptOrigin.y - r.Height() ;
x= x<0 ? 0 : x;
y= y<0 ? 0 : y;
if(x + r.Width() > 800)
x = 800 - WindowSize.cx -4;
if(y + r.Height() > 600)
x = 600 - WindowSize.cy -3;
SetWindowPos(&wndTop, x, y , r.Width(), r.Height(), SWP_NOACTIVATE | SWP_SHOWWINDOW);
TRACE(_T("timerShow...wnd size(%d,%d,%d,%d)\r\n"),
x,y,
r.Width(),r.Height());
m_nTimer = SetTimer(timerHide, TIMER_HIDE, NULL);
break;
/////////////////////////////////////////////////////////////////////
// Hide the tip window
/////////////////////////////////////////////////////////////////////
case timerHide:
GetCursorPos(&ptCursor);
// if (ptCursor != m_ptOrigin)
{
KillTimer(m_nTimer);
ShowWindow(SW_HIDE);
}
break;
}
CWnd::OnTimer(nIDEvent);
}
/////////////////////////////////////////////////////////////////////
//
// CXInfoTip::OnDestroy()
//
// DESCRIPTION
//
// Window destruction
//
// RETURNS
//
// [void]
//
/////////////////////////////////////////////////////////////////////
void CXInfoTip::OnDestroy()
{
KillTimer(m_nTimer);
CWnd::OnDestroy();
}
/////////////////////////////////////////////////////////////////////
//
// CXInfoTip::RelayEvent()
//
// DESCRIPTION
//
// Call this in the parent's PreTranslateMessage() to
// relay tooltip event messages.
//
// RETURNS
//
// [void]
//
// PARAMETERS
//
// [lpMsg] - Pointer to message structure
//
/////////////////////////////////////////////////////////////////////
void CXInfoTip::RelayEvent(LPMSG lpMsg)
{
CPoint point;
CWnd *pWindow;
CString szTooltipText;
switch(lpMsg->message)
{
case WM_LBUTTONDOWN:
GetCursorPos(&point);
TRACE(_T("L down..%d %d.\r\n"),point.x,point.y);
// if (point != m_ptOrigin)
{
// Find the tool
pWindow = WindowFromPoint(point);
TRACE(_T(" pWindow .0x%.8X..\r\n"),pWindow);
if (pWindow != NULL)
{
HWND hWnd = pWindow->GetSafeHwnd();
mapIter item = m_ToolMap.find(hWnd);
if (item == m_ToolMap.end())
{
break;
}
TipToolInfo & Info = item->second;
m_ptOrigin = point;
TRACE(_T(" Show ...\r\n"));
Show(Info.szText, &point);
}
}
break;
case WM_RBUTTONDOWN:
case WM_MBUTTONDOWN:
case WM_LBUTTONUP:
ShowWindow(SW_HIDE);
break;
case WM_MOUSEMOVE:
break;
}
}
/////////////////////////////////////////////////////////////////////
//
// CXInfoTip::SetIcon()
//
// DESCRIPTION
//
// Sets the tip window icon
//
// RETURNS
//
// [void]
//
// PARAMETERS
//
// [hIcon] - Handle to the icon
//
/////////////////////////////////////////////////////////////////////
void CXInfoTip::SetIcon(HICON hIcon)
{
ICONINFO IconInfo;
m_hIcon = hIcon;
if (hIcon == NULL)
{
m_IconSize = CSize(0, 0);
return;
}
// Get the icon sizes
ZeroMemory(&IconInfo, sizeof(ICONINFO));
::GetIconInfo(m_hIcon, &IconInfo);
m_IconSize.cx = (BYTE)(IconInfo.xHotspot * 2);
m_IconSize.cy = (BYTE)(IconInfo.yHotspot * 2);
::DeleteObject(IconInfo.hbmMask);
::DeleteObject(IconInfo.hbmColor);
if (IsWindow(m_hWnd))
RedrawWindow();
}
void CXInfoTip::SetIcon(int nIconIn)
{
HICON hIcon = NULL;
HINSTANCE hInstResource = NULL;
hInstResource = AfxFindResourceHandle(MAKEINTRESOURCE(nIconIn), RT_GROUP_ICON);
hIcon = (HICON)::LoadImage(hInstResource, MAKEINTRESOURCE(nIconIn), IMAGE_ICON, 16, 16, 0);
m_hIcon = hIcon;
m_IconSize.cx = m_IconSize.cy = 16;
}
/////////////////////////////////////////////////////////////////////
//
// CXInfoTip::AddTool()
//
// DESCRIPTION
//
// Adds a tool
//
// RETURNS
//
// [void]
//
// PARAMETERS
//
// [pWnd] - Pointer to the tool window
// [szTooltipText] - Text to display when the cursor hovers
// over the window (pWnd)
// [hIcon] - Icon to display in the tooltip, or NULL
// to display no icon.
//
/////////////////////////////////////////////////////////////////////
void CXInfoTip::AddTool(CWnd *pWnd, LPCTSTR szTooltipText, HICON hIcon /* = NULL */)
{
TRACE(_T("CXInfoTip::AddTool(hWnd=0x%08X)\n"), pWnd->GetSafeHwnd());
ASSERT(pWnd != NULL);
// Store the tool information
TipToolInfo Info;
Info.szText = szTooltipText;
Info.hIcon = hIcon;
HWND hWnd = pWnd->GetSafeHwnd();
mapIter item = m_ToolMap.find(hWnd);
if (item == m_ToolMap.end())
{
m_ToolMap.insert(std::make_pair(hWnd, Info));
}
else
AfxMessageBox(_T("Wnd is in the tooltips!"));
};
/////////////////////////////////////////////////////////////////////
//
// CXInfoTip::RemoveTool()
//
// DESCRIPTION
//
// Removes a tool
//
// RETURNS
//
// [void]
//
// PARAMETERS
//
// [pWnd] - Pointer to the tool window to remove
//
/////////////////////////////////////////////////////////////////////
void CXInfoTip::RemoveTool(CWnd *pWnd)
{
TRACE (_T("CXInfoTip::RemoveTool(hWnd=0x%08X)\n"), pWnd->GetSafeHwnd());
ASSERT(pWnd);
HWND hWnd = pWnd->GetSafeHwnd();
mapIter item = m_ToolMap.find(hWnd);
if (item == m_ToolMap.end())
return;
m_ToolMap.erase(item);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -