关闭

封装按钮

311人阅读 评论(0) 收藏 举报
分类:
// SkinButton.cpp : 实现文件
//
#include "stdafx.h"
#include "project.h"
#include "SkinButton.h"


// SkinButton
IMPLEMENT_DYNAMIC(SkinButton, CButton)
SkinButton::SkinButton()
{
 m_bMouseLeave = FALSE;
 m_BitmapNormal = NULL;
 m_BitmapHot = NULL;
 m_BitmapPress = NULL;
 m_BitmapDisnable = NULL;
}
SkinButton::~SkinButton()
{
}


BEGIN_MESSAGE_MAP(SkinButton, CButton)
 ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
 ON_MESSAGE(WM_MOUSEHOVER, OnMouseHover)
 ON_WM_MOUSEMOVE()
END_MESSAGE_MAP()
 
// SkinButton 消息处理程序
LRESULT SkinButton::OnMouseLeave(WPARAM Wparam, LPARAM Lparam)
{
 Invalidate();
 m_bMouseLeave = FALSE;
 return 0;
}
LRESULT SkinButton::OnMouseHover(WPARAM Wparam, LPARAM Lparam)
{
 Invalidate();
 m_bMouseLeave = TRUE;
 return 0;
}
void SkinButton::LoadBitmaps(HBITMAP hLeave, HBITMAP hHonver, HBITMAP hClick, HBITMAP hDisable)
{
 m_BitmapNormal = hLeave;
 m_BitmapHot = hHonver;
 m_BitmapPress = hClick;
 m_BitmapDisnable = hDisable;
}
void SkinButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
 // TODO:  添加您的代码以绘制指定项
 if ( GetStyle() == WS_VISIBLE )
 {
  return ;
 }
 HBITMAP bitmap;
 if (lpDrawItemStruct->itemState == ODS_DISABLED)
 {
  bitmap = m_BitmapDisnable;
 } 
 else if( lpDrawItemStruct->itemState == ODS_SELECTED)
 {
  bitmap = m_BitmapPress;
 }
 else
 {
  if (!m_bMouseLeave)
  {
   bitmap = m_BitmapNormal;
  }
  else
  {
   bitmap = m_BitmapHot;
  }
 }
  HDC dc = CreateCompatibleDC(lpDrawItemStruct->hDC);
  HBITMAP OldBitmap = (HBITMAP)SelectObject(dc, bitmap);
  RECT rect = lpDrawItemStruct->rcItem;
  //BitBlt(lpDrawItemStruct->hDC, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, dc, 0, 0, SRCCOPY);


  BITMAP bmp;
  ::GetObject(bitmap, sizeof(BITMAP), &bmp);
  SetStretchBltMode(lpDrawItemStruct->hDC,COLORONCOLOR/*HALFTONE*/);
  StretchBlt(lpDrawItemStruct->hDC,rect.left, rect.top,rect.right-rect.left,rect.bottom-rect.top, dc,0,0,bmp.bmWidth,bmp.bmHeight,SRCCOPY);
  CString text;
  GetWindowText(text);
  SetBkMode(lpDrawItemStruct->hDC, TRANSPARENT);
 
 DrawText(lpDrawItemStruct->hDC, text, text.GetLength(), &rect, DT_CENTER|DT_VCENTER|DT_SINGLELINE);
 
 SelectObject(dc, OldBitmap);
 DeleteDC(dc);
}
void SkinButton::OnMouseMove(UINT nFlags, CPoint point)
{
 // TODO: 在此添加消息处理程序代码和/或调用默认值
 if (!m_bMouseLeave)
 {
  Invalidate();
  TRACKMOUSEEVENT tme = {sizeof(TRACKMOUSEEVENT), TME_LEAVE, m_hWnd, 0};
  m_bMouseLeave = TrackMouseEvent(&tme); 
 }
 CButton::OnMouseMove(nFlags, point);
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:75391次
    • 积分:1687
    • 等级:
    • 排名:千里之外
    • 原创:70篇
    • 转载:175篇
    • 译文:0篇
    • 评论:1条
    最新评论