⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 titletip.cpp

📁 电力监控系统 实时告警处理程序
💻 CPP
字号:
////////////////////////////////////////////////////////////////////////////
// TitleTip.cpp : implementation file
//
// Code taken from www.codeguru.com. - thanks Zafir!
//
// Modifed 10 Apr 1999  Now accepts a LOGFONT pointer and 
//                      a tracking rect in Show(...)  (Chris Maunder)
//         18 Apr 1999  Resource leak in Show fixed by Daniel Gehriger
   

#include "stdafx.h"
#include "XListCtrl.h"

#ifndef GRIDCONTROL_NO_TITLETIPS

#include "TitleTip.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CTitleTip

CTitleTip::CTitleTip()
{
	// Register the window class if it has not already been registered.
	WNDCLASS wndcls;
	HINSTANCE hInst = AfxGetInstanceHandle();
	if(!(::GetClassInfo(hInst, TITLETIP_CLASSNAME, &wndcls)))
	{
		// otherwise we need to register a new class
		wndcls.style			= CS_SAVEBITS;
		wndcls.lpfnWndProc		= ::DefWindowProc;
		wndcls.cbClsExtra		= wndcls.cbWndExtra = 0;
		wndcls.hInstance		= hInst;
		wndcls.hIcon			= NULL;
		wndcls.hCursor			= LoadCursor( hInst, IDC_ARROW );
		wndcls.hbrBackground	= (HBRUSH)(COLOR_INFOBK + 1); 
		wndcls.lpszMenuName		= NULL;
		wndcls.lpszClassName	= TITLETIP_CLASSNAME;

		if (!AfxRegisterClass(&wndcls))
			AfxThrowResourceException();
	}
}

CTitleTip::~CTitleTip()
{
}


BEGIN_MESSAGE_MAP(CTitleTip, CWnd)
	//{{AFX_MSG_MAP(CTitleTip)
	ON_WM_MOUSEMOVE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CTitleTip message handlers

BOOL CTitleTip::Create(CWnd * pParentWnd)
{
	ASSERT_VALID(pParentWnd);

	DWORD dwStyle = WS_BORDER | WS_POPUP; 
	DWORD dwExStyle = WS_EX_TOOLWINDOW | WS_EX_TOPMOST;
	m_pParentWnd = pParentWnd;

	return CreateEx(dwExStyle, TITLETIP_CLASSNAME, NULL, dwStyle, 
                    CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 
		            NULL, NULL, NULL );
}


// Show 		 - Show the titletip if needed
// rectTitle	 - The rectangle within which the original 
//				    title is constrained - in client coordinates
// lpszTitleText - The text to be displayed
// xoffset		 - Number of pixel that the text is offset from
//				   left border of the cell
void CTitleTip::Show(CRect rectTitle, LPCTSTR lpszTitleText, int xoffset /*=0*/,
                     LPRECT lpHoverRect /*=NULL*/,
                     LPLOGFONT lpLogFont /*=NULL*/)
{
	ASSERT( ::IsWindow( GetSafeHwnd() ) );

//    if (rectTitle.IsRectEmpty())        return;
	if(strcmp("",lpszTitleText)==0) return;
	if( IsWindowVisible() ) 		return;
	if( GetFocus() == NULL )		return;

	int n=0;int start=0;
	int witch=0; 
	CClientDC dc(this);
	CSize size;
	for(int i=0;i<1000;i++)
	{
		if(*(lpszTitleText+i)==0x0d)
		{
			if(*(lpszTitleText+i+1)==0x0a)
			{
				m_STR_OPT.m_len[n]=i-start;
				m_STR_OPT.m_str[n]=lpszTitleText;
				m_STR_OPT.m_str[n]=m_STR_OPT.m_str[n].Mid(start,m_STR_OPT.m_len[n]);
				if(m_STR_OPT.m_len[n]>witch)
				{
					witch= m_STR_OPT.m_len[n];
					size= dc.GetTextExtent( m_STR_OPT.m_str[n] );
				}
				start=i+2;
				n++;
			}
		}
		if(*(lpszTitleText+i)==0x00)
		{
				m_STR_OPT.m_len[n]=i-start;
				if(m_STR_OPT.m_len[n]>witch)		witch= m_STR_OPT.m_len[n];
				m_STR_OPT.m_str[n]=lpszTitleText;
				m_STR_OPT.m_str[n]=m_STR_OPT.m_str[n].Mid(start,m_STR_OPT.m_len[n]);
				start=i+2;
				n++;
				if(n<NUM_OPT)			m_STR_OPT.m_len[n]=-1;
			break;
		}
	}
    TEXTMETRIC tm;
    dc.GetTextMetrics(&tm);

   m_rectHover = (lpHoverRect != NULL)? lpHoverRect : rectTitle;
	m_pParentWnd->ClientToScreen( m_rectHover );
  ScreenToClient( m_rectHover );

	// Determine the width of the text
  rectTitle.left=xoffset;
	m_pParentWnd->ClientToScreen( rectTitle );

	CFont font, *pOldFont = NULL;
    if (lpLogFont)
    {
        font.CreateFontIndirect(lpLogFont);
	    pOldFont = dc.SelectObject( &font );
    }
    else	    pOldFont = dc.SelectObject( m_pParentWnd->GetFont() );


    size.cx += tm.tmOverhang;

	CRect rectDisplay = rectTitle;
//	rectDisplay.left = xoffset;
	rectDisplay.right = rectDisplay.left + size.cx +OFFSET*2;
	rectDisplay.bottom=rectDisplay.top+size.cy*n+OFFSET*2;
    
    // Do not display if the text fits within available space
//    if( rectDisplay.right >= rectTitle.right-xoffset )
    {
        // Show the titletip
        SetWindowPos( &wndTop, rectDisplay.left+20, rectDisplay.top+20, 
            rectDisplay.Width(), rectDisplay.Height(), 
            SWP_SHOWWINDOW|SWP_NOACTIVATE );
//0D 0A转行        
        dc.SetBkMode( TRANSPARENT );
		for(int i=0;i<NUM_OPT;i++)
		{
			if(m_STR_OPT.m_len[i]<0) break;
			dc.TextOut( OFFSET, i*tm.tmHeight+OFFSET, m_STR_OPT.m_str[i] );
		}
        SetCapture();
    }
    
    dc.SelectObject( pOldFont );
}

void CTitleTip::Hide()
{
  	if (!::IsWindow(GetSafeHwnd()))
        return;

    if (GetCapture()->GetSafeHwnd() == GetSafeHwnd())
        ReleaseCapture();

	ShowWindow( SW_HIDE );
}

void CTitleTip::OnMouseMove(UINT nFlags, CPoint point) 
{
    if (!m_rectHover.PtInRect(point)) 
    {
        Hide();
        
        // Forward the message
        ClientToScreen( &point );
        CWnd *pWnd = WindowFromPoint( point );
        if ( pWnd == this ) 
            pWnd = m_pParentWnd;
        
        int hittest = (int)pWnd->SendMessage(WM_NCHITTEST,0,MAKELONG(point.x,point.y));
        
        if (hittest == HTCLIENT) {
            pWnd->ScreenToClient( &point );
            pWnd->PostMessage( WM_MOUSEMOVE, nFlags, MAKELONG(point.x,point.y) );
        } else {
            pWnd->PostMessage( WM_NCMOUSEMOVE, hittest, MAKELONG(point.x,point.y) );
        }
    }
}

BOOL CTitleTip::PreTranslateMessage(MSG* pMsg) 
{
	CWnd *pWnd;
	int hittest;
	switch (pMsg->message)
	{
	case WM_LBUTTONDOWN:
	case WM_RBUTTONDOWN:
	case WM_MBUTTONDOWN:
		POINTS pts = MAKEPOINTS( pMsg->lParam );
		POINT  point;
		point.x = pts.x;
		point.y = pts.y;
		ClientToScreen( &point );
		pWnd = WindowFromPoint( point );
		if( pWnd == this ) 
			pWnd = m_pParentWnd;

		hittest = (int)pWnd->SendMessage(WM_NCHITTEST,0,MAKELONG(point.x,point.y));

		if (hittest == HTCLIENT) {
			pWnd->ScreenToClient( &point );
			pMsg->lParam = MAKELONG(point.x,point.y);
		} else {
			switch (pMsg->message) {
			case WM_LBUTTONDOWN: 
				pMsg->message = WM_NCLBUTTONDOWN;
				break;
			case WM_RBUTTONDOWN: 
				pMsg->message = WM_NCRBUTTONDOWN;
				break;
			case WM_MBUTTONDOWN: 
				pMsg->message = WM_NCMBUTTONDOWN;
				break;
			}
			pMsg->wParam = hittest;
			pMsg->lParam = MAKELONG(point.x,point.y);
		}

        Hide();
		pWnd->PostMessage(pMsg->message,pMsg->wParam,pMsg->lParam);
		return TRUE;	
		
	case WM_KEYDOWN:
	case WM_SYSKEYDOWN:
        Hide();
		m_pParentWnd->PostMessage( pMsg->message, pMsg->wParam, pMsg->lParam );
		return TRUE;
	}

	if( GetFocus() == NULL )
	{
        Hide();
		return TRUE;
	}

	return CWnd::PreTranslateMessage(pMsg);
}

#endif // GRIDCONTROL_NO_TITLETIPS

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -