智慧控制元件變幻
標頭檔案
#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;
}
相關文章
- 12:變幻的矩陣矩陣
- [NOIP 2024 模擬3]變幻
- canvas實現的變幻線程式碼例項Canvas線程
- 如何應用 matrix3d 對映變幻3D
- UE乾貨| UE虛幻引擎除錯神器—控制元件反射器除錯控制元件反射
- switch控制元件(變更顏色)控制元件
- wps演示教程:妙用控制元件工具在幻燈片裡播放網路影片控制元件
- 高階自定義View — 粒子變幻、隧道雜湊、組合文字View
- 高階自定義View --- 粒子變幻、隧道雜湊、組合文字View
- js 改變 控制元件的屬性值JS控制元件
- 熱門變形筆記本編輯推薦 變幻莫測碾壓一切筆記
- java實現控制元件的移動及使用滑鼠改變控制元件大小Java控制元件
- 從典範走向幻滅:谷歌13億元的智慧城市夢谷歌
- tkinter中scale拖拉改變值控制元件(十一)控制元件
- 夢幻布丁
- 多變的智慧降噪
- win7桌面背景幻燈片功能:桌面隨時變Win7
- 在執行時使用滑鼠移動控制元件和改變控制元件的大小 (轉)控制元件
- 智慧家居如何把老款定頻空調變成智慧“變頻”空調#米家#智慧家居#HA
- 底部隨輸入法高度變化而變化的控制元件SoftLinearLayout控制元件
- 從“魚虎”相爭到“魚虎”幫,看遊戲直播的風雲變幻遊戲
- 匯聚6年思想變遷:知識圖譜報告幻燈片大全
- 排版幻燈片
- 工業富聯智慧嬗變
- [22] 虛幻引擎知識擴充 智慧指標、JSON解析、外掛指標JSON
- 【智慧製造】製造業智慧變革之道
- 【智慧物流】智慧製造下的智慧供應鏈變革
- 改變自定義UIButton裡子控制元件的位置UI控制元件
- Android之改變控制元件的背景及形態Android控制元件
- Android 動態佈局 動態生成 銷燬控制元件 改變控制元件的位置等Android控制元件
- 夢幻西遊公式蒐集公式
- 破解幻八角
- 一勞永逸讓VB自動改變控制元件大小控制元件
- 基於canvas繪製的一個跟隨滑鼠變幻的動態背景線條Canvas
- 智慧裝備無處不在 連奶嘴都變的智慧了!
- 智慧變色T恤 顏色隨運動狀態變
- 實現控制元件的移動、改變大小(DELPHI實現) (轉)控制元件
- 8階幻方「穿越介面」的384組基礎~夢幻之匙(上)