您的位置:首页 > 其它

封装按钮

2015-04-28 15:58 141 查看
// 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);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: