VC 封装按钮

#pragma once
#include "afxwin.h"


class CSkinButton : public CButton
{
public:
 CSkinButton(void);
 ~CSkinButton(void);

 DECLARE_DYNAMIC(CSkinButton)

public:
 void LoadBitmaps(HBITMAP Normal, HBITMAP Hot, HBITMAP Pressed, HBITMAP Disable);

public:
 HBITMAP m_BitmapNormal;
 HBITMAP m_BitmapHot;
 HBITMAP m_BitmapPressed;
 HBITMAP m_BitmapDisable;

 BOOL m_MouseHover;
 BOOL m_bTextColorFlag;
 BOOL m_bFontFlag;
 COLORREF m_Colorref;
 int m_nFontHeight;
 int m_nFontWeight;
 TCHAR m_szFontName[LF_FACESIZE];

public:
 inline void SetCustomFlag(bool bFlag){m_bCustom = bFlag;}
 

private:
 bool m_bCustom;

private:
 virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);

 afx_msg void OnMouseMove(UINT nFlags, CPoint point);
 afx_msg LRESULT OnMouseLeave(WPARAM wParam, LPARAM lParam);

 DECLARE_MESSAGE_MAP()
 

};

 

 

 

 

 

 

#include "StdAfx.h"
#include "SkinButton.h"
#include "CommFunc.h"


IMPLEMENT_DYNAMIC(CSkinButton, CButton)

CSkinButton::CSkinButton(void)
{
 m_MouseHover = FALSE;

 m_BitmapNormal = NULL;
 m_BitmapHot = NULL;
 m_BitmapPressed = NULL;
 m_BitmapDisable = NULL;
 m_bCustom = false;
 m_bTextColorFlag = FALSE;
 m_bFontFlag = FALSE;
}

CSkinButton::~CSkinButton(void)
{
}

BEGIN_MESSAGE_MAP(CSkinButton, CButton)
 ON_WM_MOUSEMOVE()
 ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
END_MESSAGE_MAP()

void CSkinButton::LoadBitmaps(HBITMAP Normal, HBITMAP Hot, HBITMAP Pressed, HBITMAP Disable)
{
 m_BitmapNormal = Normal;
 m_BitmapHot = Hot;
 m_BitmapPressed = Pressed;
 m_BitmapDisable = Disable;
}

void CSkinButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
 if (!(GetStyle() & WS_VISIBLE))
 {
  return;
 }

 RECT rc;
 GetClientRect(&rc);
 HBITMAP bitmap;
 if (lpDrawItemStruct->itemState & ODS_DISABLED)
  bitmap = m_BitmapDisable;
 else if (lpDrawItemStruct->itemState & ODS_SELECTED)
  bitmap = m_BitmapPressed;
 else
 {
  if (m_MouseHover)
   bitmap = m_BitmapHot;
  else
   bitmap = m_BitmapNormal;
 }
 HDC dc = CreateCompatibleDC(lpDrawItemStruct->hDC);
 HBITMAP oldBitmap = (HBITMAP)SelectObject(dc, bitmap);
 //BitBlt(lpDrawItemStruct->hDC, rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, dc, 0, 0, SRCCOPY);

 BITMAP bmp;
 ::GetObject(bitmap, sizeof(BITMAP), &bmp);

 SetStretchBltMode(lpDrawItemStruct->hDC,COLORONCOLOR/*HALFTONE*/);
 StretchBlt(lpDrawItemStruct->hDC,rc.left, rc.top,rc.right-rc.left,rc.bottom-rc.top, dc,0,0,bmp.bmWidth,bmp.bmHeight,SRCCOPY);

 if (m_bCustom)
 {
  CString text;
  GetWindowText(text);
  SetBkMode(lpDrawItemStruct->hDC, TRANSPARENT);

   if (m_bTextColorFlag)
   {
    SetTextColor(lpDrawItemStruct->hDC, m_Colorref);
   }

  if (m_bFontFlag)
  {
   CDC *pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
   CFont *pFont,font;
   LOGFONT lf;
   memset(&lf, 0, sizeof(LOGFONT));
   if (m_nFontHeight == 0)
   {
    lf.lfHeight = 20;
    lf.lfWeight = FW_SEMIBOLD;
    _tcscpy(lf.lfFaceName, _T("Ebrima"));
   }
   else
   {
    lf.lfHeight = m_nFontHeight;
    lf.lfWeight = m_nFontWeight;
    _tcscpy(lf.lfFaceName, m_szFontName);
   }
   
   font.CreateFontIndirect(&lf);
   pFont = pDC->SelectObject (&font);

   DrawText(lpDrawItemStruct->hDC, text, text.GetLength(), &rc, DT_CENTER|DT_VCENTER|DT_SINGLELINE);

   pDC->SelectObject (pFont);
   font.DeleteObject();
  }
  else
  {
   DrawText(lpDrawItemStruct->hDC, text, text.GetLength(), &rc, DT_CENTER|DT_VCENTER|DT_SINGLELINE);
  }
 }

 SelectObject(dc, oldBitmap);
 DeleteDC(dc);
}

void CSkinButton::OnMouseMove(UINT nFlags, CPoint point)
{
 if (!m_MouseHover)
 {
  Invalidate();
  TRACKMOUSEEVENT tme = {sizeof(TRACKMOUSEEVENT), TME_LEAVE, m_hWnd, 0};
  m_MouseHover = TrackMouseEvent(&tme);
 }

 CButton::OnMouseMove(nFlags, point);
}

LRESULT CSkinButton::OnMouseLeave(WPARAM wParam, LPARAM lParam)
{
 Invalidate();
 m_MouseHover = FALSE;
 return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值