時隔1年半後,因工作需要重新寫起了用戶端。這麼久沒接觸了,确實很生疏,一考慮到要自繪個tab控件需要有個關閉按鈕,剛開始我的邏輯就是用公司現成的按鈕類但發現現成的按鈕類,用起來很費勁,因為分裝好的按鈕類提供了比較完整的各種功能和接口,要是想在原來的基礎上修改,就需要搞清楚各個成員及接口的用法,這加大了很大的難度,比起從零開始寫,或許還更耗時,相信很多人都會有類似的感受,于是我網上搜尋和參考了别人的文章封裝了本按鈕。本按鈕提供的功能非常簡單,但是易用且可擴充性強,完全可以根據自己的需求做修改.
//使用例子
RECT rc = {0, 0, 20, 20};
m_btnClose.LoadBtnImg(_T("PNG"),IDB_PNG_TAB_CLOSE_NOR, IDB_PNG_TAB_CLOSE_HOT,IDB_PNG_TAB_CLOSE_HOT);
m_btnClose.Create(this, rc, L"",ID_BTN_MYTAB_CLOSE);
m_btnClose.SetToolTipText(_T("關閉"));
//頭檔案
#pragma once
// CLhsButton
#define MYWM_BTN_CLICK WM_USER+3001 //關閉按鈕單擊響應
//tab按鈕的狀态
enum ButtonState
{
BTN_STATE_NOR = 0,
BTN_STATE_DOWN = 1,
};
class CLhsButton : public CWnd
{
DECLARE_DYNAMIC(CLhsButton)
public:
CLhsButton();
virtual ~CLhsButton();
bool Create(CWnd* pParent,CRect rc,CString text,DWORD id = 0,DWORD style = WS_VISIBLE|WS_CHILD);
DECLARE_MESSAGE_MAP()
public:
protected:
CString szClassName;
bool m_isMouseHover; //滑鼠是否懸浮
bool m_isMouseClicked; //滑鼠是否單擊
CString m_strShowText; //要顯示的文字
Image* m_pImgNor; //正常時的圖檔
Image* m_pImgHot; //滑鼠懸浮時的圖檔
Image* m_pImgDown; //單擊按下時的圖檔
void PostClickEvent();
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg void OnMouseHover(UINT nFlags, CPoint point);
afx_msg void OnMouseLeave();
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
afx_msg void OnPaint();
afx_msg void OnSize(UINT nType, int cx, int cy);
virtual BOOL PreTranslateMessage(MSG* pMsg);
public:
void SetTabState(ButtonState state){m_btnState = state; Invalidate();} //設定tab狀态
ButtonState GetTabState(){return m_btnState;}
void SetToolTipText(CString spText, BOOL bActivate = TRUE);
void LoadBtnImg(LPCTSTR tType, UINT nNorID, UINT nHotID, UINT nDownID); //加載按鈕圖檔
private:
ButtonState m_btnState; //tab的狀态
CToolTipCtrl* m_pToolTip;
CString m_tooltext;
public:
afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
};
//源檔案
// CLhsButton.cpp : 實作檔案
//
#include "stdafx.h"
#include "../Lander_mini.h"
#include "memdc.h"
#include "../Utility.h"
#include "LhsButton.h"
// CLhsButton
IMPLEMENT_DYNAMIC(CLhsButton, CWnd)
CLhsButton::CLhsButton()
{
m_isMouseHover = false;
m_isMouseClicked = false;
// 注冊控件類
szClassName = AfxRegisterWndClass(0);
m_pImgNor = NULL;
m_pImgHot = NULL;
m_pImgDown = NULL;
m_btnState = BTN_STATE_NOR;
m_pToolTip = NULL;
}
CLhsButton::~CLhsButton()
{
SAFE_RELEASE(m_pToolTip);
SAFE_RELEASE(m_pImgNor);
SAFE_RELEASE(m_pImgHot);
SAFE_RELEASE(m_pImgDown);
}
BEGIN_MESSAGE_MAP(CLhsButton, CWnd)
ON_WM_MOUSEMOVE()
ON_WM_MOUSEHOVER() // 此消息系統并不會給我們發送
ON_WM_MOUSELEAVE()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_PAINT()
ON_WM_SIZE()
ON_WM_ERASEBKGND()
ON_WM_CREATE()
ON_WM_CTLCOLOR()
END_MESSAGE_MAP()
// CLhsButton 消息處理程式
bool CLhsButton::Create(CWnd* pParent,CRect rc,CString text,DWORD id /* = 0 */,DWORD style /* = WS_VISIBLE|WS_CHILD */)
{
// 動态建立控件
BOOL ret = CWnd::CreateEx(0, szClassName, text, style, rc, pParent, id);
return ret ? true : false;
}
int CLhsButton::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
//::SetWindowLong(m_hWnd, GWL_EXSTYLE, GetWindowLong(m_hWnd, GWL_EXSTYLE) ^ WS_EX_LAYERED);
//ModifyStyle(WS_CLIPCHILDREN, 0);
//ModifyStyle(0, WS_CLIPCHILDREN , 0);
return 0;
}
void CLhsButton::PostClickEvent()
{
// 該函數用來向父視窗發送 單擊 消息
CWnd* parent = GetParent();
if(parent)
parent->SendMessage(MYWM_BTN_CLICK, 0,0);
}
void CLhsButton::OnMouseHover(UINT nFlags, CPoint point)
{
// 滑鼠進入
Invalidate();
}
void CLhsButton::OnMouseMove(UINT nFlags, CPoint point)
{
// 隻處理滑鼠第一次進入時的情況
if(!m_isMouseHover)
{
m_isMouseHover = true;
TRACKMOUSEEVENT evt = { sizeof(evt), TME_LEAVE|TME_HOVER, m_hWnd, 0 };
TrackMouseEvent(&evt);
OnMouseHover(0,CPoint());
}
}
void CLhsButton::OnMouseLeave()
{
// 滑鼠離開
m_isMouseHover = false;
m_isMouseClicked = false;
Invalidate();
}
void CLhsButton::OnLButtonDown(UINT nFlags, CPoint point)
{
// 滑鼠按下
m_isMouseClicked = true;
Invalidate();
}
void CLhsButton::OnLButtonUp(UINT nFlags, CPoint point)
{
// 滑鼠松開
if(m_isMouseClicked)
{
m_isMouseClicked = false;
Invalidate();
PostClickEvent();
}
}
BOOL CLhsButton::OnEraseBkgnd(CDC* pDC)
{
// return CWnd::OnEraseBkgnd(pDC);
return TRUE; // 阻止擦除背景,防止閃爍
}
void CLhsButton::OnPaint()
{
CPaintDC dc(this);
CRect rc;
GetClientRect(&rc);
// 采用雙緩存,防止閃爍
CMemDC memdc(&dc,&rc,TRUE);
Graphics graphic(memdc);
if (!m_pImgNor || !m_pImgHot || !m_pImgDown)
{//沒有提供按鈕圖檔就刷下背景
// 刷背景
COLORREF bkgnd = RGB(100,0,0);
if(m_isMouseHover)
{
if(m_isMouseClicked)
bkgnd = RGB(250,0,0);
else
bkgnd = RGB(180,0,0);
}
memdc.FillSolidRect(&rc,bkgnd);
}
if (m_isMouseClicked || m_btnState == BTN_STATE_DOWN)
{//單擊一定畫單擊狀态
graphic.DrawImage(m_pImgDown, 0, 0, m_pImgDown->GetWidth(), m_pImgDown->GetHeight());
}
else if (m_isMouseHover && !m_isMouseClicked)
{
//懸浮,但是沒單擊
graphic.DrawImage(m_pImgHot, 0, 0, m_pImgHot->GetWidth(), m_pImgHot->GetHeight());
}
else
{
graphic.DrawImage(m_pImgNor, 0, 0, m_pImgNor->GetWidth(), m_pImgNor->GetHeight());
}
// 使繪制生效
graphic.ReleaseHDC(memdc);
}
BOOL CLhsButton::PreTranslateMessage(MSG* pMsg)
{
if (m_pToolTip)
{
if (::IsWindow(m_pToolTip->m_hWnd))
{
m_pToolTip->RelayEvent(pMsg);
}
}
return CWnd::PreTranslateMessage(pMsg);
}
void CLhsButton::OnSize(UINT nType, int cx, int cy)
{
}
void CLhsButton::SetToolTipText(CString spText, BOOL bActivate)
{
if (m_pToolTip == NULL)
{
m_pToolTip = new CToolTipCtrl;
// Create ToolTip control
m_pToolTip->Create(this);
m_pToolTip->Activate(TRUE);
}
m_tooltext = spText;
// If there is no tooltip defined then add it
if (m_pToolTip->GetToolCount() == 0)
{
CRect rectBtn;
GetClientRect(rectBtn);
m_pToolTip->AddTool(this, m_tooltext, rectBtn, 1);
}
// Set text for tooltip
m_pToolTip->UpdateTipText(m_tooltext, this, 1);
m_pToolTip->SetDelayTime(2000);
m_pToolTip->Activate(bActivate);
}
HBRUSH CLhsButton::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CWnd::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: 在此更改 DC 的任何屬性
// TODO: 如果預設的不是所需畫筆,則傳回另一個畫筆
return hbr;
}
//加載按鈕圖檔
void CLhsButton::LoadBtnImg(LPCTSTR tType, UINT nNorID, UINT nHotID, UINT nDownID)
{
m_pImgNor = CQYUtility::LoadImgFromRes(_T("PNG"), nNorID);
m_pImgHot = CQYUtility::LoadImgFromRes(_T("PNG"), nHotID);
m_pImgDown = CQYUtility::LoadImgFromRes(_T("PNG"), nDownID);
}
<pre name="code" class="cpp">#ifndef _MEMDC_H_
#define _MEMDC_H_
//
// CMemDC - memory DC
//
class CMemDC : public CDC {
private:
CBitmap m_bitmap; // Offscreen bitmap
CBitmap* m_oldBitmap; // bitmap originally found in CMemDC
CDC* m_pDC; // Saves CDC passed in constructor
CRect m_rect; // Rectangle of drawing area.
BOOL m_bMemDC; // TRUE if CDC really is a Memory DC.
public:
void Build( CDC* pDC )
{
ASSERT(pDC != NULL);
m_pDC = pDC;
m_oldBitmap = NULL;
m_bMemDC = !pDC->IsPrinting();
if( m_bMemDC )
{
CreateCompatibleDC(pDC);
pDC->LPtoDP(&m_rect);
m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width(), m_rect.Height());
m_oldBitmap = SelectObject(&m_bitmap);
SetMapMode(pDC->GetMapMode());
pDC->DPtoLP(&m_rect);
SetWindowOrg(m_rect.left, m_rect.top);
}
else
{
m_bPrinting = pDC->m_bPrinting;
m_hDC = pDC->m_hDC;
m_hAttribDC = pDC->m_hAttribDC;
}
FillSolidRect( m_rect, pDC->GetBkColor() );
}
CMemDC(CDC* pDC, const CRect* pRect = NULL, BOOL bBg = FALSE) : CDC()
{
ASSERT(pDC != NULL);
// Some initialization
m_pDC = pDC;
m_oldBitmap = NULL;
m_bMemDC = !pDC->IsPrinting();
// Get the rectangle to draw
if (pRect == NULL) {
pDC->GetClipBox(&m_rect);
} else {
m_rect = *pRect;
}
if (m_bMemDC) {
// Create a Memory DC
CreateCompatibleDC(pDC);
pDC->LPtoDP(&m_rect);
m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width(), m_rect.Height());
m_oldBitmap = SelectObject(&m_bitmap);
SetMapMode(pDC->GetMapMode());
SetWindowExt(pDC->GetWindowExt());
SetViewportExt(pDC->GetViewportExt());
pDC->DPtoLP(&m_rect);
SetWindowOrg(m_rect.left, m_rect.top);
} else {
// Make a copy of the relevent parts of the current DC for printing
m_bPrinting = pDC->m_bPrinting;
m_hDC = pDC->m_hDC;
m_hAttribDC = pDC->m_hAttribDC;
}
// Fill background
if( bBg )
BitBlt(m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height(),
m_pDC, m_rect.left, m_rect.top, SRCCOPY);
else
FillSolidRect(m_rect, pDC->GetBkColor());
}
~CMemDC()
{
if (m_bMemDC) {
// Copy the offscreen bitmap onto the screen.
m_pDC->BitBlt(m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height(),
this, m_rect.left, m_rect.top, SRCCOPY);
//Swap back the original bitmap.
SelectObject(m_oldBitmap);
} else {
// All we need to do is replace the DC with an illegal value,
// this keeps us from accidently deleting the handles associated with
// the CDC that was passed to the constructor.
m_hDC = m_hAttribDC = NULL;
}
}
// Allow usage as a pointer
CMemDC* operator->()
{
return this;
}
// Allow usage as a pointer
operator CMemDC*()
{
return this;
}
CMemDC( HDC hDC )
: CDC()
{
CDC::FromHandle( hDC )->GetClipBox( &m_rect );
Build( CDC::FromHandle( hDC ) );
}
};
#endif
自繪Tab控件:點選打開連結