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

📄 taskbarmsgwnd.cpp

📁 vc++6.0开发网络典型应用实例导航 1. 本光盘提供了本书中所有的实例源程序文件。 2. 附录文件夹下是Winsock 函数参考以及错误码列表
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    // Start creating two fonts, one underlined , other non underlined
    //
    // LOGFONT structure for font properties
    LOGFONT lf;
    ::ZeroMemory (&lf, sizeof (lf));
    lf.lfHeight = 100;
	lf.lfWeight = FW_NORMAL;
    lf.lfUnderline = TRUE;

    ::strcpy (lf.lfFaceName, _T("MS Sans Serif"));
	
    // Prepare for an underlined font
    m_fontMessageUnderline.CreatePointFontIndirect(&lf);

    // Prepare  an non undelined font
    lf.lfUnderline = FALSE;
    m_fontMessageNoUnderline.CreatePointFontIndirect(&lf);

    // Initialize the cursor.

#ifdef IDC_HAND //dang everyone complains about this
    m_hCursor = ::LoadCursor(NULL, IDC_HAND); // Use IDC_HAND if it compiles
#endif 
    return 0;
}

void CTaskBarMsgWnd::OnMouseMove(UINT nFlags, CPoint point)
{
    // Register the tracking of Mouse entering and leaveing the window
    // for WM_MOUSEHOVER and WM_MOUSELEAVE
    TRACKMOUSEEVENT t_MouseEvent;
    t_MouseEvent.cbSize      = sizeof(TRACKMOUSEEVENT);
    t_MouseEvent.dwFlags     = TME_LEAVE | TME_HOVER;
    t_MouseEvent.hwndTrack   = m_hWnd;
    t_MouseEvent.dwHoverTime = 1;

    ::_TrackMouseEvent(&t_MouseEvent);
}

void CTaskBarMsgWnd::OnTimer(UINT nIDEvent)
{
    switch (nIDEvent)
    {
        case IDT_POP_WINDOW_TIMER:    // When the window comes up
        {
            switch (m_nTaskBarPos)
            {
                case STP_BOTTOM:
                case STP_RIGHT:
                {
                    m_nWndSize += 2;
                    if (m_nWndSize > m_nWndHeight)
                    {
                        KillTimer(IDT_POP_WINDOW_TIMER);
                        SetTimer(IDT_SHOW_WINDOW_TIMER, m_nMsgTimeOut, NULL);
                    }
                    else
                    {
                        // Keep sizing the window, show it
                        SetWindowPos(
                            &wndTopMost,
                            m_nWndLeft,
                            m_nWndBottom - m_nWndSize, 
                            m_nWndWidth - 10,
                            m_nWndSize,
                            SWP_SHOWWINDOW
                        );
                    }
                }
                break;

                case STP_TOP:
                {
                    m_nWndSize += 2;
                    if (m_nWndSize > m_nWndHeight)
                    {
                        KillTimer(IDT_POP_WINDOW_TIMER);
                        SetTimer(IDT_SHOW_WINDOW_TIMER, m_nMsgTimeOut, NULL);
                    }
                    else
                    {
                        // Keep sizing the window, collapse it
                        SetWindowPos(
                            &wndTopMost,
                            m_nWndLeft,
                            m_nWndTop, 
                            m_nWndWidth - 10,
                            m_nWndSize,
                            SWP_SHOWWINDOW
                        );
                    }
                }
                break;

                case STP_LEFT:
                {
                    m_nWndSize += 2;
                    if (m_nWndSize > m_nWndHeight)
                    {
                        KillTimer(IDT_POP_WINDOW_TIMER);
                        SetTimer(IDT_SHOW_WINDOW_TIMER, m_nMsgTimeOut, NULL);
                    }
                    else
                    {
                        // Keep sizing the window, collpase it
                        SetWindowPos(
                            &wndTopMost,
                            m_nWndLeft + 10,
                            m_nWndBottom - m_nWndSize, 
                            m_nWndWidth - 10,
                            m_nWndSize,
                            SWP_SHOWWINDOW
                        );
                    }
                }
                break;
            }
        }
        break;

        case IDT_SHOW_WINDOW_TIMER:
        {
            KillTimer(IDT_SHOW_WINDOW_TIMER);
            SetTimer(IDT_COLLAPSE_WINDOW_TIMER, m_nMsgWndCreationDelay, NULL);
        }
        break;

        case IDT_COLLAPSE_WINDOW_TIMER:
        {
            switch (m_nTaskBarPos)
            {
                case STP_BOTTOM:
                case STP_RIGHT:
                {
                    m_nWndSize -= 2;
                    if (m_nWndSize <= 0) 
                    {
                        KillTimer(IDT_COLLAPSE_WINDOW_TIMER);
                        m_nWndSize = 0;

                        DestroyWindow();
						Quit(); // Use because it is in a separate thread
                    }
                    else
                    {
                        // Keep showing the window, collapse it
                        SetWindowPos(
                            &wndTopMost,
                            m_nWndLeft,
                            m_nWndBottom - m_nWndSize, 
                            m_nWndWidth - 10,
                            m_nWndSize,
                            SWP_SHOWWINDOW
                        );      
                    }
                }
                break;

                case STP_TOP:
                {
                    m_nWndSize -= 2;
                    if (m_nWndSize <= 0)
                    {
                        KillTimer(IDT_COLLAPSE_WINDOW_TIMER);
                        m_nWndSize = 0;

                        DestroyWindow();
						Quit(); // Use because it is in a separate thread
                    }
                    else
                    {
                        SetWindowPos(
                            &wndTopMost,
                            m_nWndLeft,
                            m_nWndTop, 
                            m_nWndWidth - 10,
                            m_nWndSize,
                            SWP_SHOWWINDOW
                        );
                    }
                }
                break;

                case STP_LEFT:
                {
                    m_nWndSize -= 2;
                    if (m_nWndSize <= 0)
                    {
                        KillTimer(IDT_COLLAPSE_WINDOW_TIMER);
                        m_nWndSize = 0;

                        DestroyWindow();
						Quit(); // Use because it is in a separate thread
                    }
                    else
                    {
                        SetWindowPos(
                            &wndTopMost,
                            m_nWndLeft + 10,
                            m_nWndBottom - m_nWndSize, 
                            m_nWndWidth - 10,
                            m_nWndSize,
                            SWP_SHOWWINDOW
                        );
                    }
                }
                break;
            }
        }
        break;
    }

}

LRESULT CTaskBarMsgWnd::OnMouseHover(WPARAM w, LPARAM l)
{
    if (m_bMouseOverWnd == FALSE) // Mouse was not on window
    {
        CClientDC dc(this);
        CFont* pFont = dc.SelectObject(&m_fontMessageUnderline);

        COLORREF clrPreviousColor = dc.SetBkColor(m_clrBox); 
        COLORREF clrPreviousTextColor = dc.SetTextColor(m_clrText);
        dc.SetBkMode(TRANSPARENT);
		dc.DrawState(CPoint(0, 0), CSize(130, 105), bmBack, NULL, NULL);
		dc.DrawIcon(CPoint(94, 68), icBack);
        dc.DrawText(m_strMsg, &m_rectMsgRect, DT_VCENTER | DT_CENTER | DT_WORDBREAK | DT_END_ELLIPSIS);
        dc.SelectObject(pFont);
        dc.SetBkColor(clrPreviousColor);
        dc.SetTextColor(clrPreviousTextColor);
    

        m_bMouseOverWnd = TRUE; // Mouse is now over window
    }
    return 0;
}

LRESULT CTaskBarMsgWnd::OnMouseLeave(WPARAM w, LPARAM l)
{
    if (m_bMouseOverWnd) // Mouse was over window, now it is leaving
    {
        CClientDC dc(this);
        CFont* pFont = dc.SelectObject(&m_fontMessageNoUnderline);

        COLORREF clrPreviousBkColor = dc.SetBkColor(m_clrBox); 
        COLORREF clrPreviousTextColor = dc.SetTextColor(m_clrText);
        dc.SetBkMode(TRANSPARENT);
		dc.DrawState(CPoint(0, 0), CSize(130, 105), bmBack, NULL, NULL);
		dc.DrawIcon(CPoint(94, 68), icBack);
        dc.DrawText(m_strMsg, &m_rectMsgRect, DT_VCENTER | DT_CENTER | DT_WORDBREAK | DT_END_ELLIPSIS);

        // Restore the DC to its previous state
        dc.SelectObject(pFont);
        dc.SetBkColor(clrPreviousBkColor);
        dc.SetTextColor(clrPreviousTextColor);

        m_bMouseOverWnd = FALSE; // Mouse is not over window
    }
    return 0;
}

BOOL CTaskBarMsgWnd::OnSetCursor(CWnd* pWnd , UINT nHitTest , UINT message)
{
    if (nHitTest == HTCLIENT)
    {
        ::SetCursor(m_hCursor);  // Set cursor to HAND type when mouse is over window

        return TRUE;
    }
    
    return CFrameWnd::OnSetCursor (pWnd, nHitTest, message);
}

void CTaskBarMsgWnd::OnDestroy(void)
{
    ::DeleteObject(m_hCursor); // Free the cursor as it is a shared resource..
	if (GetApp()->View->LastPopup)
	{
		switch (m_nTaskBarPos)
		{
		case STP_BOTTOM:
		case STP_RIGHT:
		case STP_LEFT:
			if (GetApp()->View->LastPopup->Top >= Top)
			GetApp()->View->LastPopup = NULL;
			break;
		case STP_TOP:
			if (Top >= GetApp()->View->LastPopup->Top)
			GetApp()->View->LastPopup = NULL;
			break;
		}
	}

    CFrameWnd::OnDestroy();
}

HBRUSH CTaskBarMsgWnd::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
	return NULL;
}

void CTaskBarMsgWnd::OnPaint()
{
	CFont* pFont = NULL;
    CPaintDC dc(this);

    if (m_bMouseOverWnd)
    {
        pFont = dc.SelectObject(&m_fontMessageUnderline);
    }
    else
    {
        pFont = dc.SelectObject(&m_fontMessageNoUnderline);
    }

    COLORREF clrPreviousBkColor = dc.SetBkColor(m_clrBox); 
    COLORREF clrPreviousTextColor = dc.SetTextColor(m_clrText);
    
	// Show with the message with the new font
	dc.SetBkMode(TRANSPARENT);
	dc.DrawState(CPoint(0, 0), CSize(130, 105), bmBack, NULL, NULL);
	dc.DrawIcon(CPoint(94, 68), icBack);
    dc.DrawText(m_strMsg, &m_rectMsgRect, DT_VCENTER | DT_CENTER | DT_WORDBREAK | DT_END_ELLIPSIS);

    dc.SelectObject(pFont);
    dc.SetBkColor(clrPreviousBkColor);
    dc.SetTextColor(clrPreviousTextColor);
}

void CTaskBarMsgWnd::Quit()
{
	PostQuitMessage(0);
}

void CTaskBarMsgWnd::OnLButtonDown(UINT nFlags, CPoint point)
{
	View->NewConversation(Contact, NULL);
}

CTaskBarMsgWnd* CTaskBarMsgWnd::CreateObject(CString strMsg,
                                       unsigned int nWndWidth,
                                       unsigned int nWndHeight, 
                                       unsigned int nMsgTimeOut,         
                                       unsigned int nMsgWndCreationDelay, 
                                       CRect rectMsgRect,
                                       COLORREF clrBox,
                                       COLORREF clrText,
									   CContactView *ContactView,
									   CContact *With,
									   CTaskBarMsgWnd *pLast
                                    )
{
    CTaskBarMsgWnd* t_TaskBarMsgWnd = new CTaskBarMsgWnd(
												strMsg,
												nWndWidth,
												nWndHeight, 
												nMsgTimeOut,         
												nMsgWndCreationDelay, 
												rectMsgRect,
												clrBox,
												clrText,
												ContactView,
												With,
												pLast
                                              );


    return (t_TaskBarMsgWnd);
}

BOOL CTaskBarMsgWnd::PreCreateWindow(CREATESTRUCT& cs)
{
	if (!CFrameWnd::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,
				                    CW_USEDEFAULT,
                                    CW_USEDEFAULT,
                                    CW_USEDEFAULT, 
                                    CW_USEDEFAULT,
				                    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;
}

⌨️ 快捷键说明

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