MFC CStatic控件自绘,背景透明。

来源:互联网 发布:thinkpad怎么样知乎 编辑:程序博客网 时间:2024/06/04 19:30
#ifndef TrackControl_h__
#define TrackControl_h__

template <class BaseClass>
class CTrackControl : public BaseClass
{
public:
    CTrackControl()
    {
        m_bTracking = m_bHover = false;
    }


    virtual ~CTrackControl() {}


    bool IsHover() { return m_bHover; }


bool IsFocus() { return m_bFoucs; }


    virtual LRESULT WindowProc (UINT message, WPARAM wParam, LPARAM lParam)
    {
        switch (message)
        {
        case WM_MOUSEMOVE:
        {
            if (!m_bTracking)
            {
                m_bTracking = !m_bTracking;


                TRACKMOUSEEVENT TrackEvent;
                TrackEvent.cbSize = sizeof (TrackEvent);
                TrackEvent.hwndTrack = m_hWnd;
                TrackEvent.dwFlags = TME_LEAVE;// | TME_HOVER;
                TrackEvent.dwHoverTime = 0;
                _TrackMouseEvent (&TrackEvent);


                m_bHover = true;
                OnEventMouseEnter();
            }
        }
        break;


        case WM_MOUSELEAVE:
        {
            m_bTracking = m_bHover = m_bFoucs = false;
            OnEventMouseLeave();
        }
        break;


        case WM_SETFOCUS:
        {
m_bFoucs = true;
OnEventSetFocus();
        } break;


        case WM_KILLFOCUS:
        {
m_bFoucs = false;
OnEventKillFocus();
        } break;
        }


        return __super::WindowProc (message, wParam, lParam);
    }


protected:
    virtual void OnEventMouseEnter() = NULL;
    virtual void OnEventMouseLeave() = NULL;
    virtual void OnEventSetFocus() {}
    virtual void OnEventKillFocus() {}

protected:
bool m_bFoucs;
    bool m_bHover;
    bool m_bTracking;


};


#endif // TrackControl_h__


#ifndef SkinStatic_h__
#define SkinStatic_h__

#include "ShareControl.h"
#include "TrackControl.h"
#include "HelperTool.h"


// CSkinStatic


class SHARE_CONTROL_CLASS CSkinStatic : public CTrackControl<CStatic>
{
DECLARE_DYNAMIC(CSkinStatic)


protected:
COLORREF m_clrNormalText;
COLORREF m_clrHotText;


public:
CSkinStatic();
virtual ~CSkinStatic();


protected:
virtual void PreSubclassWindow();


protected:
virtual void OnEventMouseEnter();
virtual void OnEventMouseLeave();


public:
void SetWindowText(LPCTSTR lpString);
void RefreshStatic();


public:
afx_msg HBRUSH CtlColor(CDC* pDC, UINT /*nCtlColor*/);
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
DECLARE_MESSAGE_MAP()
};






#endif // SkinStatic_h__

// SkinStatic.cpp : implementation file
//

#include "stdafx.h"
#include "SkinStatic.h"


// CSkinStatic


IMPLEMENT_DYNAMIC(CSkinStatic, CStatic)


CSkinStatic::CSkinStatic()
{
m_clrNormalText = RGB(255, 140, 0);
m_clrHotText = RGB(0, 0, 255);
}


CSkinStatic::~CSkinStatic()
{
}




BEGIN_MESSAGE_MAP(CSkinStatic, CStatic)
ON_WM_CTLCOLOR_REFLECT()
ON_WM_ERASEBKGND()
END_MESSAGE_MAP()


// CSkinStatic message handlers

HBRUSH CSkinStatic::CtlColor(CDC* pDC, UINT /*nCtlColor*/)
{
pDC->SetBkMode(TRANSPARENT);

if (IsHover())
{
pDC->SetTextColor(m_clrHotText);
}
else
{
pDC->SetTextColor(m_clrNormalText);
}

return (HBRUSH)::GetStockObject(NULL_BRUSH);
}


BOOL CSkinStatic::OnEraseBkgnd(CDC* pDC)
{
return TRUE;
return CStatic::OnEraseBkgnd(pDC);
}

void CSkinStatic::SetWindowText(LPCTSTR lpString)
{
__super::SetWindowText(lpString);
RefreshStatic();
}

void CSkinStatic::OnEventMouseEnter()
{
RefreshStatic();
}

void CSkinStatic::OnEventMouseLeave()
{
RefreshStatic();
}


void CSkinStatic::RefreshStatic()
{
CWindowRect rcWindow(this->m_hWnd);
GetParent()->ScreenToClient(rcWindow);
GetParent()->InvalidateRect(rcWindow);
}

void CSkinStatic::PreSubclassWindow()
{
ModifyStyle(0, GetStyle() | SS_NOTIFY);

CTrackControl<CStatic>::PreSubclassWindow();
}