TOOLTIPWND.CPP
上传用户:lvjun8202
上传日期:2013-04-30
资源大小:797k
文件大小:8k
- // ToolTipWnd.cpp : implementation file
- //
- #include "stdafx.h"
- #include "ToolTipWnd.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CToolTipWnd
- CToolTipWnd::CToolTipWnd()
- {
- lpWndCls = AfxRegisterWndClass(0);
-
- //Defaults
- m_bMouseIn = false;
- m_bStuck = false;
-
- m_iWidth = 120;
- m_iHeight = 60;
- m_clrBkColor = RGB(249,254,188); //light yellow
- m_clrFrameColor = RGB(0,0,255); //blue
- m_clrTextColor = RGB(0,0,0); //black
- m_iFontHeight = 14;
- m_strFontName = "Arial";
- m_rcCurrFit = CRect(0,0,0,0);
- }
- CToolTipWnd::~CToolTipWnd()
- {
- BTOOLINFO *stToolInfo;
- CWnd *pWnd;
- for(POSITION pos = m_ToolPtr.GetStartPosition(); pos != NULL;)
- {
- m_ToolPtr.GetNextAssoc(pos, (void *&)pWnd, (void*&) stToolInfo);
- delete stToolInfo;
- }
- m_ToolPtr.RemoveAll();
- }
- BEGIN_MESSAGE_MAP(CToolTipWnd, CWnd)
- //{{AFX_MSG_MAP(CToolTipWnd)
- ON_WM_PAINT()
- ON_WM_CREATE()
- ON_WM_TIMER()
- ON_WM_SHOWWINDOW()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CToolTipWnd message handlers
- BOOL CToolTipWnd::Create(CWnd* pParentWnd)
- {
- BOOL bRet = CWnd::CreateEx(NULL, lpWndCls, NULL,
- WS_POPUP, 0, 0, m_iWidth, m_iHeight,
- pParentWnd->GetSafeHwnd(), NULL, NULL);
- m_hParentWnd = pParentWnd->GetSafeHwnd();
-
- if(bRet)
- SetOwner(pParentWnd);
- return bRet;
- }
- void CToolTipWnd::OnPaint()
- {
- CPaintDC dc(this); // device context for painting
-
- CRect rectCl;
- GetClientRect(&rectCl);
- CRgn rgnComb;
- rgnComb.CreateRectRgn(rectCl.left+10,rectCl.top,rectCl.right,rectCl.bottom);
- int iRetComb = rgnComb.CombineRgn(&rgnTri, &rgn, RGN_OR);
- if(iRetComb==ERROR)
- {
- AfxMessageBox("ERROR in Combining Region");
- return;
- }
- CBrush pBrush;
- pBrush.CreateSolidBrush(m_clrFrameColor);
-
- CBrush pBrush1;
- pBrush1.CreateSolidBrush(m_clrBkColor);
-
- dc.FillRgn( &rgnComb, &pBrush1);
- dc.FrameRgn(&rgnComb, &pBrush, 2, 1);
- dc.SetBkMode(TRANSPARENT);
- dc.SetTextColor(m_clrTextColor);
-
- CFont *pFont = dc.SelectObject(&m_fontText);
- //dc.Rectangle(&m_RectText);
- CSize czTextWidth = dc.GetTextExtent(m_strText);
-
- if( czTextWidth.cx < m_RectText.Width())
- dc.DrawText(m_strText, m_RectText, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
- else
- dc.DrawText(m_strText, m_RectText, DT_CENTER | DT_WORDBREAK);
-
-
- dc.SelectObject(pFont);
-
- }
- int CToolTipWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CWnd::OnCreate(lpCreateStruct) == -1)
- return -1;
-
- CRect rectCl;
- GetClientRect(&rectCl);
- int x=0, y=0;
- CRect rectTemp;
- rectTemp = rectCl;
- rectTemp.left = rectTemp.left + 10;
-
- x = (int)( (float)((float)rectTemp.Width() / 2.0) / 1.41421);
- y = (int)( (float)((float)rectTemp.Height() / 2.0) / 1.41421);
- m_RectText.top = ( (rectTemp.Height() / 2) - y);
- m_RectText.left = ( (rectTemp.Width() / 2) - x) + 10;
- m_RectText.right = ( (rectTemp.Width() / 2) + x) + 10;
- m_RectText.bottom = ( (rectTemp.Height() / 2) + y);
- rgn.m_hObject = NULL;
- rgnTri.m_hObject = NULL;
- rgnComb.m_hObject = NULL;
- BOOL bRegRet = rgn.CreateEllipticRgn(rectCl.left+10,rectCl.top,rectCl.right,rectCl.bottom);
-
- CPoint ptTri[3];
- ptTri[0].x = rectCl.left;
- ptTri[0].y = (rectCl.bottom / 2) - 10;
- ptTri[1].x = rectCl.left + 15;
- ptTri[1].y = (rectCl.bottom / 2) - 5;
- ptTri[2].x = rectCl.left + 15;
- ptTri[2].y = (rectCl.bottom / 2) + 5;
-
- ptTri[3].x = rectCl.left;
- ptTri[3].y = (rectCl.bottom / 2) - 10;
- BOOL bRegTriRet = rgnTri.CreatePolygonRgn(ptTri, 3, ALTERNATE);
- rgnComb.CreateRectRgn(rectCl.left+10,rectCl.top,rectCl.right,rectCl.bottom);
- int iRetComb = rgnComb.CombineRgn(&rgnTri, &rgn, RGN_OR);
- if(iRetComb == ERROR)
- {
- AfxMessageBox("ERROR in Combining Region");
- return -1;
- }
- int bRgnWnd = SetWindowRgn(rgnComb.operator HRGN( ), TRUE);
- // m_fontText.CreateFont(m_iFontHeight, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,m_strFontName);
- m_fontText.CreateStockObject(DEFAULT_GUI_FONT) ||
- m_fontText.CreateStockObject(SYSTEM_FONT);
-
- /* BOOL b = m_wndAlert.Create(NULL,"1234",WS_CHILD|WS_VISIBLE,CRect(50,2-0,200,200),this,1001,NULL);
- if(m_wndAlert.Load("green.gif"))
- {
- m_wndAlert.put_AutoSize(TRUE);
- m_wndAlert.Play();
- }
- */ return 0;
- }
- void CToolTipWnd::RelayEvent(LPMSG lpMsg)
- {
- if(GetSafeHwnd() == NULL)
- return;
- switch(lpMsg->message)
- {
- case WM_KEYDOWN:
- if(IsWindowVisible())
- {
- ShowWindow(SW_HIDE);
- }
- break;
- case WM_LBUTTONDOWN:
- case WM_RBUTTONDOWN:
- if(IsWindowVisible())
- {
- ShowWindow(SW_HIDE);
- }
- break;
- case WM_MOUSEMOVE:
- {
- CWnd *pFocusWnd = AfxGetApp()->m_pMainWnd->GetFocus();
- if(pFocusWnd==NULL)
- {
- break;
- }
- CWnd* pWnd = CWnd::FromHandle(lpMsg->hwnd);
- CWnd* pWndOwner = GetOwner();
- if(pWndOwner == NULL || pWndOwner != pWnd)
- {
- break;
- }
- CPoint pt;
- pt.x = lpMsg->pt.x;
- pt.y = lpMsg->pt.y;
-
- m_ptCur = pt;
- TOOLINFO ti;
- if(!m_bMouseIn)
- {
- if(pWnd->OnToolHitTest(pt,&ti) != -1)
- {
- m_rcCurrFit = ti.rect;
- // SetTimer(1,500,NULL);
- SetTimer(2,6000,NULL);
- if(m_bStuck && IsWindowVisible())
- {
- SetWindowPos(&wndTop,pt.x,pt.y,m_iWidth,m_iHeight,SWP_NOACTIVATE);
- ShowWindow(SW_SHOWNOACTIVATE);
- }
- m_bMouseIn = true;
- m_clrTextColor = ti.uFlags;
- m_strText = ti.lpszText;
- // break;
-
- SetWindowPos(&wndTop,pt.x,pt.y,m_iWidth,m_iHeight,SWP_NOACTIVATE);
- ShowWindow(SW_SHOWNOACTIVATE);
- // for(int i = 0; i<m_iHeight+10; i += 10)
- // {
- // MoveWindow(m_ptCur.x,m_ptCur.y,i*m_iWidth/m_iHeight,i);
- // OnPaint();
- /// Sleep(10);
- // }
-
- break;
- }
- }
- else
- {
- if(pWnd->OnToolHitTest(pt,&ti) == -1)
- {
- // KillTimer(1);
- KillTimer(2);
- }
- /* if(m_bStuck && IsWindowVisible())
- {
- SetWindowPos(&wndTop,pt.x,pt.y,m_iWidth,m_iHeight,SWP_NOACTIVATE);
- ShowWindow(SW_SHOWNOACTIVATE);
- }
- */
-
- if(!m_rcCurrFit.PtInRect(pt))
- {
- m_bMouseIn = false;
- ShowWindow(SW_HIDE);
- }
-
- break;
- }
-
- }
- break; //WM_MOUSEMOVE
- }
-
- }
- void CToolTipWnd::AddTool(CWnd *pWnd, CString strText, COLORREF clrTextColor)
- {
- BTOOLINFO *stToolInfo;
- if(!m_ToolPtr.Lookup( pWnd, ( void*& ) stToolInfo))
- {
- stToolInfo = new BTOOLINFO;
- stToolInfo->hwndTool = pWnd->GetSafeHwnd();
- stToolInfo->strToolText = strText;
- if(clrTextColor==NULL)
- stToolInfo->clrToolTextClr = m_clrTextColor;
- else
- stToolInfo->clrToolTextClr = clrTextColor;
-
- m_ToolPtr.SetAt(pWnd, stToolInfo);
- }
- }
- void CToolTipWnd::OnTimer(UINT nIDEvent)
- {
- // TODO: Add your message handler code here and/or call default
- if(nIDEvent == 1)
- {
- CWnd* pWndOwner = GetOwner();
- if(pWndOwner != NULL)
- {
- CPoint pt;
- GetCursorPos(&pt);
- CRect rc;
- pWndOwner->GetClientRect(rc);
- pWndOwner->ClientToScreen(&rc);
- if(rc.PtInRect(pt))
- {
- m_bMouseIn = true;
-
- SetWindowPos(&wndTop,m_ptCur.x,m_ptCur.y,m_iWidth,m_iHeight,SWP_NOACTIVATE);
- ShowWindow(SW_SHOWNOACTIVATE);
- for(int i = 0; i<m_iHeight+10; i += 10)
- {
- MoveWindow(m_ptCur.x,m_ptCur.y,i*m_iWidth/m_iHeight,i);
- OnPaint();
- Sleep(10);
- }
- }
- }
- }
- else
- {
- ShowWindow(SW_HIDE);
- }
- KillTimer(nIDEvent);
- CWnd::OnTimer(nIDEvent);
- }
- BOOL CToolTipWnd::PreTranslateMessage(MSG* pMsg)
- {
- // TODO: Add your specialized code here and/or call the base class
- if (pMsg->message == WM_KEYDOWN ||
- pMsg->message == WM_SYSKEYDOWN ||
- pMsg->message == WM_LBUTTONDOWN ||
- pMsg->message == WM_RBUTTONDOWN ||
- pMsg->message == WM_MBUTTONDOWN ||
- pMsg->message == WM_NCLBUTTONDOWN ||
- pMsg->message == WM_NCRBUTTONDOWN ||
- pMsg->message == WM_NCMBUTTONDOWN)
- {
- ShowWindow(SW_HIDE);
- return TRUE; // message handled here
- }
-
- return CWnd::PreTranslateMessage(pMsg);
- }
- void CToolTipWnd::OnShowWindow(BOOL bShow, UINT nStatus)
- {
- CWnd::OnShowWindow(bShow, nStatus);
-
- // TODO: Add your message handler code here
- // if(m_wndAlert.GetSafeHwnd())
- // m_wndAlert.ShowWindow(bShow);
- }