智慧控制元件變幻

shaderdx發表於2014-12-31



標頭檔案

#pragma once


#include <GdiPlus.h>




// CCfsButton


class CCfsButton : public CButton
{
DECLARE_DYNAMIC(CCfsButton)


public:
CCfsButton();
virtual ~CCfsButton();


protected:
DECLARE_MESSAGE_MAP()


private:
int m_iCfsBtn;


enum EM_CFSBTN_ALIGNMODE
{
CFSBTN_HORIZ,//水平對齊
CFSBTN_VERT, //垂直對齊


MAX_CFSBTN_ALIGNMODE
};


int m_iTextAlign;
bool m_bTextShow;
bool m_bDrawBorder;
bool m_bMouseOnBtn;


COLORREF m_clrActiveBg;
COLORREF m_clrActiveFg;
COLORREF m_clrInactiveBg;
COLORREF m_clrInactiveFg;


struct SCfsButton
{
//CBitmap* pImage;
//HBITMAP hBitmap;
//Gdiplus::Image* pImage;
Gdiplus::Bitmap* pImage;
int iWidth;
int iHeight;


SCfsButton()
{
pImage = NULL;
iWidth = 0;
iHeight = 0;
}
};
SCfsButton m_btnComf[3];


private:
COLORREF GetDefActiveColor(void) const;
COLORREF GetDefInactiveColor(void) const;
void InitParam(void);
void UninitParam(void);
protected:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
virtual void PreSubclassWindow();
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg void OnKillFocus(CWnd* pNewWnd);


public:
void SetTextAlign(int iAlign);
void SetTextShow(bool bShow);
void SetDrawBorder(bool bBorder);
int GetTextALign(void) const;
bool GetTextShow(void) const;
bool GetDrawBorder(void) const;


void SetDefActiveBgColor(bool bRepaint = false);
void SetDefInactiveBgColor(bool bRepaint = false);
void SetDefActiveFgColor(bool bRepaint = false);
void SetDefInactiveFgColor(bool bRepaint = false);
void SetActiveBgColor(COLORREF clrSelf, bool bRepaint = false);
void SetInactiveBgColor(COLORREF clrSelf, bool bRepaint = false);
void SetActiveFgColor(COLORREF clrSelf, bool bRepaint = false);
void SetInactiveFgColor(COLORREF clrSelf, bool bRepaint = false);


COLORREF GetDefActiveBgColor(void) const;
COLORREF GetDefInactiveBgColor(void) const;
COLORREF GetDefActiveFgColor(void) const;
COLORREF GetDefInactiveFgColor(void) const;
COLORREF GetActiveBgColor(void) const;
COLORREF GetInactiveBgColor(void) const;
COLORREF GetActiveFgColor(void) const;
COLORREF GetInactiveFgColor(void) const;


bool SetIcon(LPCTSTR lpNormal, LPCTSTR lpOver = NULL, LPCTSTR lpDisable = NULL, CSize szBtn = CSize(0,0));
bool SetPng(LPCTSTR lpNormal, LPCTSTR lpOver = NULL, LPCTSTR lpDisable = NULL, CSize szBtn = CSize(0,0));
};



原始檔

// CfsButton.cpp : 實現檔案
//


#include "stdafx.h"
#include "TestDock.h"
#include "CfsButton.h"




#define SAFE_DELETEP(xx) if(xx){delete xx; xx=NULL;}
#define SAFE_DELETEA(xx) if(xx){delete [] xx; xx=NULL;}
#define SAFE_DELETEOBJECT(xx) if(xx){::DeleteObject(xx);xx=NULL;}


// CCfsButton


IMPLEMENT_DYNAMIC(CCfsButton, CButton)


CCfsButton::CCfsButton()
: m_iCfsBtn(0)
, m_iTextAlign(CFSBTN_HORIZ)
, m_bTextShow(true)
, m_bDrawBorder(false)
, m_bMouseOnBtn(false)
{
SetDefActiveBgColor();
SetDefActiveFgColor();
SetDefInactiveBgColor();
SetDefInactiveFgColor();
}


CCfsButton::~CCfsButton()
{
UninitParam();
}


void CCfsButton::InitParam( void )
{


}


void CCfsButton::UninitParam( void )
{
for (int i=0; i<3; ++i)
{
SAFE_DELETEOBJECT(m_btnComf[i].pImage);
}
}




BEGIN_MESSAGE_MAP(CCfsButton, CButton)
ON_WM_MOUSEMOVE()
ON_WM_KILLFOCUS()
END_MESSAGE_MAP()






// CCfsButton 訊息處理程式








void CCfsButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO:  新增您的程式碼以繪製指定項
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);


CPen *pOldPen = NULL;
BOOL bIsPressed  = (lpDrawItemStruct->itemState & ODS_SELECTED);
BOOL bIsFocused  = (lpDrawItemStruct->itemState & ODS_FOCUS);
BOOL bIsDisabled = (lpDrawItemStruct->itemState & ODS_DISABLED);


CRect itemRect = lpDrawItemStruct->rcItem;


if (bIsFocused)
{
CBrush br(RGB(0,0,0));  
pDC->FrameRect(&itemRect, &br);
itemRect.DeflateRect(1, 1);
}


COLORREF bgColor;
if ((m_bMouseOnBtn) || (bIsPressed))
{
bgColor = m_clrActiveBg;//GetActiveBgColor();
}
else
{
bgColor = m_clrInactiveBg;//GetInactiveBgColor();
}


CBrush br(bgColor);
pDC->FillRect(&itemRect, &br);


if (bIsPressed)
{
CBrush brBtnShadow(GetSysColor(COLOR_BTNSHADOW));
pDC->FrameRect(&itemRect, &brBtnShadow);
}
else
{
CPen penBtnHiLight(PS_SOLID, 0, GetSysColor(COLOR_BTNHILIGHT)); // White
CPen pen3DLight(PS_SOLID, 0, GetSysColor(COLOR_3DLIGHT));       // Light gray
CPen penBtnShadow(PS_SOLID, 0, GetSysColor(COLOR_BTNSHADOW));   // Dark gray
CPen pen3DDKShadow(PS_SOLID, 0, GetSysColor(COLOR_3DDKSHADOW)); // Black


pOldPen = pDC->SelectObject(&penBtnHiLight);
pDC->MoveTo(itemRect.left, itemRect.bottom-1);
pDC->LineTo(itemRect.left, itemRect.top);
pDC->LineTo(itemRect.right, itemRect.top);
// Light gray line
pDC->SelectObject(pen3DLight);
pDC->MoveTo(itemRect.left+1, itemRect.bottom-1);
pDC->LineTo(itemRect.left+1, itemRect.top+1);
pDC->LineTo(itemRect.right, itemRect.top+1);
// Disegno i bordi a destra e in basso
// Black line
pDC->SelectObject(pen3DDKShadow);
pDC->MoveTo(itemRect.left, itemRect.bottom-1);
pDC->LineTo(itemRect.right-1, itemRect.bottom-1);
pDC->LineTo(itemRect.right-1, itemRect.top-1);
// Dark gray line
pDC->SelectObject(penBtnShadow);
pDC->MoveTo(itemRect.left+1, itemRect.bottom-2);
pDC->LineTo(itemRect.right-2, itemRect.bottom-2);
pDC->LineTo(itemRect.right-2, itemRect.top);
//
pDC->SelectObject(pOldPen);
}


// Read the button title
CString csTitle;
GetWindowText(csTitle);


// If we don't want the title displayed
if (!m_bTextShow) 
{
csTitle.Empty();
}


CRect captionRect = itemRect;//lpDrawItemStruct->rcItem;


// Draw the iamge
Gdiplus::Graphics grap(pDC->m_hDC);//
int iItem = 0;
iItem = (m_bMouseOnBtn) ? 1 : (bIsDisabled ? 2 : 0);
if (m_btnComf[iItem].pImage)
{
if (bIsPressed)
{
grap.DrawImage(m_btnComf[iItem].pImage, 1, 1, m_btnComf[0].iWidth-1, m_btnComf[0].iHeight-1);
}
else
{
grap.DrawImage(m_btnComf[iItem].pImage, 0, 0, m_btnComf[0].iWidth, m_btnComf[0].iHeight);
}
}


// Write the button title (if any)
if (!csTitle.IsEmpty())
{
if (bIsPressed)
{
captionRect.OffsetRect(1, 1);
}


if ((m_bMouseOnBtn) || (bIsPressed)) 
{
pDC->SetTextColor(m_clrActiveFg);//GetActiveFgColor()
pDC->SetBkColor(m_clrActiveBg);//GetActiveBgColor()

else 
{
pDC->SetTextColor(m_clrInactiveFg);//GetInactiveFgColor()
pDC->SetBkColor(m_clrInactiveBg);//GetInactiveBgColor()
}
// Center text
CRect centerRect = captionRect;
pDC->SetBkMode(TRANSPARENT);
pDC->DrawText(csTitle, -1, captionRect, DT_SINGLELINE|DT_CALCRECT);
captionRect.OffsetRect((centerRect.Width() - captionRect.Width())/2, (centerRect.Height() - captionRect.Height())/2);


pDC->DrawState(captionRect.TopLeft(), captionRect.Size(), (LPCTSTR)csTitle, (DSS_NORMAL), TRUE, 0, (CBrush*)NULL);


if (bIsFocused)
{
CRect focusRect = itemRect;
focusRect.DeflateRect(3, 3);
pDC->DrawFocusRect(&focusRect);
}
}
}


void CCfsButton::PreSubclassWindow()
{
// TODO: 在此新增專用程式碼和/或呼叫基類
SetButtonStyle(GetButtonStyle() | BS_OWNERDRAW);


CButton::PreSubclassWindow();
}


void CCfsButton::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: 在此新增訊息處理程式程式碼和/或呼叫預設值


CButton::OnMouseMove(nFlags, point);


if (nFlags & MK_LBUTTON && !m_bMouseOnBtn) 
return;


CWnd* pWnd = GetActiveWindow();
CWnd* pParent = GetOwner();


if ((GetCapture() != this) && 
(
#ifndef ST_LIKEIE
pWnd != NULL && 
#endif
pParent != NULL)) 
{
m_bMouseOnBtn = true;
//SetFocus(); // Thanks Ralph!
SetCapture();
Invalidate();
}
else
{
CRect rc;
GetClientRect(&rc);
if (!rc.PtInRect(point))
{
// Redraw only if mouse goes out
if (m_bMouseOnBtn)
{
m_bMouseOnBtn = false;
Invalidate();
}
// If user is NOT pressing left button then release capture!
if (!(nFlags & MK_LBUTTON)) 
ReleaseCapture();
}
}
}


void CCfsButton::OnKillFocus(CWnd* pNewWnd)
{
CButton::OnKillFocus(pNewWnd);


// TODO: 在此處新增訊息處理程式程式碼
if (m_bMouseOnBtn)
{
m_bMouseOnBtn = false;
Invalidate();
}
}


void CCfsButton::SetTextAlign( int iAlign )
{
if (iAlign >= CFSBTN_HORIZ && iAlign < MAX_CFSBTN_ALIGNMODE)
{
m_iTextAlign = iAlign;
Invalidate();
}
}


void CCfsButton::SetTextShow( bool bShow )
{
m_bTextShow = bShow;
}


void CCfsButton::SetDrawBorder( bool bBorder )
{
m_bDrawBorder = bBorder;
}


int CCfsButton::GetTextALign( void ) const
{
return m_iTextAlign;
}


bool CCfsButton::GetTextShow( void ) const
{
return m_bTextShow;
}


bool CCfsButton::GetDrawBorder( void ) const
{
return m_bDrawBorder;
}


void CCfsButton::SetDefActiveBgColor( bool bRepaint /*= false*/ )
{
m_clrActiveBg = ::GetSysColor(COLOR_BTNFACE); 
if (bRepaint) Invalidate();
}


void CCfsButton::SetDefInactiveBgColor( bool bRepaint /*= false*/ )
{
m_clrActiveFg = ::GetSysColor(COLOR_BTNTEXT); 
if (bRepaint) Invalidate();
}


void CCfsButton::SetDefActiveFgColor( bool bRepaint /*= false*/ )
{
m_clrInactiveBg = ::GetSysColor(COLOR_BTNFACE); 
if (bRepaint) Invalidate();
}


void CCfsButton::SetDefInactiveFgColor( bool bRepaint /*= false*/ )
{
m_clrInactiveFg = ::GetSysColor(COLOR_BTNTEXT); 
if (bRepaint) Invalidate();
}


COLORREF CCfsButton::GetDefActiveColor( void ) const
{
return  ::GetSysColor(COLOR_BTNFACE);
}


COLORREF CCfsButton::GetDefInactiveColor( void ) const
{
return ::GetSysColor(COLOR_BTNTEXT);
}


void CCfsButton::SetActiveBgColor( COLORREF clrSelf, bool bRepaint /*= false*/ )
{
m_clrActiveBg = clrSelf;
if (bRepaint)
{
Invalidate();
}
}


void CCfsButton::SetInactiveBgColor( COLORREF clrSelf, bool bRepaint /*= false*/ )
{
m_clrInactiveBg = clrSelf;
if (bRepaint)
{
Invalidate();
}
}


void CCfsButton::SetActiveFgColor( COLORREF clrSelf, bool bRepaint /*= false*/ )
{
m_clrInactiveBg = clrSelf;
if (bRepaint)
{
Invalidate();
}
}


void CCfsButton::SetInactiveFgColor( COLORREF clrSelf, bool bRepaint /*= false*/ )
{
m_clrInactiveFg = clrSelf;
if (bRepaint)
{
Invalidate();
}
}


COLORREF CCfsButton::GetDefActiveBgColor( void ) const
{
return  ::GetSysColor(COLOR_BTNFACE);
}


COLORREF CCfsButton::GetDefInactiveBgColor( void ) const
{
return ::GetSysColor(COLOR_BTNTEXT);
}


COLORREF CCfsButton::GetDefActiveFgColor( void ) const
{
return  ::GetSysColor(COLOR_BTNFACE);
}


COLORREF CCfsButton::GetDefInactiveFgColor( void ) const
{
return ::GetSysColor(COLOR_BTNTEXT);
}


COLORREF CCfsButton::GetActiveBgColor( void ) const
{
return m_clrActiveBg;
}


COLORREF CCfsButton::GetInactiveBgColor( void ) const
{
return m_clrInactiveBg;
}


COLORREF CCfsButton::GetActiveFgColor( void ) const
{
return m_clrActiveFg;
}


COLORREF CCfsButton::GetInactiveFgColor( void ) const
{
return m_clrInactiveFg;
}


bool CCfsButton::SetIcon( LPCTSTR lpNormal, LPCTSTR lpOver /*= NULL*/, LPCTSTR lpDisable /*= NULL*/, CSize szBtn /*= CSize(0,0)*/ )
{
return true;
}


bool CCfsButton::SetPng( LPCTSTR lpNormal, LPCTSTR lpOver /*= NULL*/, LPCTSTR lpDisable /*= NULL*/, CSize szBtn /*= CSize(0,0)*/ )
{
if (lpNormal == NULL)
{
return false;
}


UninitParam();
m_btnComf[0].pImage = new Gdiplus::Bitmap(lpNormal);


if (lpOver)
{
m_btnComf[1].pImage = new Gdiplus::Bitmap(lpOver);
}
if (lpDisable)
{
m_btnComf[2].pImage = new Gdiplus::Bitmap(lpDisable);
}


for (int i=0; i<3; ++i)
{
if (m_btnComf[i].pImage)
{
m_btnComf[i].iWidth = m_btnComf[i].pImage->GetWidth();
m_btnComf[i].iHeight = m_btnComf[i].pImage->GetHeight();
MoveWindow(0, 0, m_btnComf[i].iWidth, m_btnComf[i].iHeight);
}
}
RedrawWindow();


return true;
}


相關文章