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;
}
#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;
}
相关文章推荐
- VC改变对话框按钮字体颜色和背景的解决方案(转)
- 封装自定义View,创建N个按钮,按钮根据title长度Size-fit显示
- VC封装LDAP开发接口
- VB的ShowInTaskbar功能分析以及用VC的实现 如何隐藏对话框在工具栏上的按钮
- VC++ 6.0点打开按钮出现 “Microsoft (R) Developer Studio 已停止工作”的解决方法
- 项目中封装了一个弹出框但是点击上面的按钮就崩溃,尽在iOS9.0以下版本出现
- VC单选按钮控件Radio Button用法
- 基于VC++ PNG按钮的实现【1】
- vc 串口通讯类封装
- 对话框封装类(普通对话框,列表对话框,单选按钮对话框,复选对话框)
- vc响应最大化、最小化按钮
- BCB中封装VC控件CodeSense
- 神经网络-3层BP网的VC动态库封装
- VC实现自定义按钮响应拖动
- VC 位图按钮CBitmapButton的使用
- VC++/MFC自己封装好的一个访问数据库的类
- VC++ CxSkinButton加载封装在dll文件中的资源文件(图片)
- easyui treegrid 封装(不用分页,用加载更多按钮)延迟加载加加载更多
- VC下动态创建了BUTTON按钮后,如何设置按钮的Modal Frame风格???
- VC2010 浮动工具栏 按钮