*******************************************************************************
文件名稱 : CSkinButton.h 頭文件
作 者 : 楊..
創(chuàng)建時間 : 2010-11-4 16:50:13
文件描述 :
版權聲明 : Copyright (C) 2010-2012 ..科技
修改歷史 : 楊.. 2010-11-4 1.00 初始版本
*******************************************************************************/
#pragma once
// CSkinButton
class CSkinButton : public CButton
{
DECLARE_DYNAMIC(CSkinButton)
public:
CSkinButton();
virtual ~CSkinButton();
protected:
DECLARE_MESSAGE_MAP()
public:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
virtual BOOL PreTranslateMessage(MSG* pMsg);
protected:
virtual void PreSubclassWindow();
public:
void Init(UINT nNormalID,UINT nMouseOverID,CString strTipText);
BOOL SetBitmap(UINT nNormalID,UINT nMouseOverID);
void SetToolTipText(CString strText);
BOOL SetButtonCursor(HCURSOR hCursor);
protected:
afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg void OnTimer(UINT nIDEvent);
private:
void AdjustPosition();
CWnd* pWndParent;
UINT m_nMouseOverID;
UINT m_nNormalID;
CToolTipCtrl m_ToolTip;
void SetDefaultCursor();
HCURSOR m_hCursor;
BOOL m_bMouseOver;
};
// SkinButton.cpp : 實現(xiàn)文件
/*******************************************************************************
文件名稱 : SkinButton.cpp 實現(xiàn)文件
作 者 : 楊..
創(chuàng)建時間 : 2010-11-4 17:47:35
文件描述 :
版權聲明 : Copyright (C) 2010-2012 ..科技
修改歷史 : 楊..2010-11-4 1.00 初始版本
*******************************************************************************/
#include "stdafx.h"
#include "YPlayer.h"
#include "SkinButton.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define TIP_ID 1
// CSkinButton
IMPLEMENT_DYNAMIC(CSkinButton, CButton)
CSkinButton::CSkinButton()
{
}
CSkinButton::~CSkinButton()
{
}
BEGIN_MESSAGE_MAP(CSkinButton, CButton)
ON_WM_SETCURSOR()
ON_WM_MOUSEMOVE()
ON_WM_TIMER()
END_MESSAGE_MAP()
// CSkinButton 消息處理程序
/********************************************************************
函數(shù)名稱 : Init(UINT nNormalID, UINT nMouseOverID,CString strTipText)
*********************************************************************/
void CSkinButton::Init(UINT nNormalID, UINT nMouseOverID,CString strTipText)
{
m_nNormalID=nNormalID;
m_nMouseOverID=nMouseOverID;
m_ToolTip.UpdateTipText(strTipText,this,TIP_ID);
pWndParent=this->GetParent();
AdjustPosition();
m_bMouseOver=FALSE;
Invalidate();
}
/********************************************************************
函數(shù)名稱 : DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
*********************************************************************/
void CSkinButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: Add your code to draw the specified item
UINT nID;
CDC*pDC;
pDC=CDC::FromHandle(lpDrawItemStruct->hDC);
UINT nState=lpDrawItemStruct->itemState;
if(m_bMouseOver&&(!(nState & ODS_SELECTED)))
nID=m_nMouseOverID;
else
nID=m_nNormalID;
CBitmap bitmap;
BITMAP m_Bitmap;
CDC MemDC;
bitmap.LoadBitmap(nID);
MemDC.CreateCompatibleDC(pDC);
MemDC.SelectObject(&bitmap);
bitmap.GetBitmap(&m_Bitmap);
pDC->BitBlt(0,0,m_Bitmap.bmWidth,m_Bitmap.bmHeight,&MemDC,0,0,SRCCOPY);
bitmap.DeleteObject();
MemDC.DeleteDC();
}
/********************************************************************
函數(shù)名稱 : SetButtonCursor(HCURSOR hCursor)
*********************************************************************/
BOOL CSkinButton::SetButtonCursor(HCURSOR hCursor)
{
m_hCursor=hCursor;
if(m_hCursor==NULL){
SetDefaultCursor();
return FALSE;
}
return TRUE;
}
/********************************************************************
函數(shù)名稱 : SetDefaultCursor()
函數(shù)描述 : ......
輸入?yún)?shù) : ....
輸出參數(shù) : .....
返回值 : ....
備注 : .....
*********************************************************************/
void CSkinButton::SetDefaultCursor()
{
m_hCursor=LoadCursor(NULL,MAKEINTRESOURCE(32649));
}
/********************************************************************
函數(shù)名稱 : SetToolTipText(CString strText)
函數(shù)描述 : ...
輸入?yún)?shù) : ..........
輸出參數(shù) : .............
返回值 : ..
備注 : ..
*********************************************************************/
void CSkinButton::SetToolTipText(CString strText)
{
m_ToolTip.UpdateTipText(strText,this,TIP_ID);
}
/********************************************************************
函數(shù)名稱 : SetBitmap(UINT nNormalID, UINT nMouseOverID)
函數(shù)描述 :
輸入?yún)?shù) :
輸出參數(shù) :
返回值 :
備注 :
*********************************************************************/
BOOL CSkinButton::SetBitmap(UINT nNormalID, UINT nMouseOverID)
{
m_nNormalID=nNormalID;
m_nMouseOverID=nMouseOverID;
AdjustPosition();
Invalidate();
return TRUE;
}
/********************************************************************
函數(shù)名稱 : PreSubclassWindow()
函數(shù)描述 : ???
輸入?yún)?shù) : ???
輸出參數(shù) : void
返回值 : void
備注 : ???
*********************************************************************/
void CSkinButton::PreSubclassWindow()
{
// TODO: Add your specialized code here and/or call the base class
SetDefaultCursor();
CRect rect;
GetClientRect(&rect);
m_ToolTip.Create(this);
m_ToolTip.SetDelayTime(100);
m_ToolTip.SetMaxTipWidth(200);
m_ToolTip.AddTool(this,"",rect,TIP_ID);
CButton::PreSubclassWindow();
}
/********************************************************************
函數(shù)名稱 : PreTranslateMessage(MSG* pMsg)
函數(shù)描述 : ???
輸入?yún)?shù) : ???
輸出參數(shù) : void
返回值 : void
備注 : ???
*********************************************************************/
BOOL CSkinButton::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
m_ToolTip.RelayEvent(pMsg);
return CButton::PreTranslateMessage(pMsg);
}
/********************************************************************
函數(shù)名稱 : OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
函數(shù)描述 : ???
輸入?yún)?shù) : ???
輸出參數(shù) : void
返回值 : void
備注 : ???
*********************************************************************/
BOOL CSkinButton::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
// TODO: Add your message handler code here and/or call default
if (m_hCursor) //如果設置了光標,就使用新設置的 光標
{
::SetCursor(m_hCursor);
return TRUE;
}
return FALSE;
}
/********************************************************************
函數(shù)名稱 : OnMouseMove(UINT nFlags, CPoint point)
函數(shù)描述 : ???
輸入?yún)?shù) : ???
輸出參數(shù) : void
返回值 : void
備注 : ???
*********************************************************************/
void CSkinButton::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_bMouseOver=TRUE;
SetTimer(1,100,NULL);
Invalidate();
OnTimer(1);
CButton::OnMouseMove(nFlags, point);
}
/********************************************************************
函數(shù)名稱 : OnTimer(UINT nIDEvent)
函數(shù)描述 : ???
輸入?yún)?shù) : ???
輸出參數(shù) : void
返回值 : void
備注 : ???
*********************************************************************/
void CSkinButton::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
POINT point;
GetCursorPos(&point);
ScreenToClient(&point);
CRect rect;
GetClientRect(&rect);
if(!rect.PtInRect(point)){
KillTimer(1);
m_bMouseOver=FALSE;
Invalidate();
}
CButton::OnTimer(nIDEvent);
}
/********************************************************************
函數(shù)名稱 : AdjustPosition()
函數(shù)描述 : ???
輸入?yún)?shù) : ???
輸出參數(shù) : void
返回值 : void
備注 : ???
*********************************************************************/
void CSkinButton::AdjustPosition()
{
CBitmap bitmap;
BITMAP m_Bitmap;
CRect rect;
bitmap.LoadBitmap(m_nNormalID);
bitmap.GetBitmap(&m_Bitmap);
GetWindowRect(&rect);//根據(jù)圖象的大小調(diào)整按鈕大小
pWndParent->ScreenToClient(&rect);
rect.right=rect.left+m_Bitmap.bmWidth;
rect.bottom=rect.top+m_Bitmap.bmHeight;
MoveWindow(&rect,TRUE);
}
用時,拖入按鈕資源Owner Draw選為TRUE,用這個類關聯(lián)對象,然后調(diào)用Init(UINT nNormalID, UINT nMouseOverID,CString strTipText);函數(shù)。參數(shù)nNormalID是一般狀態(tài)位圖ID,參數(shù)nMouseOverID是熱點狀態(tài)位圖ID,參數(shù)strTipText熱點時顯示按鈕功能;按鈕會跟著圖片大小變化。確定按鈕位置:CRect rect; rect.right=橫坐標終點值;rect.bottom=縱坐標終點值; rect.left=橫坐標起點值; rect.top=縱坐標起點值;,然后調(diào)用MoveWindow(&rect,TRUE);函數(shù),就把按鈕移到自己要的位置了。