您的位置:首页 > 其它

VC 封装按钮

2015-03-27 17:22 99 查看
#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;

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