手机不知道解锁密码:自己写的按钮类(继承CButton的不是CWnd)
来源:百度文库 编辑:中财网 时间:2024/04/25 09:56:04
*******************************************************************************
文件名称 : CSkinButton.h 头文件
作 者 : 杨..
创建时间 : 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 : 实现文件
/*******************************************************************************
文件名称 : SkinButton.cpp 实现文件
作 者 : 杨..
创建时间 : 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 消息处理程序
/********************************************************************
函数名称 : 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();
}
/********************************************************************
函数名称 : 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();
}
/********************************************************************
函数名称 : SetButtonCursor(HCURSOR hCursor)
*********************************************************************/
BOOL CSkinButton::SetButtonCursor(HCURSOR hCursor)
{
m_hCursor=hCursor;
if(m_hCursor==NULL){
SetDefaultCursor();
return FALSE;
}
return TRUE;
}
/********************************************************************
函数名称 : SetDefaultCursor()
函数描述 : ......
输入参数 : ....
输出参数 : .....
返回值 : ....
备注 : .....
*********************************************************************/
void CSkinButton::SetDefaultCursor()
{
m_hCursor=LoadCursor(NULL,MAKEINTRESOURCE(32649));
}
/********************************************************************
函数名称 : SetToolTipText(CString strText)
函数描述 : ...
输入参数 : ..........
输出参数 : .............
返回值 : ..
备注 : ..
*********************************************************************/
void CSkinButton::SetToolTipText(CString strText)
{
m_ToolTip.UpdateTipText(strText,this,TIP_ID);
}
/********************************************************************
函数名称 : SetBitmap(UINT nNormalID, UINT nMouseOverID)
函数描述 :
输入参数 :
输出参数 :
返回值 :
备注 :
*********************************************************************/
BOOL CSkinButton::SetBitmap(UINT nNormalID, UINT nMouseOverID)
{
m_nNormalID=nNormalID;
m_nMouseOverID=nMouseOverID;
AdjustPosition();
Invalidate();
return TRUE;
}
/********************************************************************
函数名称 : PreSubclassWindow()
函数描述 : ???
输入参数 : ???
输出参数 : 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();
}
/********************************************************************
函数名称 : PreTranslateMessage(MSG* pMsg)
函数描述 : ???
输入参数 : ???
输出参数 : 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);
}
/********************************************************************
函数名称 : OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
函数描述 : ???
输入参数 : ???
输出参数 : 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;
}
/********************************************************************
函数名称 : OnMouseMove(UINT nFlags, CPoint point)
函数描述 : ???
输入参数 : ???
输出参数 : 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);
}
/********************************************************************
函数名称 : OnTimer(UINT nIDEvent)
函数描述 : ???
输入参数 : ???
输出参数 : 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);
}
/********************************************************************
函数名称 : AdjustPosition()
函数描述 : ???
输入参数 : ???
输出参数 : void
返回值 : void
备注 : ???
*********************************************************************/
void CSkinButton::AdjustPosition()
{
CBitmap bitmap;
BITMAP m_Bitmap;
CRect rect;
bitmap.LoadBitmap(m_nNormalID);
bitmap.GetBitmap(&m_Bitmap);
GetWindowRect(&rect);//根据图象的大小调整按钮大小
pWndParent->ScreenToClient(&rect);
rect.right=rect.left+m_Bitmap.bmWidth;
rect.bottom=rect.top+m_Bitmap.bmHeight;
MoveWindow(&rect,TRUE);
}
用时,拖入按钮资源Owner Draw选为TRUE,用这个类关联对象,然后调用Init(UINT nNormalID, UINT nMouseOverID,CString strTipText);函数。参数nNormalID是一般状态位图ID,参数nMouseOverID是热点状态位图ID,参数strTipText热点时显示按钮功能;按钮会跟着图片大小变化。确定按钮位置:CRect rect; rect.right=横坐标终点值;rect.bottom=纵坐标终点值; rect.left=横坐标起点值; rect.top=纵坐标起点值;,然后调用MoveWindow(&rect,TRUE);函数,就把按钮移到自己要的位置了。