概述
// 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);
}
最后
以上就是陶醉往事为你收集整理的封装按钮的全部内容,希望文章能够帮你解决封装按钮所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复